2

I wish to get the local machine ip address on which a particular has to run. To run the page I first check if the local IP address matches the specified one and only then it will open that page. I am programming in PHP.

I have tried $_SERVER and getenv but the address provided by them does not match the IP address of my computer. Moreover they change every time the connection refreshes.

Please help.

  • 1
    Do you want the address of the *client* computer (where your browser runs) or the *server* computer (where the PHP program actually runs)? Remember that in most cases PHP is a *server* side programming language. – Some programmer dude Sep 25 '16 at 06:34
  • 1
    I want the address of the client computer. I want to check that if it is my computer, or the designated person's computer, only then the pages should open. – Gurucharan Sharma Sep 25 '16 at 06:36
  • That's not really good security. It might also be impossible if you're using a broadband subscription where the address you get assigned is not static and fixed. – Some programmer dude Sep 25 '16 at 06:46
  • `$localIPAddress = getHostByName(getHostName());` Try this one – Sampad Sep 25 '16 at 06:53
  • Is the client computer (your computer) on the same, local, network as the server? Or is the client computer connecting across the internet. For both on the local network then you need to tell the router to always assign the same IP address to each computer. To do this you assign IP by MAC address in your router admin pages. That way your computer and the server will always have the same IP address. Now, if your client is connecting across the internet? That is another story... and slightly more work :) – Ryan Vincent Sep 25 '16 at 10:33
  • Client connects accross internet. – Gurucharan Sharma Sep 25 '16 at 15:34

1 Answers1

5

You can find IP address is using the $_SERVER['REMOTE_ADDR'] or $_SERVER['REMOTE_HOST'] variables.

Or You can also try:

From CLI

PHP < 5.3.0

$myIp= getHostByName(php_uname('n'));
 echo $myIp;

PHP >= 5.3.0

$myIp = getHostByName(getHostName());
echo $myIp;

For Client IP Address you can follow the below link: Source: Get the client IP address using PHP

Community
  • 1
  • 1