Is the $ _SERVER ['SERVER_ADDR'] parameter always set?

Is $_SERVER['SERVER_ADDR'] ?

Should I check with isset () or is this not necessary?

I need to get the IP address of the site so that I can find out if it is 127.0.0.1/localhost

+4
source share
4 answers

This will not always be asked. Note that you can install PHP even without a server and run it from the command line. There is no guarantee with any of the $_SERVER , but if you try it once on your server and it works, you can bet that it will always be installed on this server configuration. You just need to do something so that if you ever make major changes to your server configuration or switch servers, you should check it again.

You can also check the value of server variables with phpinfo()

+3
source

No, it is not installed in the CLI . Therefore, not always.

 $ php -r "echo $_SERVER['SERVER_ADDR'];" 

(no conclusion)

If you have errors registered or reported (based on your PHP.ini settings), you will also receive this message:

 PHP Notice: Undefined index: SERVER_ADDR in Command line code on line 1 
+3
source

CLI is a good example of when it is not installed, but all _SERVER values ​​are set by the server running php, so depending on the server you are using and its configuration, there is no guarantee that it will be the same.

+1
source

As previously stated through cli, it is not available. Just in case you need to know the IP address through both cli and through an HTTP call, consider using the following:

 $IP = isset($_SERVER['SERVER_ADDR'])?$_SERVER['SERVER_ADDR']:gethostbyname(gethostname()); 
0
source

All Articles