PHP Howto Read IP address of remote computer/browser

by Vivek Gite on February 27, 2006 · 21 comments

PHP has in built facility to detect remote browser or IP address and other properties. These values are assigned to special variable. In this case, it is assigned to variable called REMOTE_ADDR. You can use any one of the following statement to obtained or read IP address:

$ip = $_SERVER['REMOTE_ADDR'];
OR
$ip= $REMOTE_ADDR;

Here is sample code:

<?
$ip= $REMOTE_ADDR;
echo "<br> Your IP address : " . $ip;
echo "<br> Your hostname : " . GetHostByName($ip);
?>

See PHP getenv for more information.

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 21 comments… read them below or add one }

1 soroccoheaven October 2, 2007

hi,

I am trying to figure out the “spam” word enabled form submission like this one …anyone here can help me ..

Thanks

Reply

2 Dmitri Zhuchkov November 28, 2007

echo “ Your hostname : ” . GetHostByName($ip);

You can get the host name from IP.gethostbyname

gethostbyname – Get the IP address corresponding to a given Internet host name

Try the gethostbyaddr for this task.

Reply

3 Rajesh January 2, 2008

How to get the remote IP location?

Reply

4 Timmehx January 4, 2008

<?php
$ip = $_SERVER['REMOTE_ADDR'];

And if you want to echo it use

that is what i use

Reply

5 Nilesh January 6, 2008

hey! this is a child’s play! such an easy thing! i said that coz i am 15 and a hard core PHP developer!!

Reply

6 Ben December 11, 2008

I don’t understand something. 5 of my friends using different HOSTS in different CITIES used a script that logged each user’s IP address and time in a database and all their IP was exactly the same as mine. I used $_SERVER['REMOTE_ADDR']

Reply

7 Joshua April 22, 2011

Hey ben, all of your IP’s are the same since all of you are using the same ISP or the same main ISP:

user1->ISP1->main_isp->internet
you->ISP3->main_isp->internet

So the website actually detects your main_isp IP address since none of you have a dedicated IP address on the internet.

Reply

8 Geert March 22, 2009

They are all behind the same proxy-server. In that case $_SERVER['REMOTE_ADDR'] provides you with the ip-address of the proxy-server.

This function will determine the real ip-adress:

function ipCheck() {
		if (getenv('HTTP_CLIENT_IP')) {
			$ip = getenv('HTTP_CLIENT_IP');
		}
		elseif (getenv('HTTP_X_FORWARDED_FOR')) {
			$ip = getenv('HTTP_X_FORWARDED_FOR');
		}
		elseif (getenv('HTTP_X_FORWARDED')) {
			$ip = getenv('HTTP_X_FORWARDED');
		}
		elseif (getenv('HTTP_FORWARDED_FOR')) {
			$ip = getenv('HTTP_FORWARDED_FOR');
		}
		elseif (getenv('HTTP_FORWARDED')) {
			$ip = getenv('HTTP_FORWARDED');
		}
		else {
			$ip = $_SERVER['REMOTE_ADDR'];
		}
		return $ip;
	}

Reply

9 Sarmad Mahar June 17, 2010

it do not work if client computer is using ISA Server.

but code is working fine if client is connected with DSL , or modem direct internet connection.

can any one provide how do I resolved issue on intranet application/ Client is using ISA

Reply

10 leni June 10, 2009

how can i get the ip address of the client requesting a particular url?
if possible give me a php code for the same..

Reply

11 Young Bobby October 22, 2009

Yay;

Thanks it’s working fine !

Reply

12 chuman September 24, 2010

supose am chatting with my friend using gtalk how would i get his ip address. [what command do i got to use in DOS]. what i got his ip what command [DOS} do i use to get his host name.
One more thing to what command to view that a specified computer is online or not.

Reply

13 DarkCoder November 29, 2010

Seems not to work for ‘localhost’ but works well with other IPs in the system

Reply

14 Marko April 12, 2011

Why do you have 2 x same conditions in this script ?

elseif (getenv(‘HTTP_X_FORWARDED_FOR’)) {
$ip = getenv(‘HTTP_X_FORWARDED_FOR’); x 2
}
and

elseif (getenv(‘HTTP_FORWARDED’)) {
$ip = getenv(‘HTTP_FORWARDED’); x 2
}

Reply

15 Joshua April 22, 2011

they arent 2 same conditions in the script marco, if you check back you will notice all the conditions have a slight difference

Reply

16 Magson Fernandes July 15, 2011

Can any post a simple ip detection script or code. i need it please.

Reply

17 MoCua.Com August 6, 2011

thanks and thanks

Reply

18 anand September 14, 2011

how to get ip with computer name in php

Reply

19 developer rashtra September 23, 2011

thanks for the code. Looking for php code to get computer name of a visitor.

Reply

20 lineesh January 20, 2012

thanks vivek

Reply

21 ram January 30, 2012

how to send messages or popups to remote ip address

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 6 + 5 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.



Previous post:

Next post: