PHP implementation of online User computing _php tutorial

Source: Internet
Author: User
Include "class/config.inc.php"; Include the configuration file in
$DB = new db;
$db-Db_connect (); Connecting to a database
$user = new User; Initialization
$session = new Session;
Start session
Session_Start ();
Delete a user who has expired in the session table (ie not online) because this file is always called to ensure that the current user is displayed online
$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") {//Here is also available $_session["online"]
$userid = $HTTP _session_vars["userid"]; UserID of the current online user
$ipaddress =substr ($REMOTE _addr,0,50);
$lastactivity =time () +3600; The last active time is updated, if the page is not mobilized within one hours, the user is considered offline and will be deleted.
$session->update ($userid, $ipaddress, $lastactivity);
}else{
If not logged in then go directly to the login page
$firstpage = "logon.php";
Header ("Location: $firstpage");
Exit
}

Insert ($userid, $ipaddress, $lastactivity) inserts a successful user into the table
Update ($userid, $ipaddress, $lastactivity) updates the last active time for online users
Del ($con) Remove the user who satisfies the condition, use it to clear the offline user
Get_from_condition ($con) returns the recordset that satisfies the query criteria

if ($hiddenField = = "0") {//test form has not been submitted
$con = "Username= ' $username ' and userpwd= ' $userpwd '";
$result = $user->get_from_condition ($con);
if ($user->counter==1) {
if (!session_is_registered ("online")) {//detection is registered
Session_register ("online"); Register a new variable for the session variable
}
if (!session_is_registered ("Ccauser")) {
Session_register ("Ccauser");
}
if (!session_is_registered ("userid")) {
Session_register ("userid");
}
$ccauser = $username; Assigning a value to a session variable
$online = "on"; This variable is used in global.php to update the last active time lastactivity
$userid = $user->userid;
$ipaddress =substr ($REMOTE _addr,0,50);
$lastactivity =time () +3600;
$con = "Userid= $userid";
$session->get_from_condition ($con);
To determine if a session exists, it is possible that you can log on to a different machine two times.
if ($session->counter==1) {
$session->update ($userid, $ipaddress, $lastactivity); If present, update
}else{
$session->insert ($userid, $ipaddress, $lastactivity); If not present, insert
}
Setting cookies on the client
Setcookie ("Ccauser", $username, Time () +3600);
Header ("Location:test.php");//Then guide the test page
}
}
?>
if ($HTTP _session_vars["online"]== "") {//determine if logged in
?>
The following is a signed-in form

Name:
Password:


}else{
echo "Netizen:". $HTTP _cookie_vars["Ccauser"]. " You are already logged in. "; Show a prompt if you are logged in
$str = "

Exit the community ";
Echo $str;
}
?>

Include "global.php"; Include the global.php file in
$strWelcome = "Welcome". $_session[' Ccauser ']. "
";
Echo $strWelcome; Show welcome Message
$str = "Current Online User:
===================
”;
$con = "1=1";
All the records in the session table are the current online users, not counting the visitors.
$result = $session->get_from_condition ($con);
while ($row =mysql_fetch_array ($result)) {
$con 1= "userid= $row [UserID]";
$user->get_from_condition ($con 1);
$str. = $user->username. " ";
}
Echo $str;
?>

Exit the Community

http://www.bkjia.com/PHPjc/630494.html www.bkjia.com true http://www.bkjia.com/PHPjc/630494.html techarticle Include class/config.inc.php;//include the configuration file in $db = new db; $db-db_connect ();//Connect database $user = new user;//Initialize $session = New session; Start Session ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.