Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

definitive way to get user ip address php [duplicate]

Tags:

php

Possible Duplicate:
Function to get user ip address

<?PHP  $ipaddress = $_SERVER["REMOTE_ADDR"];  echo "Your IP is $ipaddress!";  ?> 

I was told this way of getting ip address has issues such as being able to fool. Is there a better way to gather ip address? looking for tutorials of better way to get ip address?

like image 422
user62617 Avatar asked Jul 22 '11 18:07

user62617


People also ask

How do you get the user's IP address in PHP?

Using getenv() function: To get the IP Address,we use getenv(“REMOTE_ADDR”) command. The getenv() function in PHP is used for retrieval of values of an environment variable in PHP. It is used to return the value of a specific environment variable.

What is $_ SERVER [' Remote_addr ']?

$_SERVER['REMOTE_ADDR'] Returns the IP address from where the user is viewing the current page. $_SERVER['REMOTE_HOST'] Returns the Host name from where the user is viewing the current page. $_SERVER['REMOTE_PORT']

How can I get static IP in PHP?

The simplest way is to use $_SERVER with 'REMOTE_ADDR', it will return the user's IP address who is currently viewing the page. Example using ['REMOTE_ADDR'] to identify the server's IP address in PHP.

Which script correctly validates an IP address?

The FILTER_VALIDATE_IP filter validates an IP address. Possible flags: FILTER_FLAG_IPV4 - The value must be a valid IPv4 address. FILTER_FLAG_IPV6 - The value must be a valid IPv6 address.


1 Answers

$_SERVER['REMOTE_ADDR'] is the only reliable IP address you'll get - it's extracted directly from the TCP stack and is where the current connection was established from. This means if the user is connecting via a proxy, you'll get the proxy's address, not the user's.

Any of the other header-based ones are unreliable, as HTTP headers are trivial to forge. You can use the information from them, if you'd like, as long as you don't TRUST it.

like image 56
Marc B Avatar answered Oct 20 '22 00:10

Marc B