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.
Well ok, this is an "oldschool" (php4 ) 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
I prefer statconter. They give much details about site stats, and have option of visiable and insiable counter.
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).
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
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