Jump to content

Keep track of users logged into the system: PHP


9911782

Recommended Posts

Hi All
Can someone help me. I need help.

I have the small system running, where people register in the registration.php page & Logged in after that.

I would like to keep track of the users
1. who last loggin the system e.g last logged in date & time.
2. currently logged into the system?

Can somebody help me  ???
Thank You
Link to comment
Share on other sites

The usual way to do this if you're using a database is to create a table called something like 'active_users', with columns like 'user_id' and 'login_time'.  When a user logs in, you INSERT them on to the table with a current timestamp and when they log out you DELETE them from that table.  You can then do a query on the most recent timestamp to get the most recently logged in user id, or just do a "SELECT user_id FROM active_users ... ", doing a join on your 'users' table for names, to get all currently logged in users.

There are probably other ways, but that's the what I've seen most often.
Link to comment
Share on other sites

Hi bqallover

Thank you very much for ur quick response.

I have manage to get the code http://www.plus2net.com, but Im still faced with small problems.
According to you tips. I have done these:

1. Storing the login information in a table called active_users_online
username,userid, session,time logged in, username, and status(ON/Off).
2. Updating the status of the member, where I have the status stored in in
active_users_online where I have set the status to ON by defualt. Now I have to update this status to ON and update the new time (field name tm) on every time the member opens inside the member area.
3. When click logged out, change the system status to OFF and destroy the session. I have the problem with this part because when click logout, now the status doesnt change to status="off".
4. Then I display who are active at site. Which is where my problem is, because it only shows the 1st user even though I loop them.



here is my code:
----------------------------------------------------------------------
<?php
$session=session_id();
$time=time();
$time_check=$time-600; //SET TIME 10 Minute

//open connection to the database
require_once('inc_conn.php');
// prepare query
$sql="SELECT * FROM active_users_online WHERE session = '".$session."'";
//execute sql statements
$sessions = mysql_query($sql, $connwmis) or die(mysql_error());
//retrieve one row of records
$rows_sessions = mysql_fetch_array($sessions);
//determine the number of records in recordset
$num_rows = mysql_num_rows($sessions);
if($num_rows=="0"){
$time=date("y-m-d");
$sql1="INSERT INTO active_users_online(session, timestamp,username,ip)VALUES('$session', '$time','".$_SESSION['svusername']."','".$_SESSION['svip']."')";
$result1 = mysql_query($sql1, $connwmis) or die(mysql_error());
}
else {
$time=date("y-m-d");
$sql2="UPDATE active_users_online SET timestamp='$time', status='ON' WHERE session = '$session'";
$result2 = mysql_query($sql2, $connwmis) or die(mysql_error());
}



$sql3 = "SELECT * FROM  active_users_online";
$result3 = mysql_query($sql3, $connwmis) or die(mysql_error());

$count_user_online=mysql_num_rows($result3);



// if over 10 minute, delete session
$sql4 = "DELETE FROM active_users_online WHERE timestamp < $time_check";
$result4=mysql_query($sql4, $connwmis) or die(mysql_error());

mysql_close();

// Open multiple browser page for result
?>
------------------------------------------------------------------

Here is the code for Displaying active users at site:
--------------------------------------------------
<?php
//open connection to the database
require_once('inc_conn.php');
// prepare query
$sql="SELECT * FROM active_users_online";
//execute sql statements
$sessions = mysql_query($sql, $connwmis) or die(mysql_error());
//retrieve one row of records
$rows_sessions = mysql_fetch_array($sessions);
//determine the number of records in recordset
$num_rows = mysql_num_rows($sessions);
?>
<?php
require_once('inc_conn.php');
$gap=10; // Gap value can be changed, this is in minutes.
// let us find out the time before 10 minutes of present time. //
$time=date ("Y-m-d H:i:s", mktime (date("H"),date("i")-$gap,date("s"),date("m"),date("d"),date("Y")));
$ut=mysql_query("update active_users_online set status='OFF' where timestamp < '$time'");
$ro = mysql_query("SELECT username,timestamp FROM active_users_online where timestamp > '$time' and status='ON'");
  $active = mysql_num_rows($ro); // sets $active as the number of active users
 
  echo "Logged users: <b>".$rows_sessions['username']."</b><br>"; // lists the username of active users
  echo "Last Logged: <b>".$rows_sessions['timestamp']."</b><br>"; // lists the number of active users

?>
</td>
</tr>
<tr>
<td><?php // Actually lists the users
  do {
$name = $rows_sessions['username']; // Sets $name as the users' names
$i= 0;
// Lists the users names
if ($i != ($num_rows - 1)) { // if $i doesn't equal $active minus 1
  echo "<b>".$name." | ";
}
else {
echo "<b>".$name."</b>";
}

$i++; // increase temp variable
} while ($rows_sessions = mysql_fetch_array($sessions)) ?></td>
</tr>
--------------------------------------------------

Something is wrong, I dont know.
Can somebody help me please????
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.