doubledee Posted March 6, 2012 Share Posted March 6, 2012 Please refresh my memory... What is the life of a Session? If my script creates the following... $_SESSION['loggedIn'] = $loggedIn; ...then what is it when I close my browser window? I believe it dies. Is that true? Is that *always* true? Debbie Quote Link to comment Share on other sites More sharing options...
Zane Posted March 6, 2012 Share Posted March 6, 2012 So long as you have session.cookie_lifetime = 0; in your php.ini file, then yeah, the browser will destroy the session. Quote Link to comment Share on other sites More sharing options...
doubledee Posted March 6, 2012 Author Share Posted March 6, 2012 So long as you have session.cookie_lifetime = 0; in your php.ini file, then yeah, the browser will destroy the session. How do I find out what that is on my VPS? Is it in phpinfo() ? Is there anyway to do that in my code? Debbie Quote Link to comment Share on other sites More sharing options...
Zane Posted March 6, 2012 Share Posted March 6, 2012 Is it in phpinfo() ? Yes Quote Link to comment Share on other sites More sharing options...
marcus Posted March 6, 2012 Share Posted March 6, 2012 print_r(session_get_cookie_params()); works as well. Quote Link to comment Share on other sites More sharing options...
doubledee Posted March 6, 2012 Author Share Posted March 6, 2012 print_r(session_get_cookie_params()); works as well. I am trying to find a way to update my database - without using JavaScript - if a User leaves my website by closing their browser. Specifically, when someone reading an Article looks at those who have posted Comments, I don't want any Users who have closed their browsers to still appear as being "Online". I was thinking one way to do this using PHP only would be to look for $_SESSION['loggedIn'] = $loggedIn; before I display who made Comments, and if $_SESSION['loggedIn'] = then have my script update the database to mark the User as "Offline". Would that work?! Debbie Quote Link to comment Share on other sites More sharing options...
doubledee Posted March 6, 2012 Author Share Posted March 6, 2012 Snap! That wouldn't work because the script to display User Comments for User A wouldn't be able to see the Session Data for other Users, right?! Debbie Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted March 6, 2012 Share Posted March 6, 2012 <?php // 7200 = Time in seconds for the cookie to be active; set to "0" for it to be active until the browser closes // "/" = Where this cookie can access, currently set to root // ".site.com" = allow this cookie to access current domain and it's sub-domains; remove the first "." to only allow with in the domain and not subdomain // FALSE = This cookie doesn't require a Secure Network session_set_cookie_params(7200,"/",".site.com",FALSE); session_start(); ?> Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted March 6, 2012 Share Posted March 6, 2012 That's why most sites have users as "recently online", where once they are logged in, viewing any webpage will update the user's "last_activity" value and the "Who's Online" box just shows the users who have a last_activity value within the last 5-10 minutes. Of course it doesn't account for someone who sits and reads one web page for 20 minutes, but there's no way to monitor idle activity(that I know of) Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted March 6, 2012 Share Posted March 6, 2012 but there's no way to monitor idle activity(that I know of) ajax Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 6, 2012 Share Posted March 6, 2012 http://ajaxpatterns.org/Timeout as to what little guy said it can be done in Ajax. Quote Link to comment Share on other sites More sharing options...
doubledee Posted March 6, 2012 Author Share Posted March 6, 2012 That's why most sites have users as "recently online", where once they are logged in, viewing any webpage will update the user's "last_activity" value and the "Who's Online" box just shows the users who have a last_activity value within the last 5-10 minutes. Of course it doesn't account for someone who sits and reads one web page for 20 minutes, but there's no way to monitor idle activity(that I know of) I'm determined to get the behavior I want without Ajax! How about this... When I display User Comments, for each Poster I check the database to see if their "logged_in" field is set to "1" AND I also look at the "last_activity" field and compare that against the current system time(). If more than, say 4 hours, have passed, then I run an UPDATE on the User's record and change "logged_in" to "0" which would log the Poster out from an "Online/Offline" standpoint, but not log them out since that is dictated by their own personal SESSION. I'm looking at my code, and I think this would work, except I don't know if it is possible with my Prepared Statements to run an UPDATE query inside this code... // ******************************** // Display Comments on Article. * // ******************************** while (mysqli_stmt_fetch($stmt2)){ // Set Photo Label. $photoLabel = (empty($photoLabel) ? $username : $photoLabel) echo '<div class="post">'; // ******************** // Display User Info. * // ******************** echo ' <div class="userInfo"> <a href="#" class="username"> <strong>' . nl2br(htmlentities($username, ENT_QUOTES)) . '</strong> </a>'; if ($loggedIn==1){ //<!-- Member Online --> echo ' <img src="/images/Light_Green_10.png" width="10" alt="Member Status: Online" title="Green Light. Credit: Someone, Wikimedia Commons." /> <br />'; }else{ //<!-- Member Offline --> echo ' <img src="/images/Light_Gray_10.png" width="10" alt="Member Status: Offline" title="Gray Light. Credit: Someone, Wikimedia Commons." /> <br />'; } Hope that makes sense?! Debbie Quote Link to comment Share on other sites More sharing options...
Zane Posted March 6, 2012 Share Posted March 6, 2012 check out onUnload and onBeforeUnload Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.