Php set session with cookies
$_SESSION['user_id'] = $login;
setcookie('user_id', '$login' , time()+86000);
header('Location: userindex.php');
function logged_in() {
return (isset($_SESSION['user_id']) || isset($_COOKIE['user_id']) ? true : false;
}
I have a SESSION, but I also have a COOKIE, but I don't know how to restart a SESSION with a COOKIE. I have no idea how I can get this. I create a COOKIE but can't log out and have problems with SESSION, can someone help me fix my problem ???? And on each page at the top, I have a logged_in function to check if the user is logged in or not. I won this logged_in function to check if the cookie user has automatic login to the user's cookie. I think the code needs to be written in the logged_in function and ...
source to share
Note that this is not safe as anyone can create a cookie using something like firebug.
@session_start();
function logged_in() {
if(!isset($_SESSION['user_id']) && isset($_COOKIE['user_id'])) {
$_SESSION['user_id'] = $_COOKIE['user_id'];
}
return isset($_SESSION['user_id']);
}
function logout() {
unset($_SESSION['user_id']);
setcookie("user_id", "", time() - 3600);
header("Location: http://".$_SERVER['HTTP_HOST']);
exit;
}
Edit: Added logout () - will remove both sessions and cookie 'user_id' and then redirect to main page
source to share
First: you have to install it with:
setcookie('user_id', $login , time()+86000);
So $ login without quotes. And also maybe set a path variable if this cookie should appear on different pages.
Deleting a cookie is done by setting a negative time value:
setcookie('user_id', '' , time()-86000);
source to share