PHP website training also talk about PHP website online population statistics

Source: Internet
Author: User
Tags php website
function Checkonline ($userid, $tempid =null)
{
$conn = connect ();
For all users
Set yourself as online first
$stmt = "UPDATE". Db_name. ". USER SET is
$result = Query ($stmt, $conn);
info ($stmt);
If the current user is a visitor
if ($tempid! = null)
{
$stmt = "Select Tempid from". Db_name. ". Tempuser WHERE
Tempid= ". $tempid;
$result = Query ($stmt, $conn);
info ($stmt);
If the visitor is also online
if ($row = Fetch_array ($result))
{
$stmt = "UPDATE". Db_name. ". Tempuser SET
Requesttime= ' ". GetCurrentTime ()." ' WHERE tempid= ". $tempid;
$result = Query ($stmt, $conn);
info ($stmt);
}
The visitor has been offline
Else
{
$stmt = "INSERT into". Db_name. ". Tempuser
VALUES (' ". $tempid." ', ' ". GetCurrentTime ()." ') ";
$result = Query ($stmt, $conn);
info ($stmt);
}
}
View other users
Normal User
$stmt = "UPDATE". Db_name. ". USER SET is
Unix_timestamp (RequestTime) > ". Online_duration. " and usergroupid! = ". GUEST;
$result = Query ($stmt, $conn);
Tourists
$stmt = "DELETE from". Db_name. ". Tempuser WHERE ". Time ()."-
Unix_timestamp (RequestTime) > ". Online_duration;
$result = Query ($stmt, $conn);
Disconnect ($conn);
}
Get online number of users and visitors
function Getonlinenumber ()
{
$olnum = Array ();
$conn = connect ();
$stmt = "Select COUNT (UserID) from". Db_name. ". USER WHERE is
Usergroupid! = 4 "; 4 user group ID for guest
info ($stmt);
$result = Query ($stmt, $conn);
$olnum [' user '] = result ($result, 0, "COUNT (UserID)");
$stmt = "Select COUNT (Tempid) from". Db_name. ". Tempuser ";
info ($stmt);
$result = Query ($stmt, $conn);
if ($row = Fetch_array ($result))
{
$olnum [' guest '] = $row [' COUNT (Tempid) '];
}
Disconnect ($conn);
return $olnum; From www.w3sky.com
}

where connect (), disconnect (), query (), Fetch_array () function in Dbmanager.inc.PHP
Dbmanager.inc.PHP
Define ("Db_name", "databasename");
Define ("Db_user", "USER");
Define ("Db_pass", "PASS");
Define ("Db_host", "localhost");
Function Connect ()
{
echo "Connecting to Host:". HOST. "
";
$conn = mysql_connect (Db_host,db_user,db_pass);
mysql_select_db (db_name);
/*
if ($conn)
{
echo "Connect to database sucessfully. Connection ID: ". $conn."
";
}
Else
{
echo "Connect to database failed.
";
}
*/
return $conn;
}
function Pconnect ()
{
return Mysql_pconnect (Db_host,db_user,db_pass);
}
function Disconnect ($conn)
{
$close = Mysql_close ($conn);
/*
if ($close)
echo "MySQL Database disconnected.
";
Else
echo "MySQL Database disconnecting failed. Please try again.
";
*/
}
function query ($stmt, $conn)
{
Return mysql_query ($stmt, $conn); From www.w3sky.com
}
function Fetch_array ($result)
{
return mysql_fetch_array ($result);
}
function Fetch_row ($result)
{
Return mysql_fetch_row ($result);
}
function Num_rows ($result)
{
Return mysql_num_rows ($result);
}
function result ($result, $row, $field)
{
Return mysql_result ($result, $row, $field);
}

Rule layer:
Rl_online. Php
function Getonline ()
{
if ($userid = = 2)
{
if (session_is_registered ("Tempuserid"))
{
Checkonline ($userid, $tempuserid);
}
}
Else
{
Checkonline ($userid);
}
return Getonlinenumber ();
}
UI layer:
Ui_online. Php
$online _num = Getonline ();
echo "Online number, registered user". $online _num[' user '. People, tourists ". $online _num[' guest ']." People ";

The above on the introduction of the PHP website training also talk about the PHP site online statistics, including the content of the PHP Web site training, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.