PDA

View Full Version : Need PHP shows Unique hits and how many online [rep]



Swearwolf
22-04-2007, 01:02 AM
+rep to anyone who helps.

Mentor
22-04-2007, 01:09 AM
A flat file one i wrote quite a while ago:
Shows users on line + users today.

to keep a record of truely unique hits would require keeping a record of every ip thats accessed your site in how ever long your checking for, aka unquie hits a month or what ever.
So doing so could be achaived just by creating a redicusly long day length (say week long) or changeing the script slighly so it never removes old enterys.

o.0

<?php

################################################## ################
# Thybag Users Online Now and to day script #
################################################## ################
# Copyright Thybag.co.uk 2004 - 2005 #
################################################## ################
# This Script is FREE to use as long as this header remains #
# In tact. And realy since no one is ever going to see it is #
# there any point in removing it? #
################################################## ################

// Configuration
$timeoutseconds = 6000; // length of time user is considered online in seconds
$daylength = 86400; // lengh of day in seconds
$filename = "online.txt" ; // the file in wich to store information, chmod it to 777


//-----------------------------------------------------------------------------------------
# Unless you know what your doing its not advisable to edit beond this point
//-----------------------------------------------------------------------------------------

// Main Variables
$timestamp=time();
$timeout=$timestamp-$timeoutseconds;
$aday = $timestamp-$daylength;
$ip = getenv("REMOTE_ADDR");

// Run The Array Loops
$hits = file($filename);
foreach($hits as $Key => $Val)
{
$hittedinfo[$Key] = explode("|", $Val); //explode that data into a new array:
}
//Loop all me info
for($K = 0; $K < sizeof($hits); $K++) {

//Put any ips that arnt over a day old in to a string so they remain in the database
if($hittedinfo[$K][0] > $aday )
{
$hitstring .= $hittedinfo[$K][0]."|".$hittedinfo[$K][1]."";
}

$usedip[$K] = trim($hittedinfo[$K][1]); //Make an array of Ips in database

//Make array of all online users
if($hittedinfo[$K][0] > $timeout)
{
$onlinenow[$K] = "1";
}


// end the loop
}


$toenter ="";

// Dont dubble up
$usedip = array_unique($usedip) ;

//if the ip has already been counted dont add it to the database
if(in_array(trim($ip), $usedip))
{$toenter = $timestamp."|".$ip."\n" ;} //if it aint, write er in
else // its there, is it to old or shall we still use
{$K2 = array_search($ip, $usedip);
if($hittedinfo[$K2][0] < $timeout)
{$toenter = $timestamp."|".$ip."\n" ;}


}

//Write all the data we want to keep back to the file
$fp = fopen($filename , "w+");
fputs($fp , $hitstring."".$toenter);
fclose($fp);

//Now where done, set the output varibles with the numbers the need
$hitztoday = sizeof($usedip);
$usersonline = sizeof($onlinenow);

/*-----------------------------------------------------------------------------------------
$hitztoday - now contains the amount of unque hits for today
$usersonline - now contains the amount of users online

Include this file in to where ever you wish the stats varibales to be useable, make sure
you only include once per page to prevent errors.

Create and chmod to 777 a txt file called online or whatever you renamed the $filename vaible
to use, put in same directory as page this is being included into, unless you ad folder info
to the url.

To make this information appear on the page simply use the echo function
EG:
echo "Users Online: ";
echo $usersonline;


If you require any more assitance with the script feel free to ask at
<a href='http://thybag.co.uk/forum/' target='_blank'>http://thybag.co.uk/forum/</a> or email me at [email protected]
-----------------------------------------------------------------------------------------*/


// auto output vairibles
//echo "Today:".$hitztoday."<br>Online:".$usersonline;
?>

Swearwolf
22-04-2007, 01:57 AM
tyvm dude, had to change a bit cos the users online displayed todays hits and vice versa lool +rep

Want to hide these adverts? Register an account for free!