Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing IP addresses

Tags:

php

ip

function getIp() {
  return "<?php echo $_SERVER['REMOTE_ADDR']; ?>";
}

I want to adapt or redo this so that certain actions can only be taken from localhost or my isp server. I can make it work with a little bit of JavaScript for 127.0.0.1 but not for the other.

Appreciate any help; thanks

like image 473
Chris Glasier Avatar asked Feb 24 '26 07:02

Chris Glasier


1 Answers

You really want to do this completely on the server. If you check for IPs on the client side then people can very easily hack around that. For example by modifying your code in their browser using a web inspector like Firebug.

Fortunately it is pretty simple to do on the server side:

The $_SERVER['REMOTE_ADDR'] variable is a simple string so you should be able to use string comparison to check for the IPs that you want.

Like for example:

<?php
   if ($_SERVER['REMOTE_ADDR'] == "127.0.0.1" || $_SERVER['REMOTE_ADDR'] == "1.2.3.4") {
       echo "Show secret things here";
   }
?>
like image 152
Stefan Arentz Avatar answered Feb 25 '26 20:02

Stefan Arentz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!