I'm new at Code Igniter. My index page is named abc_welcome where i've below code
public function index()
{
$this->load->library('session');
$login_data = $this->session->userdata('user');
if($login_data) {
redirect('/product/product_details');
}
else {
$this->display();
}
}
So when a already logged in user hit he can redirect to product page. But strangely it is not redirecting.
Where as at login page i've same piece of code and it is redirecting all right.
So when a user come to my www.abc.com page it's not redirecting (if loged in) and session has no data checked but when cliking login button he is and obviously session got the right data. i want him to redirect first time.... more strangely in my demo site (my laptop) Index redirecting is working!!! but not at hostgator.com.
Thanks in advance.
Farness
Thank you all for your support.
i finally made it work by .htaccess redirecting. i just put below code to my .htaccess and it's working as expected.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.abc.com [NC]
RewriteRule ^(.*)$ http://abc.com/$1 [L,R=301]
yes #farness put $config['base_url'] = 'http://www.abc.com/'; in your config.php file
From what you're describing, it seems like your code is fine, if it runs locally on your laptop, so it's likely that the session data isn't being stored/read properly. Aside from the base_url above, you should also verify the following settings in /appication/config.php:
$config['cookie_domain'] should be set to .abc.com (with the leading dot) if your base url is www.abc.com
$config['cookie_path'] just to be safe, set this to "/"
$config['cookie_secure'] set to FALSE (unless your site is accessed only using https, keep this as false
Hopefully that gets you up and running.
Related
this is my first stack overflow question.
I have a home page, where you can login, if you login it sends you to a login php in which you have some code that checks if login is incorrect. If that is the case this code redirects you back to home page:
header("Location: ".SERVER_ADDRESS."home?error=invalid_login");
Now you are back on the home page but the url variable is not set. It does not show in a
var_dump(parse_url($_SERVER['HTTP_REFERER'])); or
var_dump($_GET);
This is my index.php file:
<?php
session_start();
require_once("config.php");
if(!(isset($_GET['page']))) { // redirect to main page
header("Location: ".SERVER_ADDRESS."home");
} else {
$mp = new MainPage($_GET['page']); // redirect to given page
}
?>
If you go try login again with wrong credentials and do the same redirection process it will now show the variable.
Same thing happens if I were to refresh the header after few seconds.
I seem to not understand why the home page would not show the variable straight away, I just want to use the variable for an if statement that would print an extra invalid login message.
I am using wampserver64 for localhosting.
My .htaccess looks like this:
Options FollowSymLinks
RewriteEngine On
RewriteRule ^([0-9a-zA-Z\-\#]+)/?$ index.php?page=$1 [L]
EDIT:
CBroe's answer helped me, my htaccess did not have a QSA flag
Your rule matches on home, and rewrites the request to /index.php?page=home.
You need to add the QSA flag - query string append - to get the new query string you are creating there, merged with any existing one.
I am trying to make clean URLs at my website which i am working on local.
I am using WampServer.
Here is my .htaccess content
RewriteEngine on
RewriteRule ^search/([0-9a-zA-Z]+) search.php?semt=$1 [NC,L]
Here is what i am doing at search.php file.
<?php
if(!isset($_GET["semt"]) || empty($_GET["semt"])){
header("Location: index.php");}
else{
$region = $_GET["semt"];
include('database/config.php');
include('includes/header.php');}
?>
When i am typing,
localhost/project/search.php?semt=xxx
it is quite working.
When i am typing,
localhost/project/search/xxx
it is not working. Browser giving this error.
error screen capture
This page isn’t working
localhost redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS
Final version of the link is that.
localhost/project/search/index.php
How can i solve this problem?
You should see why this is happening from your final url. Change redirect to /index.php.
My site is online, but I used this code to redirect it to a coming soon page.
<?php
if(!isset($_GET['en'])) {
header("Location: /comingsoon");
exit;
}
I then added third party SSL to my site and when I removed that bit of code the initial site would come up, but after the log in page the sessions are not being stored.
When I introduce that bit of code (earlier) back in it works.
This has really got me baffled.
I used this bit of code in HTAccess to redirect from HTTP to HTTPs.
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Not sure if this has anything to do with it.
function redirectToHTTPS()
{
if($_SERVER['HTTPS']!="on")
{
$redirect= "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
header("Location:$redirect");
}
}
// call the above function in that page where you have to redirect to https.
Everything was working fine till I added my .htaccess file. What I'm trying to do is route all my users to their profile page. So www.darudude.com/user1 routes to www.darudude.com/userinfo.php?user=user1
My .htaccess file as this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ userinfo.php?user=$1 [L,QSA]
However ever since I added this, it breaks my sessions. On every page I have a initialize a session, and the sessions stores the referrer. This is the piece of code that handles the important part.
if(isset($_SESSION['url'])){
$this->referrer = $_SESSION['url'];
}else{
$this->referrer = "/index.php";
}
//this echo is used to debug why this thing isn't working!!
echo "<script>alert('".$this->referrer."');</script>";
/* Set current url */
$this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];
and then I'm returned to the original page using this piece of code:
header("Location: ".$session->referrer);
So for example, without the .htaccess file, if I login through one of the pages everything works and I get redirected back to the page I logged in from (i.e. if I logged in from index.php I get redirected back to index.php; if faq.php, I get redirected back to faq.php). With the .htaccess file I keep getting sent to /userinfo.php leading me to thing its something wrong with my rewriterule
This is how its supposed to work:
index.php loads. the $_SESSION['url'] is set to index.php
a login form is enacted whos action redirects to process.php
process.php the $session->referrer is set from $_SESSION['url']
After the login is confirmed the page should redirect using: header("Location: ".$session->referrer);
This is how it worked originally without any problems.
However, after the .htaccess was created it seems to redirect me to userinfo.php. I think it has something to do with my rule.
Any ideas?
I'm not sure if I understand the problem, but the rewrite rule you're using seems to turn the request to /index.php into a request to /userinfo.php?user=/index.php which may not be what you want.
It's because you're relying on $_SERVER['PHP_SELF'], which does not include the query string (the ?user= part of the URI). It worked before because your pages didn't rely on query strings to uniquely identify themselves. You can use $_SERVER['REQUEST_URI'] instead, though look out for circumstances where you don't want the query string to be preserved and now it is being.
Incidentally, the \?* in your RewriteRule regex is doing exactly the same as thing as if it weren't there.
You could try logging in with AJAX, thus never having to refresh the page at all. A simple Google search will throw up plenty of results, see below. Even if you're not using jQuery (which alot of the tutorials seem to expect you to), it's still possible with basic Javascript, in fact that's how I wrote my AJAX log-in script before converting it to use jQuery later.
http://www.google.com/search?q=php+ajax+log-in
In one of the page I have is where administrators are allowed, however, I use if the session isn't set, the header will redirect them to index.php and that method works.
If I replace index.php with home which is for the htaccess which changes it to index.php but it gives an error in the browser
This works:
if(!isset($_SESSION['MEMBER'])){ header("Location: index.php"); }
This does not work:
if(!isset($_SESSION['MEMBER'])){ header("Location: home"); }
htaccess:
RewriteRule ^home$ index.php
The error in Firefox:
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this
address in a way that will never
complete.
This problem can sometimes be caused by disabling or refusing to accept
cookies.
What's wrong with it? How do I get this method to work?
You're redirecting index.php to home in your php file, and home to index.php in .htaccess. Firefox is telling you there is an infinite redirect loop.
Edit:
index.php and home are the same thing, so redirecting to either will result in an infinite loop. You need to do something like this:
#.htaccess
RewriteRule ^home$ index.php # public page
RewriteRule ^members$ members.php # member only page
And then in index.php
# index.php
if(isset($_SESSION['MEMBER'])){
header("Location: members");
exit;
}
Au Contraire. Here is how you carry PHP sessions id's within an .htaccess redirect...
RewriteEngine On
RewriteRule ^(.*)$ /home/ [QSA,L,E=REDIRECT_URL:$1]
Pay attention to the last bit... [QSA,L,E=REDIRECT_URL:$1]
You've created an infinite loop.
When PHP executes the following code:
if(!isset($_SESSION['MEMBER'])){ header("Location: home"); }
It redirects the browser to /home.
But in .htaccess you rewrite the URL to /index.php. Which then again makes PHP to execute the above PHP code.