简体   繁体   English

PHP:IP地址签入文本文件

[英]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. 我一直试图获取一个人的ip地址,然后在有人向我付款以访问某些内容时将其存储在txt文件中。 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. 当我将IP存储在txt文件(logFile.txt)中时,我希望将支付费用的人重定向到仅对支付者可用的内容页面。 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. 我的网站上有代码可以记录人们的ip地址,但是我想获取该日志文件并将其与额外的内容页面进行交叉引用。

ps i've spent hours trying to figure this out with logging it to a text file to avoid mysql PS我花了几个小时试图解决这个问题,将其记录到文本文件中以避免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. 不知道该怎么做。

  1. Why are you trying to avoid MySQL? 为什么要避免使用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. MySQL采取措施来防止这种情况。 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). IP地址既不能唯一标识(两个用户可以使用一个IP地址),也不能持久地存在于给定用户(一个用户可以使用两个IP地址)。 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. 如果我为内容付费,那我会很沮丧,然后因为我没有静态IP地址而无法再访问它。
  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. 最后(为了更直接地回答您的问题),您可以使用$_SERVER['REMOTE_ADDR']获取用户的IP地址,并使用file_put_contents将其写入文件,但是我不确定您打算采用的方式是最好的方法。

You can store the IP addresses in an array: 您可以将IP地址存储在数组中:

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. 现在,您应该拥有一个IP地址数组,可以使用标准的数组功能来搜索,添加和删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM