I'm still plugging away at this over a week later... I am looking or help with my current code... I understand the logic of what I want to do just fine... but I'm piecing the code together from reading various sources:
The logic for what's below is thus: If a "keep me logged in" cookie has been set previously and the PHP header on this protected page is checking to see if the cookie exists... then verifies the user name against the DB, and either allows the user access to the page OR destroys the cookie and bumps them back to the login page:
<?php
session_set_cookie_params (0, '/', '.rimea.org');
session_start();
MySQL_connect("localhost", '******', '******'); MySQL_select_db("users");
if (isset($_COOKIE['cookname'])) {
$_SESSION['user'] = $_COOKIE['user'];
}
$user = $_SESSION['user'];
$sql = "SELECT user
FROM users
WHERE user = '$user'";
$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
if (mysql_num_rows($result) == 1) {
// the user id is verified,
// set the session
$_SESSION['logged_in'] = true;
}else{
/* Variables are incorrect, user not logged in */
unset($_SESSION['user']);
header('Location: login.php?redir='.$_SERVER['PHP_SELF']);
exit;
}
?>
What's actually happening (and I'm sure there's a good reason... but I am a Noo😎 is that I'm being kicked back to the login page, even when the cookie is present.
My orginal "check for session login" header worked just fine... but I'm trying to add the "keep me logged in" with cookie feature.
Orginal PHP "check for login session" header, which works fine for standard sessions:
<?php
session_set_cookie_params (0, '/', '.rimea.org');
session_start();
if($logged_in){
}else{
// not logged in, move to login page
header('Location: login.php?redir='.$_SERVER['PHP_SELF']);
exit;
}
?>
Any help would be appreciated. Thank you.
~Wayne