PHP using session and file statistics online number of _php tutorials

Source: Internet
Author: User
Tags flock php online

PHP uses session and file to count the number of people online,


<?php/* @ PHP Online Population Statistics Program Copyright (c) by Pader 1:25 January 7, 2009 What is the use of It:note: the general independent online Statistics program is the number of IP numbers that are counted online, and this is not accurate such as LAN Visitors, such as companies, school rooms and internet cafes, although the network IP is different, but the external IP is the same if the same LAN, no matter how many people visit your site is only considered a person this small program solves this problem, it is computer-based, each computer is counted as a visitor Of course, because of the use of cookies, if you use two different core browser access on the same computer, then don't be conceivable. */$filename = ' online.txt '; Data file, if no new $cookiename = ' vgotcn_onlinecount '; Cookie name $onlinetime = 600; Online effective time, in seconds (i.e. 600 equals 10 minutes) @ $online = file ($filename);  Data file, if no new $nowtime = time ();  $nowonline = Array (); /* Get still valid data */if (!empty ($online)) {foreach ($online as $line) {$row = explode (' | ', $line); $sesstime = Trim ($row [1]); F (($nowtime-$sesstime) <= $onlinetime) {//If the data continues to persist within the time of validity, otherwise it will not be counted $nowonline [$row [0]] = $sesstime;//Get online list to array , the session ID is the key name, the last communication time is the key value}}}//@ Create a Visitor communication state using cookie communication cookie will expire when the browser is closed, but if you do not close the browser, this cookie will remain in effect until the online time of the program setting expires */if (Isset ($_ cookie[$cookiename])) {//If there is a cookie that is not first accessed then do not add the number of people and update the communication time $uid = $_cookie[$cookiename];} else {//If no cookie is the first access $vid = 0 ; Initialize Visitor ID do {//Give the user a new ID $vid + +; $uid = ' U '. $vid; } while (Array_key_exists ($uid, $nowonline)); Setcookie ($cookiename, $uid); } $nowonline [$uid] = $nowtime;  Update current time status//@ Stats now online number */$total _online = count ($nowonline); /* Write Data */if ($fp = @fopen ($filename, ' W ')) {if (Flock ($FP, lock_ex)) {rewind ($fp); foreach ($nowonline as $fuid + = $ft IME) {$fline = $fuid. ' | '. $ftime. " \ n ";  @fputs ($fp, $fline); } flock ($FP, lock_un);  Fclose ($FP);

http://www.bkjia.com/PHPjc/1026920.html www.bkjia.com true http://www.bkjia.com/PHPjc/1026920.html techarticle PHP Use session and file statistics online number of people, PHP/* @ PHP Online Statistics program Copyright (c) by Pader 1:25 January 7, 2009 How to use It:script src= "ONLINE.P HP "/script Note: ...

  • 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.