954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Logout after time

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

Toxikr3
Light Poster
28 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

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.

humbug
Junior Poster in Training
93 posts since Oct 2005
Reputation Points: 20
Solved Threads: 13
 

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

Toxikr3
Light Poster
28 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 
pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Thank you, I will try that now.
-Toxikr3

Toxikr3
Light Poster
28 posts since Jul 2009
Reputation Points: 10
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_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

Toxikr3
Light Poster
28 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 
<?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
	}
?>
pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

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.

Toxikr3
Light Poster
28 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

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

Toxikr3
Light Poster
28 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

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).

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

I am using a free webhost and don't know if they have cron on it, is there a code to check it or should I just go to help and support?

Toxikr3
Light Poster
28 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Its best to check with help and support.

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

Yes! It is available. I never noticed it before -_-

I don't know how to use it, is it possible for you to help me out a bit? Please and thank you.

EDIT:
Do I just run the code that was previously posted? I am guessing so...right?

-Toxikr3

Toxikr3
Light Poster
28 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

If your host has cpanel, you can do it from there easily.

Otherwise, check out some of the sites listed in the search.

http://www.google.com/#hl=en&q=crontab+howto&aq=4&oq=cront&aqi=g10&fp=Xmf0jJ9P_V0

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

Yes, it has a cpanel, it only asks me to put the file name to run, and when to run it. Its quite simple, but I don't know if I need to run the code posted or a new code. Will the code know which user to put in the variable $userfinal? I don't know if it will work...

-Toxikr3

Toxikr3
Light Poster
28 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

You will have to modify the code and maybe your database.

Pretty much you will need to use humbugs' solution.

In the cron file you will need to select all users that have not been active for lets say 10 minutes. Then run an update query for each user returned to set their status to 'offline'.

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

Alright, I will try that, thank you.
-Toxikr3

Toxikr3
Light Poster
28 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Sorry for not getting back to you about my idea. What I meant was, you store the timestamp for each user's last activity in the database. Then any time you want to check if someone is "active" you grab the timestamp and make the comparison.

You can also get a list of "active" user by doing (psuedocode) "SELECT * FROM user_activity WHERE timestamp>TIME()-60*5"

All you need to do for this is update the user's "last activity" timestamp every time they request a page, and you'll be laughing.

If you want it to be more accurate you can use AJAX to send a request to the server every 4 mins to tell the server that the user still has the webpage open in his/her browser.

Hope that helped. (I'm not saying the other way won't work either..)

humbug
Junior Poster in Training
93 posts since Oct 2005
Reputation Points: 20
Solved Threads: 13
 

Thanks! I was wondering about the comparison code :D
-Toxikr3

Toxikr3
Light Poster
28 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

For each user you want to check, you would do something like this:
1. Get the timestamp for this user from the database
SELECT last_activity FROM user_activity WHERE id=$id
2. Compare this value with the timestamp for 5 mins ago within PHP if ($last_activity > $now-60*5) { echo "online" }

I couldn't be bothered looking up the PHP code for getting timestamps, or translating to and from MySQL timestamps, but you can do that :P

humbug
Junior Poster in Training
93 posts since Oct 2005
Reputation Points: 20
Solved Threads: 13
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You