PHP handling online users
I often saw a friend asking online users on the PHP forum of 9CBS. I wrote similar code, I took it out to offer ugly, there is a little garbage, basically realize the function, it can be extended, such as the same time An account is online, etc.
/ * Purpose of Program: detection program of online users: heiyeluren Writing Time: 2004/6 * /
session_cache_limiter ("Private, Max-Age = 10800");?> @Session_Start ();?> include ("function.php");?> incrude ("conndb.php"); ?> incrude ("const.php");?>
PHP // online user processing
IF (isset ($ user_name)) // If the session writes, then the user has already logged in {$ result = mysql_query ("SELECT * from aliang_online where onlineuser = '$ user_name'") or Die ($ DIE_MSG. " Inline list "); // Query to see if the user has written online list $ num = mysql_num_rows ($ result);
If ($ NUM! = 0) // If you have written online list, update the last online time and status {// $ now_TIME is a customized constant, which represents the timestamp of the current time, which constant is in const.php Set mysql_query ("Update aliang_online set lasttime = $ now_time, userState = 1 where onlineuser = '$ user_name'") or Die ($ DIE_MSG. "If you write, then update");} else // If the user is not in the online list, then put the users write a list of online users {mysql_query ( "INSERT INTO aliang_online SET OnlineUser = '$ user_name', LoginIP = '$ REMOTE_ADDR', LoginTime = $ now_time, lastTime = $ now_time, UserState = 1" Al Die ($ DIE_MSG. "If you don't write, then write");}} else // If the user does not log in, or the visitor, use the IP address to locate whether it is already online list {$ results = mysql_Query "Select * from aliang_online where loginip = '$ remote_addr'") or Die ($ DIE_MSG. "If you are a visit, do you have any IP"); $ num = mysql_num_rows ($ result); if ($ Num! = 0) // If the IP address of the visitor is already in the list, then update the guest last time and status {mysql_query ("Update aliang_online set lasttime = $ now_time, userstate = 1 where loginip = '$ remote_addr') or Die $ DIE_MSG. "If there is an IP, update his time");} Else // If the Visitor's IP address is not in the list, then write a list {mysql_query ("Insert INTO Aliang_Online Set Onlineuser =", loginip = '$ Transote_addr', loginti ME = $ now_TIME, LastTime = $ now_TIME, USERSTATE = 1 ") OR DIE ($ DIE_MSG." If there is no IP. Write ");}}?>
PHP // Delete users with no action for a while
// Delete an online user time is now minus update time, $ update_time is a constant, set $ del_time = ($ now_TIME - $ update_time) from const.php;
// Remove the Visitor Mysql_Query ("delete from aliang_online = 'visitor') or DIE ($ DIE_MSG." Deletes the user who did not move for a period of time ($ DIE_MSG. ") Or DIE ($ DIE_MSG.") Or Die ($ DIE_MSG. ") Or DIE ($ DIE_MSG.")
/ / Update a member of the member without action is 0Mysql_Query ("Update Aliang_Online Set UserState = 0 WHERE LastTime <$ del_time and onlineuser! = 'Visitor') OR DIE ($ DIE_MSG." Update the online status of the registered member is 0 "); // The following code is used to deal with members' online time $ SQL =" Select * from aliang_online where userstate = 0 and onlineuser! = 'Visitor' "; $ result = mysql_query ($ SQL) OR DIE ($ DIE_MSG." "Select all members");
For ($ I = 0; $ I $ row = mysql_fetch_object ($ result) // reads out the information or Die that has been offline users ($ DIE_MSG. "reads out the user's information"); $ Result1 = mysql_Query ("Select Fulttime From Aliang_User WHERE User") or Die ($ DIE_MSG. "Reads the user's previous online time"); $ row1 = mysql_fetch_object ($ results1); $ Full_time = $ row1-> FullTime; // read all previous online users corresponding to time $ result2 = mysql_query ( "SELECT LoginTime, LastTime FROM aliang_online WHERE OnlineUser = '$ row-> OnlineUser'") or die ($ die_msg) $ Row2 = mysql_fetch_Object ($ results2); $ result_time = counttime ($ row2-> logintime, $ row2-> lasttime); // Calculate the user's time now $ Full_time = $ full_time $ result_time; // calculate all time online mysql_query ( "UPDATE aliang_user SET FullTime = $ full_time WHERE UserName = '$ row-> OnlineUser'") or die ($ die_msg); // put the All online time writing, such as a database MySQL_QUERY ("Delete from aliang_online where onlineuser = '$ row-> onlineuser') or DIE ($ DIE_MSG); // After getting the data, delete the user } ?>