Hidden online user count?

Discussion in 'Programming' started by digital_man, Jan 20, 2008.

  1. #1
    Hi guys,
    I have a web site that I would like to have an online user counter. However, I don't want my visitors to see how many users that are in my website. I am already using good analytics but it is not giving me real time data.

    Do you guys know any code that I could put in my html/php pages that can get the job done?

    Thank you in advance for your help and advice.
     
    digital_man, Jan 20, 2008 IP
  2. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Well ok, this is an "oldschool" (php4 :rolleyes:) example that should do the job for you.
    You need a writable textfile or whatever to store some data in.
    <?php 
    // $iActiveTime determines in seconds how long a visitor is considered active
    $iActiveTime = 600;
    
    // Read in IP and time for previous visitors from the textfile visitors.txt
    $sFileHandle = fopen('visitors.txt', 'r'); 
    $iNotActive = 0;
    
    while (!feof($sFileHandle)) {
    	$aActiveip[$iNotActive] = fgets($sFileHandle,255);
    	$iTime[$iNotActive] = fgets($sFileHandle,255);
    	$aActiveip[$iNotActive] = str_replace('\n','',$aActiveip[$iNotActive]);
    	$iTime[$iNotActive] = str_replace('\n','',$iTime[$iNotActive]);
    
    	if (strlen($aActiveip[$iNotActive]) > 3 && strlen($iTime[$iNotActive]) > 3)
    	$iNotActive++;
    }
    fclose($sFileHandle);
    // Write back the IP and time for visitors that has loaded the page within $iActiveTime seconds.
    $sFileHandle = fopen('active.txt', 'w');
    $iNewActive = 1;
    
    for($iNum = 0; $iNum < $iNotActive; $iNum++) {
    
    	if ($iTime[$iNum] > time() - $iActiveTime && strlen(strstr($aActiveip[$iNum], $_SERVER[REMOTE_ADDR])) == 0) {
    		fputs($sFileHandle, "$aActiveip[$iNum]\n$iTime[$iNum]\n");
    		$iNewActive++;
    	}
    }
    
    fputs($sFileHandle,"$_SERVER[REMOTE_ADDR]\n" . time() . "\n");
    fclose($sFileHandle);
    
    // The number of active visitors is accessible from the variable $iNewActive;
    // To print it on page just uncomment the line below
    //echo $iNewActive;
    ?>
    PHP:
    Same thing wrapped as a function
    <?php
    function getActiveVisitors ($p_iActiveTime) {
    
    // Read in IP and time for previous visitors from the textfile visitors.txt
    $sFileHandle = fopen('visitors.txt', 'r');
    $iNotActive = 0;
    
    while (!feof($sFileHandle)) {
    	$aActiveip[$iNotActive] = fgets($sFileHandle,255);
    	$iTime[$iNotActive] = fgets($sFileHandle,255);
    	$aActiveip[$iNotActive] = str_replace('\n','',$aActiveip[$iNotActive]);
    	$iTime[$iNotActive] = str_replace('\n','',$iTime[$iNotActive]);
    
    	if (strlen($aActiveip[$iNotActive]) > 3 && strlen($iTime[$iNotActive]) > 3)
    	$iNotActive++;
    }
    fclose($sFileHandle);
    // Write back the IP and time for visitors that has loaded the page within $p_iActiveTime seconds.
    $sFileHandle = fopen('active.txt', 'w'); 
    $iNewActive = 1;
    
    for($iNum = 0; $iNum < $iNotActive; $iNum++) {
    
    	if ($iTime[$iNum] > time() - $p_iActiveTime && strlen(strstr($aActiveip[$iNum], $_SERVER[REMOTE_ADDR])) == 0) {
    		fputs($sFileHandle, "$aActiveip[$iNum]\n$iTime[$iNum]\n");
    		$iNewActive++;
    	}
    }
    
    fputs($sFileHandle,"$_SERVER[REMOTE_ADDR]\n" . time() . "\n");
    fclose($sFileHandle);
    
    return $iNewActive;
    
    }
    
    // The number of active visitors is accessible from the methodcall getActiveVisitors(timeparameter);
    // 600 is sent as a parameter to the function and determines in seconds how long a visitor is considered active
    // To print it on page just uncomment the line below
    //echo getActiveVisitors(600);
    ?>
    PHP:
    Try it out and see :)
     
    wing, Jan 20, 2008 IP
  3. ravi_9793

    ravi_9793 Banned

    Messages:
    576
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I prefer statconter.
    They give much details about site stats, and have option of visiable and insiable counter.
     
    ravi_9793, Jan 20, 2008 IP
  4. digital_man

    digital_man Member

    Messages:
    56
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #4
    I still have to try the first post. The statcounter looks similar to Google analytics which doesn't give me real time data (I want some thing that can tell me how many users that are in my site in this minute).
     
    digital_man, Jan 20, 2008 IP
  5. digital_man

    digital_man Member

    Messages:
    56
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #5
    Can I add this to all of my pages and use a one text file to get the data? In that way I can print the results in dummy page that only I knows?
    Thanks for your reply


     
    digital_man, Jan 20, 2008 IP
  6. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #6
    Yes you could, but you would not know on which page the user is active.
    This code is very basic and only gives the option to see the number of active users.

    I guess you may want something more flexible and that gives you a little more info :)
     
    wing, Jan 20, 2008 IP