943,884 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 3513
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 18th, 2009
0

Logout after time

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Toxikr3 is offline Offline
28 posts
since Jul 2009
Jul 19th, 2009
1

Re: Logout after time

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.
Reputation Points: 20
Solved Threads: 13
Junior Poster in Training
humbug is offline Offline
93 posts
since Oct 2005
Jul 19th, 2009
0

Re: Logout after time

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Toxikr3 is offline Offline
28 posts
since Jul 2009
Jul 19th, 2009
0

Re: Logout after time

Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 731
Bite my shiny metal ass!
pritaeas is offline Offline
4,177 posts
since Jul 2006
Jul 19th, 2009
0

Re: Logout after time

Thank you, I will try that now.
-Toxikr3
Reputation Points: 10
Solved Threads: 0
Light Poster
Toxikr3 is offline Offline
28 posts
since Jul 2009
Jul 19th, 2009
0

Re: Logout after time

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:

PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
Toxikr3 is offline Offline
28 posts
since Jul 2009
Jul 19th, 2009
1

Re: Logout after time

php Syntax (Toggle Plain Text)
  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. ?>
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 731
Bite my shiny metal ass!
pritaeas is offline Offline
4,177 posts
since Jul 2006
Jul 19th, 2009
0

Re: Logout after time

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Toxikr3 is offline Offline
28 posts
since Jul 2009
Jul 19th, 2009
0

Re: Logout after time

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
Reputation Points: 10
Solved Threads: 0
Light Poster
Toxikr3 is offline Offline
28 posts
since Jul 2009
Jul 19th, 2009
0

Re: Logout after time

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).
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC