-1

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? 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).

I've tried using global variables such as the [REMOTE_ADDR] but with no luck, it always shows an error on the webpage.

asd
  • 11
  • 3
  • You forgot to post the most important information - your code and what error it throws. `$_SERVER['REMOTE_ADDR']` should contain the client's IP address, but there's no guarantee. Also, you should remember that more users can be behind a single IP address. – David Ferenczy Rogožan May 01 '16 at 02:46

2 Answers2

1

You can make a directory by using the mkdir() function as follows:

mkdir('path/to/directory')

To find out the user's IP address you can use the 'REMOTE_ADDR' key of the $_SERVER variable:

$_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. See this answer for more information about the subject.

Community
  • 1
  • 1
Bono
  • 4,757
  • 6
  • 48
  • 77
  • A folder with '0' is made but not my IP. – asd Apr 30 '16 at 16:16
  • @meherbanulhaq If you do `var_dump($_SERVER['REMOTE_ADDR'])` what does it give you? – Bono Apr 30 '16 at 16:17
  • string(3) "::1" - Is what it gives me but the folder name is just '0', shouldn't the folder name be "::1" too? – asd Apr 30 '16 at 16:21
  • Ah, apparently `::1` is the equivalent of `127.0.0.1` for `IPv6` addresses. Perhaps that will get you in some trouble as well when it comes to naming directories, because Windows does not support `:` characters for directory names. Still doesn't quite explain why your directory was created with name `0` however. – Bono Apr 30 '16 at 16:29
1

You can do like this:

1. Get the user ip:

$ip = $_SERVER['REMOTE_ADDR']

2. Make the directory:

mkdir(''.$ip);

example here

V. Sambor
  • 12,361
  • 6
  • 46
  • 65