| | |
Logout after time
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 27
Reputation:
Solved Threads: 0
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
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
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.
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.
★ 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.
•
•
Join Date: Jul 2009
Posts: 27
Reputation:
Solved Threads: 0
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
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.
•
•
Join Date: Jul 2009
Posts: 27
Reputation:
Solved Threads: 0
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:
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
PHP Syntax (Toggle Plain Text)
$the_time = time() + (1*60); if($_SESSION['timestamp'] < $the_time) { $_SESSION['timestamp'] = time(); echo "More time added "; //to see if it works }else{ mysql_query("UPDATE user SET onoffstatus = 'Offline' WHERE username = '$userfinal'"); session_unset(); #Session_unset and Session_destroy session_destroy();#Will remove all sessions. header("location:index.php");#This code will send you back to the index page } echo "<font color=white>"; echo $the_time; echo " "; echo $_SESSION['timestamp']; 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
php Syntax (Toggle Plain Text)
<?php if (! session_id()) { session_start(); } if (! isset($_SESSION['timestamp'])) { $_SESSION['timestamp'] = time(); } if (time() - $_SESSION['timestamp'] < 60) { $_SESSION['timestamp'] = time(); echo "More time added "; //to see if it works } else { mysql_query("UPDATE user SET onoffstatus = 'Offline' WHERE username = '$userfinal'"); session_unset(); # Session_unset and Session_destroy session_destroy(); # Will remove all sessions. header("location: index.php"); # This code will send you back to the index page } ?>
"If it is NOT source, it is NOT software."
-- NASA
-- NASA
•
•
Join Date: Jul 2009
Posts: 27
Reputation:
Solved Threads: 0
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.
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.
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).
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.
Use [code] tags.
If you have found a solution to your problem, please mark the thread as SOLVED.
![]() |
Similar Threads
- how to get login and logout time of User..? (PHP)
- finding duration worked between time ranges from login and logout time (ColdFusion)
Other Threads in the PHP Forum
| Thread Tools | Search this Thread |
301 access apache api array beginner binary broken cakephp checkbox class clean cms code compression countingeverycharactersfromastring crack cron curl database date decode directory display dissertation dropdown dynamic echo email error fairness file files folder form forms function functions google href htaccess html httppost image include insert integration ip javascript joomla limit link login mail match md5 menu methods mlm multiple mysql newsletters oop pageing pagerank paypal pdf php problem protocol query radio random recursion remote script search secure server sessions simple sms soap source space spam sql syntax system table tutorial update upload url validator variable video votedown web youtube






