Looking for guidance on how to accomplish the following.
Default login:
User is logged in with a session lifetime of 25 minutes, or until the browser closes:
Now, there are two functions that I want to incorperate with the login feature:
1 - Stay Logged In
2 - Remember Me
...this is where I'm struggleing conceptually. If the user selects both remember me, and stay logged in, they are expecting this session to remain valid until the browser closes as well as have their name populate when they return later. Since the stay logged in feature sets the expire time to = 0 on the cookie, my cookie is invalid after that point. Thus, I can't recall the users name on a returning visit. If I set the expire time to = 30 days, so i have access to the user name, then I am unable to keep the user logged in until the browser closes because I most likely will be doing login status checks to compare expire time to last refresh time...
I hope I've kinda got'n the idea across that I'm struggling with. Stay logged requires an expire time = 0, while remember me requires an expire time = 30.
What if my user wants both options??
Default login:
User is logged in with a session lifetime of 25 minutes, or until the browser closes:
Code:
// path for cookies - valid for all paths in domain $cookie_path = "/"; // timeout value for the cookie $cookie_timeout = 60 * 60 * 25; // timeout value for the garbage collector $garbage_timeout = $cookie_timeout + (60 * 10); //cookie + 10 minutes session_name(); // dynamically set - beyond question scope session_id(); // dynamically set - beyond question scope session_set_cookie_params($cookie_timeout, $cookie_path); // set the garbage collector to clean the session files ini_set('session.gc_maxlifetime', $garbage_timeout); // set new session directory to ensurer unique garbage collection $sessdir = ini_get('session.save_path').DIRECTORY_SEPARATOR."visitor"; if (!is_dir($sessdir)) { mkdir($sessdir, 0777); } ini_set('session.save_path', $sessdir); session_start();
1 - Stay Logged In
- I want the users session to remain valid for as long as the browser is open.
- I believe this could be accomplished by setting the session.cookie_ lifetime = 0, either in the session_set_coo kie_params() or later in setcookie() by setting expire = 0.
- I also understand it to be true that if I issue a cookie with an expire setting different that my default setting, I would want to save this cookie into a different session director (perhaps "stay_logge d" instead of "visitor" as above?). This is necessary to ensure that the server side session file is not impacted by a conflicting garbage collection process.
2 - Remember Me
- I want the system to remember the users login name, so that when they return to the site their name can auto populate into the name field of the login screen.
...this is where I'm struggleing conceptually. If the user selects both remember me, and stay logged in, they are expecting this session to remain valid until the browser closes as well as have their name populate when they return later. Since the stay logged in feature sets the expire time to = 0 on the cookie, my cookie is invalid after that point. Thus, I can't recall the users name on a returning visit. If I set the expire time to = 30 days, so i have access to the user name, then I am unable to keep the user logged in until the browser closes because I most likely will be doing login status checks to compare expire time to last refresh time...
I hope I've kinda got'n the idea across that I'm struggling with. Stay logged requires an expire time = 0, while remember me requires an expire time = 30.
What if my user wants both options??
Comment