how to show if user is logged in?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ecaandrew
Forum Commoner
Posts: 72
Joined: Fri Nov 12, 2004 5:05 pm

how to show if user is logged in?

Post by ecaandrew »

How would i show if a user is logged in?i am using sessions, thanks :)
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Check the session table - in file or database whichever you are using to see if a user is logged in - for these purposes, sessions in database is better.
Keep a timed session where if theres no activity within a period of time then remove his session from the file/db - unless specified logged in forever - for that cookeis come into place.
ecaandrew
Forum Commoner
Posts: 72
Joined: Fri Nov 12, 2004 5:05 pm

Post by ecaandrew »

how would i remove the session after xx minutes???
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

If the user logs on and does nothing (no activitity to and from the server) for xx minutes and then he starts using it - the data will be sent to the server - there you'll need to check the session timeout - I use sessions in a db and use session_set_save_handler () (http://www.php.net/manual/en/function.s ... andler.php) and write code for function gc($maxlifetime).
BTW, your session id is stored in the user PC's cache too.
ecaandrew
Forum Commoner
Posts: 72
Joined: Fri Nov 12, 2004 5:05 pm

Post by ecaandrew »

hmmm i think im confused


so store the session id in the database with a timestamp, but how do i have the server update and delete it after xx minutes? what if he just closes the browser and does not log out, where do i put this code to remove the sessionid after xx minutes??? thanks :) im a lil confused
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

why not just use a simple query at the beginning of all of your pages, that inserts the time into a database field

then if they've not clicked a link within so many minutes it will determine them to be offline

that is how I do it

An example code would look like this:

Code: Select all

// Query at the top of everypage, or in an included file that's included in every page
$time = time();
$active = mysql_query(&quote;INSERT INTO tablename (lastactive) VALUES ('$time')&quote;);
Then to show the number of users online, or do any other scripts involving online members:

Code: Select all

$timeonline = '300'; // Set this to the number of seconds you determine a person to be online since their last page click, this is set to 5 minutes
$timedifference = time() - $timeonline;
$whosonline = mysql_query("SELECT * FROM table WHERE lastactive >= '$timedifference'");
while($whosonlinearray = mysql_fetch_array($whosonline)){
echo $whosonlinearray['username']; } // This particular example would echo the username of who's online.
ecaandrew
Forum Commoner
Posts: 72
Joined: Fri Nov 12, 2004 5:05 pm

Post by ecaandrew »

for some reason

Code: Select all

session_start(); 
include("/home/xeo/public_html/inc/mysql.php");
if ($_SESSION['username']) {
$time = time();
$active = mysql_query("UPDATE loggedin SET lastactive='$time' WHERE username='".$_SESSION['username']."'") or die(mysql_error());
}


it always inserts 00:00:00 into lastactive
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

what is your database type for the (date)time ?

recommended:
- use the mysql NOW() function,
- or use the FROM_UNIXTIME function.

not recommended:
- or change the columntype to int
- or use php strftime function to convert the time() to the mysql format
Post Reply