From: http://www.webwoo.net/PHP/PHPshili/200902/06-31148.html
Include "Class/config. Inc. php"; // include the configuration file
$ Db = new dB;
$ Db-> db_connect (); // connect to the database
$ User = new user; // Initialization
$ Session = new session;
// Start the session
Session_start ();
// Delete expired users in the session table (that is, non-online users). This file is always called to ensure that all users are currently online.
$ Curtime = Time ();
$ Con = "lastactivity <$ curtime ";
$ Session-> Del ($ con );
// Online users must 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"]; // obtain 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 transferred within one hour, the user is considered offline and deleted.
$ Session-> Update ($ userid, $ IPaddress, $ lastactivity );
} Else {
// If you have not logged on, go directly to the logon page.
$ Firstpage = "Logon. php ";
Header ("Location: $ firstpage ");
Exit;
}
Insert ($ userid, $ IPaddress, $ lastactivity) inserts successfully logged on users into the table
Update ($ userid, $ IPaddress, $ lastactivity) updates the last activity time of an online user.
Del ($ con) deletes a qualified user and uses it to clear offline users
Get_from_condition ($ con) returns the set of records that meet the query conditions.
If ($ hiddenfield = "0") {// The 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") {// check whether it has been registered
Session_register ("online"); // register a new variable as the 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 lastactivity.
$ Userid = $ user-> userid;
$ IPaddress = substr ($ remote_addr, 0, 50 );
$ Lastactivity = Time () 3600;
$ Con = "userid = $ userid ";
$ Session-> get_from_condition ($ con );
// Determine whether a session exists. You may log on to a different machine twice.
If ($ session-> counter = 1 ){
$ Session-> Update ($ userid, $ IPaddress, $ lastactivity); // If yes, update
} Else {
$ Session-> insert ($ userid, $ IPaddress, $ lastactivity); // if not, insert
}
// Set cookies on the client
Setcookie ("ccauser", $ username, time () 3600 );
Header ("Location: Test. php"); // guide the test page
}
}
?>
If ($ http_session_vars ["online"] = ") {// you can check whether you have logged on to the server.
?>
// The following is the logon form.
Name:
Password:
} Else {
Echo ":". $ http_cookie_vars ["ccauser"]. "You have logged on"; // if you have logged on, a prompt is displayed.
$ STR ="
Withdraw from the Community ";
Echo $ STR;
}
?>
Include "Global. php"; // include the Global. php file.
$ Strwelcome = "welcome". $ _ session ['ccauser']."
";
Echo $ strwelcome; // display welcome information
$ STR = "Current online user:
==============================
";
$ Con = "1 = 1 ";
// Put forward that all the records in the session table are the current online user and the visitor is not included
$ 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;
?>