PHP script to Sort a text file of IP addresses

Can anyone help me with this?

I have a text file that contains a huge list of blocked IP address to my website, and the list gets auto updated daily.

I need a PHP script that will automatically organize them in numerical order.

Below is a list of today’s additions to the test file - Each addition to the text file always gets auto added to the end of the files previous entry just as you see it below.

I need the script to numerically organize the entire huge list every time i run it, not just the newest additions.

Here is the list of today’s unorganized additions:
222.77.218.153,153.218.77.222.broad.pt.fj.dynamic.163data.com.cn,
1.161.149.28,1-161-149-28.dynamic.hinet.net,
112.101.64.103,112.101.64.103,
146.0.74.210,socalweatherupdates.info,
59.58.159.109,109.159.58.59.broad.pt.fj.dynamic.163data.com.cn,
5.39.217.83,5.39.217.83,
94.153.10.148,94-153-10-148-lvv.broadband.kyivstar.net,
128.73.219.101,128-73-219-101.broadband.corbina.ru,
124.201.123.103,124.201.123.103,
59.58.157.242,242.157.58.59.broad.pt.fj.dynamic.163data.com.cn,
1.161.131.88,1-161-131-88.dynamic.hinet.net,
178.73.212.244,178.73.212.244,
188.165.240.186,ks390108.kimsufi.com,
120.33.241.66,66.241.33.120.broad.pt.fj.dynamic.163data.com.cn,
2.92.141.39,2.92.141.39,
46.118.118.103,sol-fttb.103.118.118.46.sovam.net.ua,
183.62.115.227,183.62.115.227,
5.39.217.56,5.39.217.56,
2.92.141.39,2.92.141.39,
96.41.164.48,96-41-164-48.dhcp.mdfd.or.charter.com,
117.27.138.176,117.27.138.176,
37.59.64.248,37.59.64.248,
91.218.245.253,91.218.245.253,
121.228.10.47,47.10.228.121.broad.sz.js.dynamic.163data.com.cn,
1.161.134.53,1-161-134-53.dynamic.hinet.net,
5.39.217.47,5.39.217.47,

I need it to sort the info to like this:

1.161.131.88,1-161-131-88.dynamic.hinet.net,
1.161.134.53,1-161-134-53.dynamic.hinet.net,
1.161.149.28,1-161-149-28.dynamic.hinet.net,
112.101.64.103,112.101.64.103,
117.27.138.176,117.27.138.176,
120.33.241.66,66.241.33.120.broad.pt.fj.dynamic.163data.com.cn,
121.228.10.47,47.10.228.121.broad.sz.js.dynamic.163data.com.cn,
124.201.123.103,124.201.123.103,
128.73.219.101,128-73-219-101.broadband.corbina.ru,
146.0.74.210,socalweatherupdates.info,
178.73.212.244,178.73.212.244,
183.62.115.227,183.62.115.227,
188.165.240.186,ks390108.kimsufi.com,
2.92.141.39,2.92.141.39,
2.92.141.39,2.92.141.39,
222.77.218.153,153.218.77.222.broad.pt.fj.dynamic.163data.com.cn,
37.59.64.248,37.59.64.248,
46.118.118.103,sol-fttb.103.118.118.46.sovam.net.ua,
5.39.217.47,5.39.217.47,
5.39.217.56,5.39.217.56,
5.39.217.83,5.39.217.83,
59.58.157.242,242.157.58.59.broad.pt.fj.dynamic.163data.com.cn,
59.58.159.109,109.159.58.59.broad.pt.fj.dynamic.163data.com.cn,
91.218.245.253,91.218.245.253,
94.153.10.148,94-153-10-148-lvv.broadband.kyivstar.net,
96.41.164.48,96-41-164-48.dhcp.mdfd.or.charter.com,

Thank you to anyone that can help me out…

See if this works: http://www.php.net/manual/en/function.asort.php

If not let me know and I may be able to come up with something else.

unfortunately I am not that skilled in PHP yet to put something together on my own, I am a novice right now and was kinda hoping someone would already know or have a scrip written already.

If this helps in any way -
I will need the scrip to autorun on this “if” statement:
[php]if ($ini[‘e_mail’] != “”[/php]

and then numerically sorts an iplog.txt file in a static directory and then saves it, so when it is manually opened, the users always sees the data numerically sorted.

The best way to learn it is to play with it. There is an example on that URL that should be very similar to what you want to do. Is your IP data already in an array? Is it in a MySQL database?

Thanks webbwbb - I agree with you on your how to “learn” recommendation and I am doing my best to follow that path of advice…

The IP info is just written to a iplog.txt file only and is not in an array or in any MySQL database.

Okay, read through http://www.tizag.com/phpT/fileread.php and http://php.net/manual/en/function.explode.php

That first one talks about how to open a file with PHP. The second will take a string and separate it into an array. What they mean when they say delimiter is something that it will look for to separate the values. In your case you should try “\n” because that’s how PHP designates that something it a new line. I would play with those for a while and once you have some code, paste it here and I can try to point you in the right direction.

webbwbb - Thanks for the links…

I got this to work some what - See here: http://www.jblairsurf.com/zbblock/test.php

but it is just reading the text file and the output is in one continuous line and not sorting the info numerically. a new line should be split after the second coma.

Example:
1.161.131.88,1-161-131-88.dynamic.hinet.net, 1.161.134.53,1-161-134-53.dynamic.hinet.net, 1.161.149.28,1-161-149-28.dynamic.hinet.net,

Should split down to three lines like this:
1.161.131.88,1-161-131-88.dynamic.hinet.net,
1.161.134.53,1-161-134-53.dynamic.hinet.net,
1.161.149.28,1-161-149-28.dynamic.hinet.net,

[php]

<?php $ipFile = "vault/ippbdb.txt"; $fh = fopen($ipFile, 'r'); $readdata = fread($fh, filesize($ipFile)); fclose($fh); $ipdata = "$readdata"; $ipsplit = explode("\n\n", $ipdata); echo "$ipsplit[0]"; echo "$ipsplit[1]"; echo "$ipsplit[2]"; echo "$ipsplit[3]"; echo "$ipsplit[4]"; echo "$ipsplit[5]"; echo "$ipsplit[6]"; echo "$ipsplit[7]"; echo "$ipsplit[8]"; echo "$ipsplit[9]"; ?>

[/php]

I would use a loop to make this easier and more dynamic. Instead of using
[php]echo “$ipsplit[0]”;echo “$ipsplit[1]”;echo “$ipsplit[2]”;echo “$ipsplit[3]”;echo “$ipsplit[4]”;echo “$ipsplit[5]”;echo “$ipsplit[6]”;echo “$ipsplit[7]”;echo “$ipsplit[8]”;echo “$ipsplit[9]”;
[/php]

I would use a while loop (http://php.net/manual/en/control-structures.while.php). Put your increment variable as the index of your array. If you use $i to count the iterations through your loop then you would use $ipsplit[$i] so that each time it goes through the loop it will reference the next item. You can then use the count function (http://php.net/manual/en/function.count.php) to see how many values are in your file and use that for the condition of your loop. As far as the problem with it not making new lines, try adding \n to the end of your echo. If you don’t get what I mean with the loop then I can post an example with some explanations added.

Sponsor our Newsletter | Privacy Policy | Terms of Service