Php: number of online users-PHP Tutorial

Source: Internet
Author: User
Tags flock
Php enables the current number of online users. Principle: calculate the number of online users based on different IP addresses. Implementation Method: you can use a database or text. Here I use text implementation .? Php *** @ Date2010.04.07 principle: calculate the number of online users based on different IP addresses.
Implementation Method: you can use a database or text.
Here I use text implementation.
/**
* @ Date 2010.04.07
* @ Author Huaxia star bkJia.com
* @ Blog http://hi.baidu.com/woaidelphi/blog
*/
$ User_online = "count. php"; // The number of objects that are stored
Touch ($ user_online); // If this file is not available, create
$ Timeout = 30; // The author is not moved within 30 seconds.
$ User_arr = file_get_contents ($ user_online );
$ User_arr = explode (#, rtrim ($ user_arr, #); print_r ($ user_arr );
$ Temp = array ();
Foreach ($ user_arr as $ value ){
$ User = explode (",", trim ($ value ));
If ($ user [0]! = Getenv (REMOTE_ADDR) & ($ user [1]> time () {// if it is not the current user's IP address and does not time out, it will be placed in the array
Array_push ($ temp, $ user [0]. ",". $ user [1]);
}
}
Array_push ($ temp, getenv (REMOTE_ADDR). ",". (time () + ($ timeout). #); // save user information
$ User_arr = implode ("#", $ temp );
// Write a file
$ Fp = fopen ($ user_online, "w ");
Flock ($ fp, LOCK_EX); // flock () cannot work normally in NFS or some other network file systems
Fputs ($ fp, $ user_arr );
Flock ($ fp, LOCK_UN );
Fclose ($ fp );
Echo "currently". count ($ temp). "online ";
?>

Bytes. Implementation Method: you can use a database or text. Here I use text implementation. ? Php/*** @ Date 2010.04.07...

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.