• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / How to Get Current System IP Address in php

How to Get Current System IP Address in php

April 4, 2022 Leave a Comment

To get the current system IP address in php, we can access the ‘REMOTE_ADDR’ key from the $_SERVER super global variable.

$ip_address = $_SERVER['REMOTE_ADDR'];

If the client is using a proxy or accessing your page with shared internet, you use the ‘HTTP_X_FORWARDED_FOR’ and ‘HTTP_CLIENT_IP’ keys respectively.

$ip_address_proxy = $_SERVER['HTTP_X_FORWARDED_FOR'];

$ip_address_shared_internet = $_SERVER['HTTP_CLIENT_IP'];

If you are looking to get the IP address of a particular website, you can do that with the php gethostbyname() function.

$ip_address = gethostbyname("www.google.com");

When designing web pages, many times we need to get the IP address of a visitor for various reasons (logging, targeting, redirecting, etc.)

To get the IP address of a user, we can use the $_SERVER super global variable.

The $_SERVER super global variable has many different values which can be accessed in our program.

To access the IP address of the user, we read the ‘REMOTE_ADDR’ field from $_SERVER.

Below is a simple example of how to retrieve the IP address from a user.

$ip_address = $_SERVER['REMOTE_ADDR'];

How to Get the IP Address of a User Behind a Proxy in php

If the user is using a proxy and accessing your website, then you will need to use a different key to get the IP Address.

To get the IP address of a client using a proxy, you should access the ‘HTTP_X_FORWARDED_FOR’ key in the $_SERVER super global variable.

$ip_address_proxy = $_SERVER['HTTP_X_FORWARDED_FOR'];

If the user is using shared internet, then it’s possible that you will have to check the ‘HTTP_CLIENT_IP’ key in the $_SERVER variable.

$ip_address_shared_internet = $_SERVER['HTTP_CLIENT_IP'];

A full function which will check for all of these cases is below.

function getIPAddress(){
    if(!empty($_SERVER['HTTP_CLIENT_IP'])){
        $ip_address = $_SERVER['HTTP_CLIENT_IP'];
    } elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip_address = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

How to Get the IP Address of a Website in php

Every website has an IP address. If we need to get the IP address of a particular website, then we can use the php gethostbyname() function.

Pass the URL of the website to gethostbyname() and you will get the IP address of that website.

Below is an example of getting the IP address of www.google.com in php.

$ip_address = gethostbyname("www.google.com");

Hopefully this article has been useful for you to learn how to get IP addresses using php.

Other Articles You'll Also Like:

  • 1.  php strlen() – Get the Length of String Variable in php
  • 2.  php rtrim() Function – Remove Whitespace at End of String
  • 3.  Replace Space with Dash in php String
  • 4.  php cos – Find Cosine of Number in Radians Using php cos() Function
  • 5.  Remove HTML Tags from String in php
  • 6.  php asin – Find Arcsine and Inverse Sine of Number Using asin() Function
  • 7.  php atanh – Find Hyperbolic Arctangent of Number Using atanh() Function
  • 8.  php tanh – Find Hyperbolic Tangent of Number Using tanh() Function
  • 9.  php array_key_exists() Function – Check if Key Exists in Array
  • 10.  Capitalize First Letter of String with php ucfirst() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x