Home > Article > Backend Development > PHP implements online user calculation_PHP tutorial
include "class/config.inc.php"; //Include the configuration file
$db = new db;
$db-> db_connect( ); //Connect to database
$user = new user; //Initialization
$session = new session;
//Start session
session_start();
//Delete expired users (i.e. non-online users) in the session table because this file is always called to ensure that all currently online users are displayed
$curtime=time();
$con="lastactivity<$curtime";
$session->del($con);
//Online users need to constantly update the lastactivity time in the session table and reset the user's COOKIES
if ($HTTP_SESSION_VARS["online"]=="on"){ //$_SESSION["online"]
is also available here
$userid=$HTTP_SESSION_VARS["userid"]; //Get the userid of the current online user
$ipaddress=substr($REMOTE_ADDR,0,50);
$lastactivity=time()+3600; //Update the last activity time. If the page is not moved within an hour, the user will be considered offline and will be deleted.
$session->update($userid,$ipaddress,$lastactivity);
}else{
//If you are not logged in, go directly to the login page
$firstpage="logon.php";
header ("Location: $firstpage");
exit;
}
insert($userid,$ipaddress,$lastactivity) inserts successfully logged-in users into the table
update($userid,$ipaddress,$lastactivity) updates the last activity time of online users
del($con) deletes users who meet the conditions and use it to clear offline users
get_from_condition($con) returns the record set that meets the query conditions
if ($hiddenField=="0"){ //Test whether the form has been submitted
$con="username='$username' and userpwd='$userpwd'";
$result=$user->get_from_condition($con);
if ($user->counter==1){
if (!session_is_registered("online")){//Check whether it has been registered
session_register("online"); //Register a new variable as a session variable
}
if (!session_is_registered("ccauser")){
session_register("ccauser");
}
if (!session_is_registered("userid")){
session_register("userid");
}
$ccauser=$username; //Assign a value to the session variable
$online="on"; //This variable is used in global.php to update the last activity time lastactivity
$userid=$user->userid;
$ipaddress=substr($REMOTE_ADDR,0,50);
$lastactivity=time()+3600;
$con=" userid=$userid";
$session->get_from_condition($con);
//Determine whether the session exists. It is possible that you logged in twice on different machines.
if ($session->counter==1){
$session->update($userid,$ipaddress,$lastactivity); //If exists, update
}else{
$session->insert($userid,$ipaddress,$lastactivity); //If it does not exist, insert
}
//Set COOKIES on the client
SetCookie("ccauser",$username,time()+3600);
Header("Location:test.php");//Then lead to the test page
}
}
?>
if($HTTP_SESSION_VARS["online"]=="") { //Determine whether you are logged in
?>
//The following is the login form
Name:
Password:
}else{
echo "Netizen:".$HTTP_COOKIE_VARS["ccauser"]."You are already logged in"; //If you are logged in, display the prompt
$str="
Exit the community";
echo $str;
}
?>
include "global.php"; //Include the global.php file
$strWelcome="Welcome".$_SESSION['ccauser']."
";
echo $strWelcome; //Display welcome message
$str=” Current online user:
===================
”;
$con=" 1=1";
//Propose that all records in the session table are the current online users, not counting tourists
$result=$session->get_from_condition($con);
while($row=mysql_fetch_array($result)){
$con1="userid=$row[userid]";
$user->get_from_condition($con1);
$str.=$user->username." ";
}
echo $str;
?>
Exit the community