Page 1 of 1
how to show if user is logged in?
Posted: Fri Mar 18, 2005 3:29 am
by ecaandrew
How would i show if a user is logged in?i am using sessions, thanks

Posted: Fri Mar 18, 2005 3:37 am
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.
Posted: Fri Mar 18, 2005 4:00 am
by ecaandrew
how would i remove the session after xx minutes???
Posted: Fri Mar 18, 2005 4:28 am
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.
Posted: Fri Mar 18, 2005 4:35 am
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
Posted: Fri Mar 18, 2005 8:18 am
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("e;INSERT INTO tablename (lastactive) VALUES ('$time')"e;);
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.
Posted: Fri Mar 18, 2005 6:41 pm
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
Posted: Fri Mar 18, 2005 7:11 pm
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