Home  >  Article  >  Backend Development  >  Two ways to count the number of people currently online_PHP Tutorial

Two ways to count the number of people currently online_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 14:52:12906browse

The first method: use the AltaVista search engine to achieve
This method is to check the number of links to your site. This is achieved using the AltaVista search engine. Here, we use the following search engine:
http://www.altavista.com/cgi-bin/query?kl=XX&pg=q&text=yes&q=link:&search=Search.
This is a text version of AltaVista. This saves us a lot of code that needs to parse HTML. Next, we use rawurlencode() to process our exact URL so Altavista can process it correctly. The processing is as follows:
$url = "http://www.oso.com.cn";
$url_encoded = rawurlencode($url);
$url_to_check = "http://www.altavista.com/cgi-bin/query?
kl=XX&pg=q&text=yes&q=link:$url_encoded&search=Search";
In this way, we can retrieve the URL through the file() function.
$num_searched = file($url_to_check);
Now the files we have retrieved have been stored in the array $num_searched. Now to find the text we want in the array "About (.*)
pages found. ".(.*) means in anything. Also, if no one links to our URL, AltaVista will display "AltaVista found
no document matching your query.". Because we want to know how many people are linking to our URL, that text will be read
Make 0 personal links.
$url = "http://www.oso.com.cn";
$url_encoded = rawurlencode($url);
$url_to_check =
"http://www.altavista.com/cgi-bin/query?kl=XX&pg=q&text=yes&q=link:$url_encoded&search=Search";
$num_searched = file($url_to_check);
for ($i = 0; $i < count($num_searched); $i ) {
if(eregi( "About (.*) pages found.", $num_searched[$i])){
$total_links = eregi_replace( "
About (.*) pages found.", "1", $num_searched[$i]);
}
elseif(eregi( "AltaVista found no document matching your query.",$num_searched[$i])){
$total_links = "0";
}
}
In this way, we can get our search results through the print statement:
print("$total_links people are linking to $url");

The second method: using MYSQL database. The following is an article I reposted from a PHP Chinese user. The method is to use a temporary data table to process the current connection. The specific content is as follows:
*************************************************** *************
First, use MySQL tools to create a table:
CREATE TABLE ccol(
id integer not null auto_increment, #recorded ID
ip char(15) not null, #Visitor’s IP address
dtstamp datetime not null, #last access time
uri char(255), #URI requested by the visitor
primary key (id)
);

Then, write a piece of PHP code:
/*
File: ccol.php - ConCurrent OnLine statistics
Purpose: To count the number of people browsing online at the same time
Author:Hunte, hunte@phpuser.com
Modified:2000-4-25
*/
$duration=1800;
require "db.php";
//Contains DBSQL, please refer to my other article for details
$ccol=new dbSQL;
$ccol->connect();
$ccol->query("DELETE FROM ccol WHERE
(UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(dtstamp))>$duration");
//Delete records older than half an hour
$ccol->query("SELECT * FROM ccol WHERE ip="$REMOTE_ADDR"");
//Determine whether the current IP exists in the table
if ($ccol->nf()) {
$ccol->next_record();//Move down the pointer of the found record array
$id=$ccol->f("id");
$ccol->query("UPDATE ccol SET dtstamp=now(), uri="$REQUEST_URI" WHERE id=$id");
//Set the last access time and visited page
} else{
$ccol->query("INSERT INTO ccol VALUES (0, "$REMOTE_ADDR", now(), "$REQUEST_URI")");
}

$ccol->query("SELECT COUNT(*) AS ccol FROM ccol WHERE
(UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(dtstamp))<=$duration");
//Find the records within half an hour, the following WHERE clause is optional - the ones that exceed the time have been deleted
$ccol->next_record()
echo "Number of people online:", $ccol->f("ccol");
$ccol->free_result();

How to use it? Call this program on every page of the site.
Of course, there is room for improvement in this code. For example, it is unnecessary and will reduce efficiency to delete records half an hour ago every time it is called.
You can try to do it after a longer period of time, such as 6 hours. You can think about it for yourselves, I won’t go into details.
As long as this method is slightly modified, it can be used for other purposes, such as SESSION management, website visit statistics analysis, etc.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/371591.htmlTechArticleThe first method: Use the AltaVista search engine to achieve this method is to check the number of links to your site. This is achieved using the AltaVista search engine. Here, we search using the following...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn