All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Get Server IP Address In PHP

Last Updated : Mar 11, 2024

Get Server IP Address 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;
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. 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.
  3. 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.
  4. At the right hand side we specified ‘ $_SERVER['SERVER_ADDR'] ’ to assign to variable ‘$serip’.
  5. 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.
  6. For ['SERVER_ADDR'], you can use ['REMOTE_ADDR'] parameter in this program this will also give you right server IP address.
  7. 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.
  8. In this same way, you can also retrieve users IP address who all visits your website. Later we will this topic in upcoming tutorial.
  9. Instead of echo() you can prefer print() method if you want and that not going to affects your result.

Conclusion :-

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.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪