Jump to content

Can $_SERVER['REMOTE_ADDR'] be trusted ?


tonit

Recommended Posts

Hello, I have a website where only a couple of people can access it, so the number of IPs logged in is very limited. Everything submitted by the 'admins' logged in is sent to a specific folder dependent of their IP Address. Again they can't access the website through a proxy or anything because there's a limited range of IPs that is allowed.

 

Can I trust $_SERVER['REMOTE_ADDR'] to give a valid IP so the log-system would be 100 % stable and efficient ?

 

Link to comment
Share on other sites

It's not impossible to forge an IP address (but it is incredibly rare). The upside is that it only works on the request - the forger won't get the response.

 

With that said, to log in the client must receive the appropriate cookies (which go in the response). For that to happen the IP address must be correct. If you track the login IP address in the session/database and compare that with the IP address for each sensitive request you should be able to prevent any IP spoofing exploits.

Link to comment
Share on other sites

I'd be surprised if it was a PHP bug (kinda surprised). My first guess would be that the web server had a kind of proxy or load balancer running.

 

Exactly.  If for example, you use a reverse proxy for load balancing, REMOTE_ADDR will be the same for every user.  In that case, PHP will typically load $_SERVER['HTTP_X_FORWARDED_FOR'].  It will often be an array with all the IP addresses that were utilized.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.