Re: tracking IP's [message #182068 is a reply to message #182065] |
Tue, 02 July 2013 08:55 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 02/07/13 01:37, richard wrote:
> I want a simple script that will keep track of IP's in a mysql table.
>
> This script works. But puts the IP in a text file.
> It's ok, but doesn't seem to detect any other IP but mine.
> I know this because my hit counter shows there has been new visitors.
>
> $ipaddress = $_SERVER['REMOTE_ADDR'];
> $date = date ("M dS H:i:s");
>
> $message = "$page _ $ipaddress _ $date\n";
>
> $File = "track.txt";
> $Open = fopen($File, "a+");
> if ($Open){
>
> fwrite($Open, "$message");
> fclose ($Open);
> }
>
>
> I have been looking around for a script but can't seem to find one that
> works the way I want.
> anyone have such a script online somewhere?
What I have here is that a common set of libraries included in all main
pages do the following
$time=date("Y-m-d H:i:s");
$ipaddr=$_SERVER['REMOTE_ADDR'];
update_db($ipaddr,$time);
function update_db($ipaddr,$time)
{
$result=mysql_query("select id, hits from ipdata where
ipaddr='".$ipaddr."'");
if($result && mysql_num_rows($result) >0) //update existing
{
$hits=mysql_result($result, 0, 'hits');
$id=mysql_result($result,0,'id');
mysql_query(sprintf("update ipdata set
hits='%d',last_access='%s' where id='%d'",$hits+1,$time,$id));
}
else // insert record into table
{
$whois=whois_extract($ipaddr);
mysql_query(sprintf("insert into ipdata set
ipaddr='%s',last_access='%s',hits='1', whois='%s'",
$ipaddr,$time,$whois));
}
}
Its old code and I wouldn't write it again this way, but it more or less
works. Two rapid hits from the same IP address result in two inserts
sadly - should have done an update or insert sql statement.
Also should use Mysqli lib instead of mysql.
The whois lookup is massively vile code and I wouldnt do that again.
Better to have a table of all the ip domains there are in the world and
link to that..:-)
--
Ineptocracy
(in-ep-toc’-ra-cy) – a system of government where the least capable to lead are elected by the least capable of producing, and where the members of society least likely to sustain themselves or succeed, are rewarded with goods and services paid for by the confiscated wealth of a diminishing number of producers.
|
|
|