Malik
Malik

Reputation: 123

Keep a user logged in with cookies

I have this forum and I want to keep users logged in if they check the check box that says logged it. So i added this php code to set the cookie :

$year = time() + 31536000;
    if($_POST['remember']) {
setcookie('email', $_SESSION['email'], $year);
}
elseif(!$_POST['remember']) {
if(isset($_COOKIE['remember_me'])) {
    $past = time() - 100;
    setcookie(email, gone, $past);
}
}

But, how do i keep them signed in into my forum using this cookie, I have been stuck on this for some time now.

Upvotes: 2

Views: 3727

Answers (3)

hammad
hammad

Reputation: 21

First check for the cookie that if cookie is set assign that variable to your session variable and do the rest of your code

Upvotes: 1

Yasir Ahmed
Yasir Ahmed

Reputation: 533

put this code on top of your login page

if(isset($_COOKIE["email"])){

$_SESSION['email']=$_COOKIE["email"];

header("location: some-page.php");

exit();

}

thats all

Upvotes: 3

Iftikar Urrhman Khan
Iftikar Urrhman Khan

Reputation: 1131

For keep user signed every time your page loads you have to check weather your cookie is set if it is then you can redirect user to your internal pages and pass login page,for that you can set user id in encrypted form by which you can get user information.

Upvotes: 1

Related Questions