MD SHAHIDUL ISLAM
MD SHAHIDUL ISLAM

Reputation: 14523

How to get user PC ip from server in php

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?

Upvotes: 0

Views: 117

Answers (2)

RaMeSh
RaMeSh

Reputation: 3424

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

Upvotes: 0

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21422

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'];
}

Upvotes: 1

Related Questions