• 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
  • VBA
  • 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.  Remove HTML Tags from String in php
  • 2.  php unset – How to Destroy Variables in php
  • 3.  php sinh – Find Hyperbolic Sine of Number Using php sinh() Function
  • 4.  Remove First Character from String Using php
  • 5.  php is_array() Function – Check if Variable is Array in php
  • 6.  php Rename File – Renaming Files and Directories Easily
  • 7.  php print_r() Function- Get Information about Variables in php
  • 8.  echo php – Output One or More Strings
  • 9.  php trim() Function – Remove Whitespace at Beginning and End of String
  • 10.  php preg_match – Find Pattern in String Using Regex (Regular Expression)

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy