Get Visitor Info Using Image Based Hit Counter
What's the best way to create hit counter using php, which can be embedded to any external site/forum using simple image. e.g. <img src="ht开发者_开发百科tp://site.com/counter.gif"></img>
and save visitors info like IP address and http referrer to MySQL database?
Can't find any info about this... is that even possible to do this?
Thanks for help.
You could have a look at AWSTATS http://awstats.sourceforge.net/
I was hoping there was a simple way to connect to google analytics data and then generate the counter graphics with imagemagic or similar. They did have some 3rd party apps for that - but in my opinion they where to much for a simple counter - or just plain ugly.
AWStats will parse the logfiles, which could show you stats in the past also.
You could also create a very small code to save IP addresses into a visitors table, then check against that table when counting unique visitors. And could use a cronjob to update counter-image a few times a day (update on every visit probably not needed).
Getting IP:
function checkIP()
{
if ( getenv( 'HTTP_CLIENT_IP' ) )
{
$ip = getenv( 'HTTP_CLIENT_IP' );
}
elseif( getenv( 'HTTP_X_FORWARDED_FOR' ) )
{
$ip = getenv( 'HTTP_X_FORWARDED_FOR' );
}
elseif( getenv( 'HTTP_X_FORWARDED' ) )
{
$ip = getenv( 'HTTP_X_FORWARDED' );
}
elseif( getenv( 'HTTP_FORWARDED_FOR' ) )
{
$ip = getenv( 'HTTP_FORWARDED_FOR' );
}
elseif( getenv( 'HTTP_FORWARDED' ) )
{
$ip = getenv( 'HTTP_FORWARDED' );
}
else
{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
For other visitor info, check out the $_SERVER options and others..
http://www.php.net/manual/en/reserved.variables.server.php
HTTP_USER_AGENT
HTTP_ACCEPT_LANGUAGE
HTTP_REFERER
REQUEST_URI
Also, if there will be a large number of sites that embed the counter, you could just grab the count itself. (and each site using it can display it with some css etc..).
精彩评论