Remember me cookie

Hey,
What I’m trying to do is a log in page with a session and cookies. The session part works fine but I just can’t get the cookies working.

How is the code below supposed to look? The cookie isn’t being set so I’m guessing that its running the query and immediately exiting and redirecting to the profile page. I find ifs and elses confusing, so any help is greatly appreciated.
Thanks,
Will.

[php] if(mysql_num_rows($result) == TRUE) {
session_regenerate_id();
$member = mysql_fetch_assoc($result);
$_SESSION[‘SESS_MEMBER_ID’] = $member[‘u_id’];
header(“location: member-index.php”);
exit();

	}else {
		//Login failed
		header("location: login-failed.php");
		exit();
	
	if ((isset($_POST['remember'])) && ($_POST['remember'] == "yes")) 
		{ 
		 $cookie_name = "loggedin"; 
		$cookie_value = $_SESSION['SESS_MEMBER_ID']; 
		$expiry = time() + 3600 * 24; 
		setcookie($cookie_name, $cookie_value, $expiry); 
	} 

}[/php]

Follow the logic:

[php]
else {
//Login failed
header(“location: login-failed.php”);
exit();

if ((isset($_POST['remember'])) && ($_POST['remember'] == "yes")) 
{ 
    $cookie_name = "loggedin"; 
    $cookie_value = $_SESSION['SESS_MEMBER_ID']; 
    $expiry = time() + 3600 * 24; 
    setcookie($cookie_name, $cookie_value, $expiry); 
} 

}
[/php]

If the login fails, redirect the page. Aside from that, you have an exit statement that stops further code from executing. So the if statement under the exit function will never be executed and is just taking up space, as is anything below it because, you have exits everywhere.

Sponsor our Newsletter | Privacy Policy | Terms of Service