Sinisterly
PHP Unique Visitor IP Grabber - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: PHP (https://sinister.li/Forum-PHP)
+--- Thread: PHP Unique Visitor IP Grabber (/Thread-PHP-Unique-Visitor-IP-Grabber)



PHP Unique Visitor IP Grabber - Crime - 10-18-2013

I see there's quite a few IP grabbers, but have yet to come across a "unique visitor" one. Since I'm new to the forums, and thought I'd share something, so here it is.

PHP Code:
<?php $BlockedIPs = Array('Your IP here'); $Unique = 1; $Time = Date("g:i a") . ' on ' . Date("m/d/Y"); $IP = $_SERVER['REMOTE_ADDR']; $Port = $_SERVER['REMOTE_PORT']; $Data = "Received at $Time\nIP Address: $IP\nAccessed through Port $Port\nhttp://www.ip-tracker.org/locator/ip-lookup.php?ip=$IP\n\n"; $Log = 'ip_log.txt'; $Contents = File_Get_Contents($Log); If($Unique == 1){ If(!strpos($Contents, $IP)){ If(!In_Array($IP, $BlockedIPs)){ $File = FOpen($Log, 'a+'); FWrite($File, $Data); FClose($File); } } Else{ Die('An unexpected error has occurred.'); } } ElseIf($Unique == 0){ $File = FOpen($Log, 'a+'); FWrite($File, $Data); FClose($File); } ?>

It's somewhat simple, but it does the trick. I tried to shorten that as best as I could, but hope everyone likes it.

Update:
I've updated the "If(!strpos" statement, as it wasn't functional.


RE: PHP Unique Visitor IP Grabber - The Real Slim Shady - 10-19-2013

Unique visitor counters are not uncommon. And they arent difficult to code.

Also, it would be much more efficient to use an sql database. sqlite even. And i would add the ability to count the number of times a unique vistor, visits.


RE: PHP Unique Visitor IP Grabber - Crime - 10-20-2013

(10-19-2013, 02:11 AM)Geoff Wrote: Unique visitor counters are not uncommon. And they arent difficult to code.

Also, it would be much more efficient to use an sql database. sqlite even. And i would add the ability to count the number of times a unique vistor, visits.

Will do, thank you for the feedback, and tips.


RE: PHP Unique Visitor IP Grabber - Crime - 11-25-2013

(11-17-2013, 02:12 PM)snooper Wrote: They are easy to code, and it would be faster+better if u used sql Smile

Technically not faster, as depending on how your database fills up, the requests could take more time, while the current code just queries a text file, and can pull by IP through the whole list. I've converted my code to a database though.


RE: PHP Unique Visitor IP Grabber - Crime - 11-25-2013

(11-17-2013, 02:12 PM)snooper Wrote: They are easy to code, and it would be faster+better if u used sql Smile

Technically not faster, as depending on how your database fills up, the requests could take more time, while the current code just queries a text file, and can pull by IP through the whole list. I've converted my code to a database though.


RE: PHP Unique Visitor IP Grabber - The Real Slim Shady - 11-26-2013

(11-25-2013, 06:45 PM)Crime Wrote:
(11-17-2013, 02:12 PM)snooper Wrote: They are easy to code, and it would be faster+better if u used sql Smile

Technically not faster, as depending on how your database fills up, the requests could take more time, while the current code just queries a text file, and can pull by IP through the whole list. I've converted my code to a database though.

I would argue that though that depending on the implementation of your file search that could also take longer. Both could be optimized or poorly coded. Also depends on size. On an app of this size you probably arent going to get thousands of IP's in a txt file so SQL would probably have a moderately larger overhead, which is why i suggested sqlite.

Allows for a much more refined storage of information without as much of the server overhead. its like opening a text file except with an easier way of storing more relevant information.


RE: PHP Unique Visitor IP Grabber - The Real Slim Shady - 11-26-2013

(11-25-2013, 06:45 PM)Crime Wrote:
(11-17-2013, 02:12 PM)snooper Wrote: They are easy to code, and it would be faster+better if u used sql Smile

Technically not faster, as depending on how your database fills up, the requests could take more time, while the current code just queries a text file, and can pull by IP through the whole list. I've converted my code to a database though.

I would argue that though that depending on the implementation of your file search that could also take longer. Both could be optimized or poorly coded. Also depends on size. On an app of this size you probably arent going to get thousands of IP's in a txt file so SQL would probably have a moderately larger overhead, which is why i suggested sqlite.

Allows for a much more refined storage of information without as much of the server overhead. its like opening a text file except with an easier way of storing more relevant information.