Whizard
Whizard

Reputation: 11

PHP: IP address Check in text file

I've been trying to grab a person's ip-address and then store it in a txt file when a person pays me to access certain content. When I store the IP in the txt file (logFile.txt), I want the person that paid the fee to be redirected to a content page that is only available for the people that have payed. I also want to be able to redirect people that try and copy/paste the browser link to the "extra content".

I have code in my in my site that can log people's ip-address, but i want take that log file and cross reference it with the extra content page.

p.s. i've spent hours trying to figure this out with logging it to a text file to avoid mysql

I just need a general idea so i can wrap my head around this thing. not sure how to go about doing all this.

Upvotes: 1

Views: 925

Answers (2)

Jon Newmuis
Jon Newmuis

Reputation: 26502

  1. Why are you trying to avoid MySQL? If two users do this at the same time, one of the writes to a log file could either be lost or blocked. MySQL takes measures to prevent this. It's also likely to be faster.
  2. IP addresses are neither uniquely identifying (two users could be using one IP address) nor persistent to a given user (one user could be using two IP addresses). I would be very upset if I paid for content, then couldn't access it any more because I don't have a static IP address.
  3. Lastly (to more directly answer your question), you can use $_SERVER['REMOTE_ADDR'] to get the user's IP address and file_put_contents to write it to a file, but I'm not certain the way you're going about this is the best way.

Upvotes: 4

F21
F21

Reputation: 33391

You can store the IP addresses in an array:

array{
   [0] => '192.168.1.1',
   [1] => '192.168.1.2',
}

Then before saving to your text file, use serialize on the array. This will return a string that you can write to your text file.

To retrive your array, just use unserialize to get the array back. Now, you should have an array of IP addresses which you can use the standard array functions to search, add and delete from.

Upvotes: 1

Related Questions