Also talk about php website online count statistics

Source: Internet
Author: User
Db layer: db_online.PHP this function enables users to be online and checks the online status of other users. function checkOnline ($ userid, $ tempid = null)
{
$ Conn = connect ();

// For all users
// Set yourself to online first
$ Stmt = "UPDATE". DB_NAME. ". user set IsOnline = 'y' WHERE UserID =". $ userid;
$ Result = query ($ stmt, $ conn );
// Info ($ stmt );
// If the current user is a tourist
If ($ tempid! = Null)
{
$ Stmt = "SELECT TempID FROM". DB_NAME. ". TEMPUSER WHERE

TempID = ". $ tempid;
$ Result = query ($ stmt, $ conn );
// Info ($ stmt );
// If the visitor is still 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 is offline
Else
{
$ Stmt = "insert into". DB_NAME. ". TEMPUSER

VALUES ('". $ tempid."', '". getCurrentTime ()."')";
$ Result = query ($ stmt, $ conn );
// Info ($ stmt );
}
}

// View other users
// Common user
$ Stmt = "UPDATE". DB_NAME. ". user set IsOnline = 'n' WHERE". time ()."-

Unix_timestamp (RequestTime)> ". ONLINE_DURATION." AND UserGroupID! = ". GUEST;
$ Result = query ($ stmt, $ conn );

// Visitor
$ Stmt = "delete from". DB_NAME. ". tempuser where". time ()."-

Unix_timestamp (RequestTime)> ". ONLINE_DURATION;
$ Result = query ($ stmt, $ conn );
Disconnect ($ conn );
}

// Obtain the number of online users, including users and visitors
Function getOnlineNumber ()
{
$ Olnum = array ();
$ Conn = connect ();
$ Stmt = "select count (UserID) FROM". DB_NAME. ". user where IsOnline = 'y' AND

UserGroupID! = 4 "; // 4 is the guest user group id
// 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
}



The connect (), disconnect (), query (), and fetch_array () functions are 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 users, registered users". $ online_num ['user']. "People, Tourists". $ online_num ['guest ']. "people ";

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.