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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
