All TalkersCode Topics

Follow TalkersCode On Social Media

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

Get Client IP Address In PHP

Last Updated : Mar 11, 2024

Get Client IP Address In PHP

In this article we will show you the solution of get client IP address in PHP, if you are haunting result for procedure to retrieve client IP address then this blog help you efficiently.

To achieve the result of this topic you need to use REMOTE_ADDR command.

This is major thing to retrieve IP address of client or visiting users IP address.

Here getenv() method helps and allows us to collect values of environment variable in PHP.

Below given example program show you how to implement and target result.

Step By Step Guide On Get Client IP Address In PHP :-

You need to create a variable to store retrieved IP address and at right hand side you should pass REMOTE_ADDR to getenv().

Here REMOTE_ADDR helps widely to collect visiting users IP address who viewed your website and their activities.

It act as security purpose and watch viewer’s action on particular website.

Using echo statement we displaying result of collected users IP address with some output text.

<?php
$cli_ip=getenv("REMOTE_ADDR");
echo "Client IP Address is: ". $cli_ip;
?>
  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. Firstly, we need to create a variable, so we created variable ‘$cli_ip’. If you want to modify or change defined name you can create variable in different name in the same way.
  4. To retrieve REMOTE_ADDR passed to getenv() function as per prebuilt definition. It successfully getting end user’s IP address and passed to left hand side present variable ‘$cli_ip’.
  5. To disclose got result to client we printed sentence ‘Client IP Address is:’ in echo statement. And using (. dot) attached result variable ‘$cli_ip’.
  6. Somehow, getenv() and $_SERVER returning same result at client side. You can verify that if you want to try this with another method. Both give you same result without fail, so utilize it properly.
  7. In real time use, we can track user’s activities that carry over on particular website. It consists of many data about end user like path, directory and name, later we will discuss them.
  8. Instead 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 client’s 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 print ‘Client IP Address is: 127. 1. 1. 0’ as output.

With this help of sample code you can test in several IP address to verify this program.

I hope this article on get client IP address in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪