Get Server IP Address In PHP
Last Updated : Jul 1, 2023
IN - PHP
In this article we will show you the solution of get server IP address in PHP, using php, you can collect both client and server IP address with a few lines of codes.
To achieve the result everyone should need to use $_SERVER variable.
It because it’s an array in PHP and web server creates entries of this array.
Along with this variable [‘SERVER_ADDR’] using this as parameter helps to accomplish this task.
Another imperative thing in this topic is [‘SERVER_ADDR’] it returns IP address of the server to end user.
Step By Step Guide On Get Server IP Address In PHP :-
In this program first we need to create a variable to hold getting IP address from server.
To retrieve server address as seen earlier you need to specify necessary parameter inside appropriate statement or array.
Instead of [‘SERVER_ADDR’] value you can another value that also leads you to get the same result at the end of execution.
To display to end users using echo statement retrieved result printed on client side page.
<?php $serip=$_SERVER['SERVER_ADDR']; echo "Server IP Address is: ". $serip; ?>
- A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
- The default file extension for php files is “.php” and php statements end with ‘;’ semicolon. In php we need to specify ‘$’ symbol before the variable name also default rule.
- As for results, we created variable $serip, here you can change this variable name as per your wish but keep in mind pre-defined rules for variable definition.
- At the right hand side we specified ‘ $_SERVER['SERVER_ADDR'] ’ to assign to variable ‘$serip’.
- It will return server IP address to left hand side present variable to showcase result we used echo statement to print sentence ‘Server IP Address is:’ with result.
- For ['SERVER_ADDR'], you can use ['REMOTE_ADDR'] parameter in this program this will also give you right server IP address.
- Do you know where ['SERVER_ADDR'] and ['REMOTE_ADDR'] differed? Answer is ['SERVER_ADDR'] returns currently executing scripts server IP and ['REMOTE_ADDR'] returns user IP address who currently visit this page.
- In this same way, you can also retrieve users IP address who all visits your website. Later we will this topic in upcoming tutorial.
- Instead of echo() you can prefer print() method if you want and that not going to affects your result.
In conclusion, hope now you know how to get server IP address using php.
When you work with php we need to create and process php files at server location and then we need to start the server before execute the program.
Otherwise sometimes you will get error so ensure it if you get any error while running output.
When we execute this program on browser it will show result as ‘Server IP Address is : :: 1’ output. With this help of sample code you can test any server IP address.
I hope this article on get server IP address in PHP helps you and the steps and method mentioned above are easy to follow and implement.