php-n00b Posted December 8, 2009 Share Posted December 8, 2009 What's Up? OK, Now I have the typical IP logging script. <?php $logfile= 'Database.html'; $IP = $_SERVER['REMOTE_ADDR']; $logdetails= date("F j, Y, g:i a") . ': ' . '<a href=http://dnsstuff.com/tools/city.ch?ip='.$_SERVER['REMOTE_ADDR'].'>'.$_SERVER['REMOTE_ADDR'].'</a>'; $fp = fopen($logfile, "a"); fwrite($fp, $logdetails); fwrite($fp, "<br>"); fclose($fp); ?> But I'm expecting lots of visits from the same IP, So instead of adding a new line to the log file for the same IP, Is it possible to over write the last recorded visit, So only the time of that visit is updated? Thanks Bye. Quote Link to comment https://forums.phpfreaks.com/topic/184434-advanced-ip-logging/ Share on other sites More sharing options...
premiso Posted December 8, 2009 Share Posted December 8, 2009 Possible, but would be extremely difficult with a flatfile database. You are better off leaving the log there as the time it would take to open the file find the right IP(s) and overwrite them then save the file, other people could have visited the site and your log file is now out of sync and overwrote other IP logs. If you want that functionality I would suggest going with a Relational Database. Quote Link to comment https://forums.phpfreaks.com/topic/184434-advanced-ip-logging/#findComment-973580 Share on other sites More sharing options...
zeodragonzord Posted December 8, 2009 Share Posted December 8, 2009 premiso is right. Putting it into a database will be better, allowing you to run queries against it and create reports from it easier. One way you could do it is this. Have a table that contains the fields: ip (char), hits (int), last_visit (datetime) When the visitor loads the page, get the IP address and check to see if it's in the table. If it is in the table, update the row containing the IP address by incrementing the count in 'hits' and updating the 'last_visit' with the new datetime. If it is not in the table, create a new row. This only works if you're only concerned with the last visited datetime only. Quote Link to comment https://forums.phpfreaks.com/topic/184434-advanced-ip-logging/#findComment-973587 Share on other sites More sharing options...
Daniel0 Posted December 8, 2009 Share Posted December 8, 2009 Have a table that contains the fields: ip (char), hits (int), last_visit (datetime) IPv4 addresses are unsigned 32-bit integers, so you should store them as unsigned ints, not as chars. Quote Link to comment https://forums.phpfreaks.com/topic/184434-advanced-ip-logging/#findComment-973596 Share on other sites More sharing options...
Philip Posted December 8, 2009 Share Posted December 8, 2009 Have a table that contains the fields: ip (char), hits (int), last_visit (datetime) IPv4 addresses are unsigned 32-bit integers, so you should store them as unsigned ints, not as chars. Note: if you use the ip2long function it will return a signed int, not unsigned. Quote Link to comment https://forums.phpfreaks.com/topic/184434-advanced-ip-logging/#findComment-973618 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.