Login with Cookie or Session in PHP5 [duplicate]

2

I'm implementing an administrative area on my site and have always used $_SESSION to log in. However, now I need the user login to be saved if it leaves the page.

  

My question: Do I need to use $_COOKIE to save $_SESSION information or can I use only $_COOKIE ?

    
asked by anonymous 29.10.2015 / 15:20

1 answer

2

You can only use $_COOKIE .

You can set the values to a $_COOKIE like this:

setcookie("loginCredentials", $user, time() * 7200); // expira depois de  2 hours

To get around you can just leave the cookie value blank like this:

setcookie("loginCredentials", ""); 

And finally to verify that the user is logged in:

if(isset($_COOKIE['user']['id'] && !empty(isset($_COOKIE['user']['id']))){
// Usuário logado.
}else{
// Usuário não esta logado.
}

This variable $user could look something like this:

$user = array(
    'id' => $id,
    'name' => $name,
    'login' => $login,
) 

Just be careful not to leave passwords and information unprotected.

    
29.10.2015 / 16:06