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.

6
  • 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. Sep 25, 2016 at 6: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. Sep 25, 2016 at 6: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. Sep 25, 2016 at 6:46
  • $localIPAddress = getHostByName(getHostName()); Try this one
    – Sampad
    Sep 25, 2016 at 6: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 :) Sep 25, 2016 at 10:33

1 Answer 1

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

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.