Logout after time

Thread Solved

Join Date: Jul 2009
Posts: 27
Reputation: Toxikr3 is an unknown quantity at this point 
Solved Threads: 0
Toxikr3 Toxikr3 is offline Offline
Light Poster

Logout after time

 
0
  #1
Jul 18th, 2009
Hi guys, I have been working on a site and have managed to add a login script. I made an "status" script myself which tells people if a user is online or not.

I did that by using database(easiest way I could think of), when person logs in it changes the value in the table "user" in column "onoffstatus" to "Online" and when they click logout it changes it to "offline".

My problem is that if they close window or navigate away from it without clicking logout, it still says "Online". How can I fix it? I have been looking at timeouts and sessions a lot, and can't find what I am looking for.

Somethings to note:
I am using a free site host, and cannot access php.ini
I have tried to use .htaccess but don't know what to do...

I hope you guys can help me. Thanks.
-Toxikr3
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 93
Reputation: humbug is an unknown quantity at this point 
Solved Threads: 13
humbug's Avatar
humbug humbug is offline Offline
Junior Poster in Training

Re: Logout after time

 
1
  #2
Jul 19th, 2009
The easiest way to do this would be to set a column "lastactivity" to the current time every time the user loads any of your pages (just build it into your authentication script). You can then classify someone as "online" if they have been active in the past 5 mins.

Hope that's close enough to what you're after.
"If your not having fun, your doing something wrong." - Humbug
★ Did I help you out? Did I piss you off? Add to my reputation!
The Gabriel Method is a great book for losing weight and keeping healthy - I know Jon Gabriel Personally.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 27
Reputation: Toxikr3 is an unknown quantity at this point 
Solved Threads: 0
Toxikr3 Toxikr3 is offline Offline
Light Poster

Re: Logout after time

 
0
  #3
Jul 19th, 2009
Thank you, I will try that.

EDIT: Well, I looked up time, and it is harder then I thought. Is it possible for you to show how the code can be used?
I have used:
$today = getdate();
print_r ($today):

It prints an array, which I do not know how to use. Also how would I check if they have been inactive for more then 5 mins and then change their logout status?

-Toxikr3
Last edited by Toxikr3; Jul 19th, 2009 at 4:32 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 798
Reputation: pritaeas is on a distinguished road 
Solved Threads: 130
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Master Poster

Re: Logout after time

 
0
  #4
Jul 19th, 2009
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 27
Reputation: Toxikr3 is an unknown quantity at this point 
Solved Threads: 0
Toxikr3 Toxikr3 is offline Offline
Light Poster

Re: Logout after time

 
0
  #5
Jul 19th, 2009
Thank you, I will try that now.
-Toxikr3
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 27
Reputation: Toxikr3 is an unknown quantity at this point 
Solved Threads: 0
Toxikr3 Toxikr3 is offline Offline
Light Poster

Re: Logout after time

 
0
  #6
Jul 19th, 2009
Gah!! It does not work. I have looked at the link and tried both of them, I am currently trying to use the simpler one:

  1. $the_time = time() + (1*60);
  2. if($_SESSION['timestamp'] < $the_time)
  3. {
  4. $_SESSION['timestamp'] = time();
  5. echo "More time added "; //to see if it works
  6. }else{
  7. mysql_query("UPDATE user SET onoffstatus = 'Offline' WHERE username = '$userfinal'");
  8. session_unset(); #Session_unset and Session_destroy
  9. session_destroy();#Will remove all sessions.
  10. header("location:index.php");#This code will send you back to the index page
  11. }
  12. echo "<font color=white>";
  13. echo $the_time;
  14. echo " ";
  15. echo $_SESSION['timestamp'];
  16. echo "</font>";

The last part is to see if the values change(the do) and the "More time" is shown also, but when I wait about 2 min(with page open in background) and then go back to it and click a link, I am still logged in. I also tried closing window and going back after 2 mins and nothing...

Any help, or is there a way to do it with javascript...onunload execute logout? or window close...?

Thanks
-Toxikr3
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 798
Reputation: pritaeas is on a distinguished road 
Solved Threads: 130
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Master Poster

Re: Logout after time

 
1
  #7
Jul 19th, 2009
  1. <?php
  2. if (! session_id()) {
  3. session_start();
  4. }
  5.  
  6. if (! isset($_SESSION['timestamp'])) {
  7. $_SESSION['timestamp'] = time();
  8. }
  9.  
  10. if (time() - $_SESSION['timestamp'] < 60) {
  11. $_SESSION['timestamp'] = time();
  12. echo "More time added "; //to see if it works
  13. }
  14. else {
  15. mysql_query("UPDATE user SET onoffstatus = 'Offline' WHERE username = '$userfinal'");
  16. session_unset(); # Session_unset and Session_destroy
  17. session_destroy(); # Will remove all sessions.
  18. header("location: index.php"); # This code will send you back to the index page
  19. }
  20. ?>
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 27
Reputation: Toxikr3 is an unknown quantity at this point 
Solved Threads: 0
Toxikr3 Toxikr3 is offline Offline
Light Poster

Re: Logout after time

 
0
  #8
Jul 19th, 2009
Thank you! Thank you! Thank you! It has worked, but I have one little concern =D

This only logs you out if you go to the page after the time has elapsed, so it only refreshes the values in the table after some "connection". I was wondering if it is possible to use AJAX to have a timer that keeps checking every minute to see if they are online. Is it possible to do that even if they are not on the page?

If you don't understand my problem:
I am trying to change the value in a table after a period of time EVEN IF the user isn't on the page... is that possible? I hope you understand.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 27
Reputation: Toxikr3 is an unknown quantity at this point 
Solved Threads: 0
Toxikr3 Toxikr3 is offline Offline
Light Poster

Re: Logout after time

 
0
  #9
Jul 19th, 2009
Just realized, I meant even if the user isn't on the website. Like if they have navigated away from the site, or closed their browser.

Sorry for double post.
-Toxikr3
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Logout after time

 
0
  #10
Jul 19th, 2009
Use a cron job to set users offline after a certain amount of time.

Just have it run every 5-10 minutes and that will solve the problem without having to rely on javascript to do it (which is not a good thing to do).
Google is your friend.

Use [code] tags.

If you have found a solution to your problem, please mark the thread as SOLVED.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC