0

Suppose user's PC IP is 192.168.10.81(IPv4 in his personal PC's windows)

When this user browse www.mydomain.com I want to get this IP from www.mydomain.com/index.php using PHP

Is it possible? If possible, How?

2
  • $_SERVER['REMOTE_ADDR'] ? Commented Mar 3, 2015 at 7:32
  • Is the domain in the same internal network? Sounds like you want to get the private IP of the PC, which isn't possible. Commented Mar 3, 2015 at 7:59

2 Answers 2

1

Try this code..

if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip = $_SERVER['REMOTE_ADDR']; } 
Sign up to request clarification or add additional context in comments.

2 Comments

It gives real ip over which browsing this domain. Not personal pc's IP
I know that this answer is copied from the other answer so have some courtesy to provide a credit link to real author and also, you should be explaining what the code does rather than just writing Try this code...
0

$_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.

Try this code:

<?php $ip = $_SERVER['REMOTE_ADDR']; $browser = $_SERVER['HTTP_USER_AGENT']; $referrer = $_SERVER['HTTP_REFERER']; if ($referred == "") { $referrer = "This page was accessed directly"; } echo "<b>Visitor IP address:</b><br/>" . $ip . "<br/>"; echo "<b>Browser (User Agent) Info:</b><br/>" . $browser . "<br/>"; echo "<b>Referrer:</b><br/>" . $referrer . "<br/>"; ?> 

Output like this:

Visitor IP address: 127.0.0.1

Browser (User Agent) Info: Mozilla/5.0 (Windows NT 6.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1

Referrer:

http://www.hexrara.com

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.