My first post here guys. Thanks in advance for any answers :)
Anyway, I have a login script (login.php) that has the following code upon submission of the credentials:
if(isset($_POST['submit'])) {
$password=$_POST['password'];
$email = (string)$_POST['email'];
try {
$db = new PDO('mysql:dbname=dbname;host=localhost', 'username', 'password'); // MY REAL DETAILS ARE USED HERE, JUST DON'T NEED THEM ON THE INTERNET
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$sth = $db->prepare('SELECT * FROM members WHERE email = :email LIMIT 1');
$sth->bindParam(':email', $email);
$sth->execute();
$user = $sth->fetch(PDO::FETCH_OBJ);
// Hashing the password with its hash as the salt returns the same hash
if (crypt($password, $user->password) == $user->password) {
if($user->id > 0) {
if($user->status == 'Y' && date('Ymd', time($_SESSION['expiration'])) == date('Ymd')) {
$data = array('status' => 'D');
$update = mysql_update('members', $data, '`id`='.$_SESSION['memberID']);
}
$status = mysql_select('SELECT status FROM members WHERE id='.$user->id);
$_SESSION['memberID'] = $user->id;
$_SESSION['expiration'] = $user->expiration_date;
$_SESSION['timeout'] = time();
if(isset($_REQUEST['url'])) header('Location: '.$_REQUEST['url']);
else header('Location: account.php');
} else {
$error = "Whoops! That wasn't supposed to happen, please contact the webmaster.";
}
} else {
$error = "Sorry, something didn't quite match up. Please try again.";
// print $user->password;
// print "<br />";
// print crypt($password, $user->password);
}
}
The problem is that when $_REQUEST['url']
is set, the redirect occurs BUT is sent right back to my login script.
Example: my url is mysite.com/login.php?url=account.php
and I click submit on login. It runs through the above code, and sends me straight back to login.php
as if the session wasn't set. The code that tells account.php
to go back to login is this:
if(!isset($_SESSION['memberID'])) header('Location: login.php?url=account.php');
I don't understand why, but when I do a hard refresh CTRL + F5
the session IS set and it takes me back to index.php
because login.php
says
if(isset($_SESSION['memberID'])) header('Location: index.php');
I'm pulling my hair out because this bothers me so much. Any help is appreciated.
I'm Using PHP version 5.3.26 just if you guys are curious. I know when my session is set because i have a page (php.php) that prints out my session with var_dump($_SESSION);
Thanks!
Edit: My Problem is not the redirect. Logically this is what should happen, in order:
- User enters login data and clicks submit
- The login page then checks their data
- Set Session Vars
- Go to account.php
Account.php should see if my session is set if(!isset($_SESSION['memberID'])) header('Location: login.php?url=account.php');
so this line is the part that is not reading my session.
Back at login.php the session is set with:
$_SESSION['memberID'] = $user->id;
$_SESSION['expiration'] = $user->expiration_date;
$_SESSION['timeout'] = time();
if(isset($_REQUEST['url'])) header('Location: '.$_REQUEST['url']);
else header('Location: account.php');
This part HAS to be working because the session IS being set AND going to account.php, but account.php is not reading the session when it is set. WHY?
This is NOT a cache problem either because I've cleared my caches and tried to limit caching of my page as well.