I know this is a basic question so it should be a basic answer. I have always done a user login system using sessions only, I would like to now make it an option for a user to stay logged in when they come back to my site if they choose to by using cookies. I am not sure the best way of doing this but I have a small mockup of the basic functionality the way I think it might work below. Please tell me if that looks about right or if I should be doing it differently. The cookie will most likely hold a user ID and some encrypted key that would be re-generated everytime they "login"
// see if Session is set
if (!isset($_SESSION['userID']) || $_SESSION['userID'] == ''){
// session is not set so see if cookie is set
if (isset($_COOKIE['userID'])){
//cookie is set so check that it is valid login ID and key
// if it returns tru then we will also initiate there session value so they will be logged in
}else{
// redirect to login page
}
if (!isset($_SESSION['userID']) || $_SESSION['userID'] == ''){
// redirect to login page
}
}else{
//user is logged in already
}