PHP Environmental Variables

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

Hello
How do I return a users IP address and referring page?
I'm writing a security section of a script of mine, and I need to log IPs and referring pages, so there aren't multiple users on one IP, and that they are in deed coming from my site to this script.
Thanks!

Laughing out loud

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

You can get a list of all vars here:
PHP Predefined Variables
Or by looking at phpinfo()

Apache variables
$HTTP_REFERER
$REMOTE_ADDR

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

Ok
I'm using a router, so $REMOTE_ADDR returns 192.168.1.1
Is there any way to get around this?
Thanks

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I've noticed that on my network too. It seems to return 192.168.1.1 because your 'remote computer' is on the LAN. If you access the site from outside your LAN (on the WAN side), then you should get a real IP.

I've not found a way around this. It wasn't crucial for me at the time, so I didn't spend too much time researching it. If you find something, let us know.

Mark Hensler
If there is no answer on Google, then there is no question.

mairving's picture

They have: 2,256 posts

Joined: Feb 2001

That's a weird one. Mine returns the IP address of the remote machine, not the router IP.

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

Ok...
I had 3 friends run this script.
One was on a LAN and it gave the 192.168.1.1
Another was on a LAN and it gave his internal IP of 192.168.1.22
The last one was on a dialup, and it returned his IP.

Laughing out loud

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

*shivers*
Freaky weird!

Maybe the routers are handling it differently?
My router is the Linksys BEFSR41, and it's acting as the DHCP server. Anyone use a different setup?

Mark Hensler
If there is no answer on Google, then there is no question.

mairving's picture

They have: 2,256 posts

Joined: Feb 2001

Yeah, well the dialup should return the IP of the ISP. The other differences probably would be in the use of proxy servers.

They have: 601 posts

Joined: Nov 2001

Here is what I found on the issue.

Quote:
REMOTE_HOST
The name of the machine from which the client is accessing the form. If the client machine is using a proxy to access the web, then this will be the name of the proxy host. If there does not exist a hostname for the client's machine, this will be unset.

REMOTE_ADDR
The IP address of the client machine. This should be the IP address which the hostname given in REMOTE_HOST will decode to [ie: they should be the same machine]. If the client machine is using a proxy to access the web, this will be the IP address of the proxy machine.

However some web servers can also intercept the ENV VAR REMOTE_PROXY to determine if the user is accessing through a proxy server, and if they are, you can do a simple check to see if the REMOTE_ADDR is set to the proxy or the IP.

Cheers

- wil

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.