简体   繁体   English

如何在PHP中将文件夹命名为用户的IP地址

[英]How to name a folder as the user's IP address in PHP

I've searched long and hard for an answer to this question with no luck, hence this question is being asked. 我一直在努力寻找这个问题的答案,但是没有运气,因此有人提出了这个问题。

How could I possibly name a folder as the client's IP address? 如何将文件夹命名为客户端的IP地址? For example: 例如:

192.168.0.1 visits my website and I want php to automatically make a folder with the name '192.168.0.1' (I can do the rest such as make a document inside showing logs etc, I just need to know if thats possible). 192.168.0.1访问我的网站,我希望php自动创建一个名为“ 192.168.0.1”的文件夹(我可以做其他工作,例如在显示日志的文件内做记录等,我只需要知道这是否可行)。

I've tried using global variables such as the [REMOTE_ADDR] but with no luck, it always shows an error on the webpage. 我尝试使用[REMOTE_ADDR]这样的全局变量,但是没有运气,它总是在网页上显示错误。

You can make a directory by using the mkdir() function as follows: 您可以使用mkdir()函数创建目录,如下所示:

mkdir('path/to/directory')

To find out the user's IP address you can use the 'REMOTE_ADDR' key of the $_SERVER variable: 要找出用户的IP地址,可以使用$_SERVER变量的'REMOTE_ADDR'键:

$_SERVER['REMOTE_ADDR']

Basically this will result in the following code: 基本上,这将导致以下代码:

$directory_name = $_SERVER['REMOTE_ADDR']
mkdir('path/to/' + $directory_name)

Please note that the received IP from REMOTE_ADDR can't always be trusted. 请注意,从REMOTE_ADDR接收到的IP并非总是可信赖的。 See this answer for more information about the subject. 有关主题的更多信息,请参见此答案。

You can do like this: 您可以这样:

1. Get the user ip: 1.获取用户ip:

$ip = $_SERVER['REMOTE_ADDR']

2. Make the directory: 2.创建目录:

mkdir(''.$ip);

example here 这里的例子

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

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