how to get client's IP address in PHP
thanks for reply.
$_SERVER['REMOTE_HOST'] doesn't give any output.
$_SERVER['REMOTE_ADDR'];
check out all the predefined $_SERVER variables here
$SERVER[REMOTE_HOST] doesn't give any output.
$SERVER[REMOTE_ADDR] gives local host's address(127.0.0.1 )as output. but i need client's ip address(192...*).
when i use $_SERVER['REMOTE_ADDR']; i get my IP address..
however, the ip address that you may have been told of by your ISP and the one that requests page information may be different if you use proxy or routers, etc..
i have checked all server variables.
<center><b>Server variables</b></center>
<hr>
GATEWAY_INTERFACE = <?php echo $SERVER['GATEWAY_INTERFACE']; ?> <br>
SERVER_ADDR = <?php echo $SERVER['SERVER_ADDR']; ?> <br>
SERVER_NAME = <?php echo $SERVER['SERVER_NAME']; ?> <br>
SERVER_SOFTWARE = <?php echo $SERVER['SERVER_SOFTWARE']; ?> <br>
SERVER_PROTOCOL = <?php echo $SERVER['SERVER_PROTOCOL']; ?> <br>
REQUEST_METHOD = <?php echo $SERVER['REQUEST_METHOD']; ?> <br>
REQUEST_TIME = <?php echo $SERVER['REQUEST_TIME']; ?> <br>
QUERY_STRING = <?php echo $SERVER['QUERY_STRING']; ?> <br>
DOCUMENT_ROOT = <?php echo $SERVER['DOCUMENT_ROOT']; ?> <br>
HTTP_ACCEPT = <?php echo $SERVER['HTTP_ACCEPT']; ?> <br>
HTTP_ACCEPT_CHARSET = <?php echo $SERVER['HTTP_ACCEPT_CHARSET']; ?> <br>
HTTP_ACCEPT_ENCODING = <?php echo $SERVER['HTTP_ACCEPT_ENCODING']; ?> <br>
HTTP_ACCEPT_LANGUAGE = <?php echo $SERVER['HTTP_ACCEPT_LANGUAGE']; ?> <br>
HTTP_CONNECTION = <?php echo $SERVER['HTTP_CONNECTION']; ?> <br>
HTTP_HOST = <?php echo $SERVER['HTTP_HOST']; ?> <br>
HTTP_REFERER = <?php echo $SERVER['HTTP_REFERER']; ?> <br>
HTTP_USER_AGENT = <?php echo $SERVER['HTTP_USER_AGENT']; ?> <br>
HTTPS = <?php echo $SERVER['HTTPS']; ?> <br>
REMOTE_ADDR = <?php echo $SERVER['REMOTE_ADDR']; ?> <br>
REMOTE_HOST = <?php echo $SERVER['REMOTE_HOST']; ?> <br>
REMOTE_PORT = <?php echo $SERVER['REMOTE_PORT']; ?> <br>
SCRIPT_FILENAME = <?php echo $SERVER['SCRIPT_FILENAME']; ?> <br>
SERVER_ADMIN = <?php echo $SERVER['SERVER_ADMIN']; ?> <br>
SERVER_PORT = <?php echo $SERVER['SERVER_PORT']; ?> <br>
SERVER_SIGNATURE = <?php echo $SERVER['SERVER_SIGNATURE']; ?> <br>
PATH_TRANSLATED = <?php echo $SERVER['PATH_TRANSLATED']; ?> <br>
SCRIPT_NAME = <?php echo $SERVER['SCRIPT_NAME']; ?> <br>
REQUEST_URI = <?php echo $SERVER['REQUEST_URI']; ?> <br>
PHP_AUTH_DIGEST = <?php echo $SERVER['PHP_AUTH_DIGEST']; ?> <br>
PHP_AUTH_USER = <?php echo $SERVER['PHP_AUTH_USER']; ?> <br>
PHP_AUTH_PW = <?php echo $SERVER['PHP_AUTH_PW']; ?> <br>
AUTH_TYPE = <?php echo $SERVER['AUTH_TYPE']; ?> <br>
by executing the above PHP codes, i got output as
Server variables
GATEWAY_INTERFACE = CGI/1.1
SERVER_ADDR = 127.0.0.1
SERVER_NAME = localhost
SERVER_SOFTWARE = Apache/1.3.23 (Win32)
SERVER_PROTOCOL = HTTP/1.1
REQUEST_METHOD = GET
REQUEST_TIME =
QUERY_STRING =
DOCUMENT_ROOT = c:/apache/htdocs
HTTP_ACCEPT = /
HTTP_ACCEPT_CHARSET =
HTTP_ACCEPT_ENCODING = gzip, deflate
HTTP_ACCEPT_LANGUAGE = en-us
HTTP_CONNECTION = Keep-Alive
HTTP_HOST = localhost
HTTP_REFERER =
HTTP_USER_AGENT = Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
HTTPS =
REMOTE_ADDR = 127.0.0.1
REMOTE_HOST =
REMOTE_PORT = 1642
SCRIPT_FILENAME = c:/apache/php/php.exe
SERVER_ADMIN = admin@localhost
SERVER_PORT = 80
SERVER_SIGNATURE =
Apache/1.3.23 Server at localhost Port 80
PATH_TRANSLATED = c:\apache\htdocs\test4.php
SCRIPT_NAME = /php/php.exe
REQUEST_URI = /test4.php
PHP_AUTH_DIGEST =
PHP_AUTH_USER =
PHP_AUTH_PW =
AUTH_TYPE =
please help me to figure out what is going wrong with my codes.
<?php
echo ' Client IP: ';
if ( isset($_SERVER["REMOTE_ADDR"]) ) {
echo '' . $_SERVER["REMOTE_ADDR"] . ' ';
} else if ( isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ) {
echo '' . $_SERVER["HTTP_X_FORWARDED_FOR"] . ' ';
} else if ( isset($_SERVER["HTTP_CLIENT_IP"]) ) {
echo '' . $_SERVER["HTTP_CLIENT_IP"] . ' ';
}
?>
The reason you're getting 127.0.0.1 from REMOTE_ADDR is because that is your remote address. If you type in http://localhost/ into your browser, you're using the loopback interface 127.0.0.1. This interface is entirely separate from your actual LAN address of 192.x.x.x that you're expecting.
If you type in http://192.12.34.56/ (using your real internal IP instead), you'll see 192.12.34.56 returned. Why? Because you're connecting to Apache on a totally different interface.
I use this function :
function getIP() {
$ip;
if (getenv("HTTP_CLIENT_IP"))
$ip = getenv("HTTP_CLIENT_IP");
else if(getenv("HTTP_X_FORWARDED_FOR"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if(getenv("REMOTE_ADDR"))
$ip = getenv("REMOTE_ADDR");
else
$ip = "UNKNOWN";
return $ip;}
refer : http://admincmd.blogspot.com
none of these _SERVER things worked for me either. but this did:
$joe = apache_request_headers();
echo $joe['PC-Remote-Addr'];
Hi bradgrafelman
Am also getting same problem in getting Ipaddress... I tried my internal it ([url]http://192.168[/url]...) instead ([url]http://localhost/[/url]) bur stil am getting result as 127.0.0.1 like that..
wat is solution for this..
hi chramya,
Are you connecting to apache from the same machine were apache is installed?
If you are doing this, you may check the following:
from command prompt type command (windows):
route print
it shows the route table, which will see something like that:
Network Destiny Netmask Gateway Interface Metrica
....
172.168.0.7 255.255.255.255 127.0.0.1 127.0.0.1 10
.....
default gateway: 192.168.0.1
if you see a line on your ip have as a gateway 127.0.0.1, your connection to apache goes throught loopback (127.0.0.1).
Try to connect from other machine or put your web on a server (out of your local network) and try again.
Ujin
[SIZE="3"]Hi i would like to kwon how can i see others IP from a mail I´ve recieved
Thanks...[/SIZE]
try this dude.
first pack in a variable like so.
<?PHP
$ip=$_SERVER["REMOTE_ADDR"];
?>
and to use it where you want do this
<?PHP echo $ip; ?>
and there you have it.
also if you are testing it on your local server it will give you ip 127.0.0.1 as this is the local host ip. tru it on the net from another PC.
Good luck
I certainly hope he's had time to solve his issue in the two years since his last post.
<?php
$ip= $REMOTE_ADDR;
echo " Your ip address: " . GetHostByName($ip);
?>
This script will assign the clients IP to a varrible so you can store it a database or just easily echo out the users IP addess. Hope this was help full!
<?php
if ( isset($_SERVER["REMOTE_ADDR"]) ) {
$ip=$_SERVER["REMOTE_ADDR"] . ' ';
} else if ( isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ) {
$ip=$_SERVER["HTTP_X_FORWARDED_FOR"] . ' ';
} else if ( isset($_SERVER["HTTP_CLIENT_IP"]) ) {
$ip=$_SERVER["HTTP_CLIENT_IP"] . ' ';
}
echo 'Clients IP Address: ' . $ip;
?>
This script will assign the clients IP to a varrible so you can store it a database or just easily echo out the users IP addess. Hope this was help full! check out my site im building. www.mysocialent.net
<?php
if ( isset($_SERVER["REMOTE_ADDR"]) ) {
$ip=$_SERVER["REMOTE_ADDR"] . ' ';
} else if ( isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ) {
$ip=$_SERVER["HTTP_X_FORWARDED_FOR"] . ' ';
} else if ( isset($_SERVER["HTTP_CLIENT_IP"]) ) {
$ip=$_SERVER["HTTP_CLIENT_IP"] . ' ';
}
echo 'Clients IP Address: ' . $ip;
?>
trigg12;10818829 wrote:I use this function :
refer : http://admincmd.blogspot.com
You saved my life with this one, Trigg!
I have user the 'REMOTE_ADDR' variable for years on the same server and suddenly, this started to give server address, not the client.
Does anyone have an idea why this happened? I only tweaked with phptraffic. Maybe it has messed something in the shades?
hi
jeyabala.. try dis..
$ip= $REMOTE_ADDR;
echo "<br> Your ip : " .GetHostByName($ip);