how to show if user is logged in?
Moderator: General Moderators
how to show if user is logged in?
How would i show if a user is logged in?i am using sessions, thanks 
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.
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.
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.
BTW, your session id is stored in the user PC's cache too.
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
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
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:
Then to show the number of users online, or do any other scripts involving online members:
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;);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.for some reason
it always inserts 00:00:00 into lastactive
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