scriptygoddess

18 Mar, 2003

Get users IP with PHP

Posted by: Jennifer In: Script snippet

Not sure why I haven't posted this before. (or maybe I did and I'm just drawing a blank). Get the IP of the user:

if ($_SERVER['HTTP_X_FORWARD_FOR']) {
$ip = $_SERVER['HTTP_X_FORWARD_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}

I've also seen it done this way too but for some reason I was under the impression the method above was more reliable:

$ip = $REMOTE_ADDR;

If you want to resolve the domain name of the IP (as seen here):

$domain = GetHostByName($REMOTE_ADDR);

12 Responses to "Get users IP with PHP"

1 | Gregory

March 18th, 2003 at 11:13 am

Avatar

Actually, depending on what version of PHP you are using this may not work. Instead of using getenv or $REMOTE_ADDR I believe you should be using $_SERVER['HTTP_X_FORWARD_FOR'] and $_SERVER['REMOTE_ADDR']. There are some comments on the manual page at http://www.php.net/manual/en/language.variables.predefined.php#language.variables.superglobals

There's also some discussion there as to times when HTTP_X_FORWARD_FOR may not give expected results.

2 | Amy

March 18th, 2003 at 11:20 am

Avatar

Gregory – you're right about the $_SERVER['REMOTE_ADDR'] part. It's been de facto long enough that I think we can safely assume that all users should be doing it that way.

The list of predefined variables in PHP, and what they mean, can be found here.

3 | Jennifer

March 18th, 2003 at 11:34 am

Avatar

Updated the post. Thanks!

4 | reese

March 18th, 2003 at 5:38 pm

Avatar

Hi, excuse my naivety…I do use PHP and other hacks on my site, but I'm clueless as to how to implement this. WOuld it be an include or in an htaccess file? And then how would you view the results?

5 | Jennifer

March 18th, 2003 at 5:53 pm

Avatar

Put your script tags around it <? and ?> and put it on your page, or in an include (it's not .htacess related so don't put it there). Then you have the user's IP as a "string" type in that variable $ip – you can do anything you want with it. Write it to a file, display it on the screen, write it to a database… all depends on what you want to do. This is just a little snippet – not too useful on its own… more as a piece to a larger whole.

6 | Neil

March 22nd, 2003 at 9:02 am

Avatar

How could I ustilize this in a Contact Form?
I am currently using a Form where I don't know how to get the respondents' IP address.

I will apprecdiate any help.
thanks
neil

7 | Corey

September 24th, 2003 at 11:10 pm

Avatar

Neil,

NOTE: as others mentioned above, these very by PHP version. But the ones below work for the latest versions (4.0+, ans in some of the 3+,,,they have just now released version 4.3.3)

<?php
$IP=$_SERVER['REMOTE_ADDR'];
$HOST= gethostbyaddr($IP);
?>
Your page content…blah…blah…blah…

The need to include the "php" right after the start of the script ("<?php") and in the "echo" statement varies by version of PHP, and in current versions can be set by the host. Try it without it first, and if it works use it so that you don't use alot of bandwidth on a PHP-heavy site.

8 | Corey

September 24th, 2003 at 11:14 pm

Avatar

SORRY about the above, forgot to take-out my "" tags, so it didn't all post.

Neil,

NOTE: as others mentioned above, these very by PHP version. But the ones below work for the latest versions (4.0+, ans in some of the 3+,,,they have just now released version 4.3.3)

<?php
$IP=$_SERVER['REMOTE_ADDR'];
$HOST= gethostbyaddr($IP);
?>
Your page content…blah…blah…blah…
<form…..>
<input type=hidden name="UserIP" value="<?php echo $IP ?>">
<input type=hidden name="HostName" value="<?php echo $HOST ?>
</form>
The need to include the "php" right after the start of the script ("<?php") and in the "echo" statement varies by version of PHP, and in current versions can be set by the host. Try it without it first, and if it works use it so that you don't use alot of bandwidth on a PHP-heavy site.

9 | C:

October 24th, 2003 at 1:36 pm

Avatar

Many thanks!!! you are in my fav. Goddess!

10 | Edwin

November 24th, 2003 at 10:43 am

Avatar

I have just tried this code at the top of a PHP script:

2    if ($_SERVER['HTTP_X_FORWARD_FOR'])
3 {
4 $REM_ADDR = $_SERVER['HTTP_X_FORWARD_FOR'];
5 } else {
6 $REM_ADDR = $_SERVER['REMOTE_ADDR'];
7 }
8  $HOST = $_SERVER['HTTP_HOST']; // grabs host
9  $USER_AGENT = $_SERVER['HTTP_USER_AGENT'];

But it gives me this error in the browser when I upload and vioew it online:

Parse error: parse error in /home/schofield/public_html/test.php on line 2

I have tried with JUST this code – nothing else and I still get the error. Is it to do with server settings at my ISP?

Not had any other problems with my PHP on the server.

This is the
http://www.extendev.co.uk/test.php

11 | Jennifer

November 24th, 2003 at 10:50 am

Avatar

Silly question maybe – but what's in line 1? Try removing any extra spaces around the code – maybe there's some hidden characters that aren't displaying in your text editor that's messing it up?

12 | Royce Hart

December 18th, 2003 at 3:05 pm

Avatar

I'm not certain, but I believe that GetHostByName() has been crashing in instances where the IP address of the visitor does not resolve. Has anyone else experienced issues with this function? Thanks.

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements