Login with Cookie or Session on PHP5

Asked

Viewed 522 times

2

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

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

  • I recommend reading http://answall.com/questions/33664/remin-usu%C3%A1rio-com-seguran%C3%A7a/33684#33684

1 answer

2


You can use only $_COOKIE.

You can set the values in one $_COOKIE thus:

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

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

setcookie("loginCredentials", ""); 

And finally to check if 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 be something like this:

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

Just be careful not to leave passwords and information unprotected.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.