• 0

[PHP] Searching a file for an IP address


Question

Hey guys.

I have a text file that has a list of IP addresses:

1.2.3.4
5.6.7.8
2.4.6.8
3.5.7.9
etc
etc

Now, I need to search this file using PHP to see if an IP address is in that list.

I have a bit of code:

$file = file_get_contents($iprecord);
if(!strpos($file, $visitorip)) 
{
	 # REDIRECT TO PAGE 1
}
else
{
	 # REDIRECT TO PAGE 2
}

Now for some reason, even though the IP address is in the list, the visitor is always getting redirected to PAGE 1.

I am guessing there is a format problem but cannot figure it out so any help appreciated!!

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0
Its okay - found the problem damned whitespace!!!

Just a rule of thumb - instead of just going through absolute gluts of text in a single bound - its usually better practice to break up the text into small chunks. This also makes it easier to process on a per line (or in your case ip) basis and allow for phps trim operations to be applied and ensure that whitespaces are no longer an issue.

Link to comment
Share on other sites

  • 0
Just a rule of thumb - instead of just going through absolute gluts of text in a single bound - its usually better practice to break up the text into small chunks. This also makes it easier to process on a per line (or in your case ip) basis and allow for phps trim operations to be applied and ensure that whitespaces are no longer an issue.

I did go that way in the end - one text file per day and one IP per line, those files are small enough to play with then.

I also have learned the benefits of trim!!!

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.