Statistics online number/user list

Source: Internet
Author: User
Tags mysql host

Save the following code as a online_list.php (focus on modifying the necessary parameters before saving)

$host = "localhost";//mysql host
$user = "root";//mysql's user name
$PW = "";//mysql's password
$db = "";//mysql database name
$update _time= "30";//Update frequency---Number of seconds
Session_Start ();

if (!$_session[' name ']) session_register ("name");

$con =mysql_connect ($host, $user, $PW) or Die ("Connection Database Failed");

$now =time ();

mysql_select_db ($db, $con) or Die ("wrong database $db");

if (!empty ($name)) {//If the user has logged in

$query =mysql_query ("SELECT count (username) from online_list where username= ' $name '", $con);//query online to table already has a user's name

$result =mysql_result ($query, 0);//Results of Query

if ($result!=0) {//If the user is already in the online list

@mysql_query ("Update online_list set second_time= ' $now ' where username= ' $name ', $con);//Update last online time of user

}else{//If the user is not on the online list

@mysql_query (INSERT into online_list (username,userip,online_time,second_time) VALUES (' $name ', ' $REMOTE _addr ', ' $ Now ', ' $now ') ', $con);//Add a user to the online list

}

}


else{//If the user does not log in

$query =mysql_query ("SELECT count (Userip) from online_list where userip= ' $REMOTE _addr '", $con);//query online to table already has a visitor's IP address

$result =mysql_result ($query, 0);//Results of Query

if ($result!=0) {//If the IP address of the visitor is already in the online list

@mysql_query ("Update online_list set second_time= ' $now ' where userip= ' $REMOTE _addr '", $con);//Update the last online time of the visitor

}else{//If the user list does not have a visitor's IP address

@mysql_query (INSERT into online_list (username,userip,online_time,second_time) VALUES (' Visitors ', ' $REMOTE _addr ', ' $now ', ' $now ') ", $con);/Add a visitor to the online list

}

}

$del _time= $now-$update _time;

@mysql_query ("Delete from online_list where second_time< $del _time", $con);//delete users who do not have actions in $update_time seconds

?>

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.