Two ways to count the number of online users: PHP Tutorial-php Tutorial

Source: Internet
Author: User
Two methods are used to count the number of online users. Method 1: Use the AltaVista search engine to check the number of links to your site. Here we use the AltaVista search engine. Here, we use the following method to search: Use the AltaVista search engine to implement
This method checks the number of links to your site. Here we use 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 HTML code to be parsed. Next, we use rawurlencode () to process our exact URL and ensure that Altavista can process it correctly. The process 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 use the file () function to retrieve the URL.
$ Num_searched = file ($ url_to_check );
The retrieved files are now stored in the array $ num_searched. Now we need to find the desired text "About (. *) in the array (.*)
Pages found .". (. *) Indicates anything. In addition, 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, the text will be viewed
For 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 print the statement to get our search results:
Print ("$ total_links people are linking to $ url ");

Method 2: Use the MYSQL database. The following is an article on my post, from a Chinese PHP user. the starting method is to use a temporary data table to process the current connection. the specific content is as follows:
**************************************** ***********************
First, use the MySQL tool to create a table:
Create table ccol (
Id integer not null auto_increment, # Record ID
Ip char (15) not null, # visitor's ip address
Dtstamp datetime not null, # Last access time
Uri char (255), # the visitor's requested URI
Primary key (id)
);

Then, write a piece of PHP code:
/*
File: ccol. php-ConCurrent OnLine statistics
Objective: to count the number of concurrent online users
Author: Hunte, hunte@phpuser.com
Modification: 2000-4-25
*/
$ Duration= 1800;
Require "db. php ";
// Contains DBSQL. For more information, see my other article.
$ Ccol = new dbSQL;
$ Ccol-> connect ();
$ Ccol-> query ("delete from ccol WHERE
(UNIX_TIMESTAMP (NOW ()-UNIX_TIMESTAMP (dtstamp)> $ duration ");
// Delete records that have exceeded half an hour
$ Ccol-> query ("SELECT * FROM ccol WHERE ip =" $ REMOTE_ADDR "");
// Determine whether the current IP address exists in the table
If ($ ccol-> nf ()){
$ Ccol-> next_record (); // move down the pointer to the record array found
$ Id = $ ccol-> f ("id ");
$ Ccol-> query ("UPDATE ccol SET dtstamp = now (), uri =" $ REQUEST_URI "WHERE id = $ id ");
// Set the last access time and 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 record in the last 30 minutes. The WHERE clause can be left blank. the record that exceeds the specified time has been deleted.
$ Ccol-> next_record ()
Echo "online users:", $ ccol-> f ("ccol ");
$ Ccol-> free_result ();

How to use it? Call this program on each page of the site.
Of course, there is still room for improvement in this code. For example, if you delete a record half an hour before each call, this is unnecessary and will reduce the efficiency.
What can I do after a longer period of time, for example, 6 hours. Let's just think about it. I will not talk about it.
This method can be used as long as it is slightly modified, such as SESSION management and website access statistical analysis.

Refer this method is to check the number of links to your site. Here we use the AltaVista search engine. Here, we use the following search...

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.