madmatter23
10-14-2007, 02:56 AM
I have a redirect code that seems to work for all but one of my pages. The code does this: checks to see if a user is logged in. If not, then it stores the URL that the user is trying to access and provides a login link. On the logged in page, the code checks to see if a redirect variable has been stored. If so, it redirects the user back to the page they were trying to access. Sounds simple.
So, page1.php:
session_start();
if (!isset($_SESSION['username'])) {
$_SESSION['redirect'] = "http://localhost/userdirectory/$querieduser";
die("<div id='message'><br/><br/><br/>You must first <a href='http://localhost/login'> login </a> before accessing this page.</div>");
}
This part of the codes seems to work. When I go to it without being logged in, it displays the login message. When echoing $_SESSION['redirect'] on this page, it shows the correct redirect url. Let's say for example, the queried user is bluemoon. It shows http://localhost/userdirectory/bluemoon. So far so good.
Next page: login.php - just a login form, no php.
Next page: loggedin.php (target of login.php form)
session_start();
//connectionString
$username = mysql_real_escape_string($_POST['username']);
$password = sha1(mysql_real_escape_string($_POST['password']));
$querypw = "SELECT password FROM users WHERE username='$username'";
$pwaction = @mysql_query($querypw, $dbc);
$ccpwarray = mysql_fetch_row($pwaction);
$ccpassword = $ccpwarray[0];
if ($ccpassword == $password) {
$LOGGEDIN = TRUE;
$_SESSION['username'] = $username;
if (isset($_SESSION['redirect']) && $_SESSION['redirect'] != "") {
$REDIRECT = TRUE;
header( "refresh: 3; url={$_SESSION['redirect']}" );
$_SESSION['redirect'] = "";
}
else {
header( 'refresh: 3; url= http://local/main.php' );
$_SESSION['redirect'] = "";
}
}
else {
$LOGGEDIN = FALSE;
}
The important part of this script is where it checks for $_SESSION['redirect']. After I login, it successfully sees that the variable is set and redirects me. However, instead of directing me to http://localhost/userdirectory/bluemoon (which was the value of $_SESSION['redirect'] on page1.php) it takes me to http://localhost/userdirectory/images/masthead/tabs.
So, somewhere in these smalls bits of code, the value of $_SESSION['redirect'] change from "http://localhost/userdirectory/bluemoon" to "http://localhost/userdirectory/images/masthead/tabs". I don't know how the hell the value is getting changed, or why it would be changed to this. images/masthead/tabs isn't even referenced in any of this code.
It's especially confusing because
1) the code on page1.php dies after displaying the login message. So, no subsequent php code on that page could be changing the value.
2) the snippet of code for loggedin.php that I have provided is not preceded by any other php code. So, no other section of code before this could be changing the value.
3) I use this exact direct method for other pages, and it works fine.
The problem must lie in the small snippets of code that I've listed here. But it all seems so straightforward, I can't imagine where it's going wrong. I'm going nuts trying to figure out where the problem could be.
I would really appreciate any input on this problem. This is the last bug on my entire site and it's like a cockroach. It just won't die.
So, page1.php:
session_start();
if (!isset($_SESSION['username'])) {
$_SESSION['redirect'] = "http://localhost/userdirectory/$querieduser";
die("<div id='message'><br/><br/><br/>You must first <a href='http://localhost/login'> login </a> before accessing this page.</div>");
}
This part of the codes seems to work. When I go to it without being logged in, it displays the login message. When echoing $_SESSION['redirect'] on this page, it shows the correct redirect url. Let's say for example, the queried user is bluemoon. It shows http://localhost/userdirectory/bluemoon. So far so good.
Next page: login.php - just a login form, no php.
Next page: loggedin.php (target of login.php form)
session_start();
//connectionString
$username = mysql_real_escape_string($_POST['username']);
$password = sha1(mysql_real_escape_string($_POST['password']));
$querypw = "SELECT password FROM users WHERE username='$username'";
$pwaction = @mysql_query($querypw, $dbc);
$ccpwarray = mysql_fetch_row($pwaction);
$ccpassword = $ccpwarray[0];
if ($ccpassword == $password) {
$LOGGEDIN = TRUE;
$_SESSION['username'] = $username;
if (isset($_SESSION['redirect']) && $_SESSION['redirect'] != "") {
$REDIRECT = TRUE;
header( "refresh: 3; url={$_SESSION['redirect']}" );
$_SESSION['redirect'] = "";
}
else {
header( 'refresh: 3; url= http://local/main.php' );
$_SESSION['redirect'] = "";
}
}
else {
$LOGGEDIN = FALSE;
}
The important part of this script is where it checks for $_SESSION['redirect']. After I login, it successfully sees that the variable is set and redirects me. However, instead of directing me to http://localhost/userdirectory/bluemoon (which was the value of $_SESSION['redirect'] on page1.php) it takes me to http://localhost/userdirectory/images/masthead/tabs.
So, somewhere in these smalls bits of code, the value of $_SESSION['redirect'] change from "http://localhost/userdirectory/bluemoon" to "http://localhost/userdirectory/images/masthead/tabs". I don't know how the hell the value is getting changed, or why it would be changed to this. images/masthead/tabs isn't even referenced in any of this code.
It's especially confusing because
1) the code on page1.php dies after displaying the login message. So, no subsequent php code on that page could be changing the value.
2) the snippet of code for loggedin.php that I have provided is not preceded by any other php code. So, no other section of code before this could be changing the value.
3) I use this exact direct method for other pages, and it works fine.
The problem must lie in the small snippets of code that I've listed here. But it all seems so straightforward, I can't imagine where it's going wrong. I'm going nuts trying to figure out where the problem could be.
I would really appreciate any input on this problem. This is the last bug on my entire site and it's like a cockroach. It just won't die.