programming

How To Get Remote IP Address in PHP

Published

on

most of medium or large projects that companies planning to let their own system to know who’s trying access the project platform or website or any other online services for a security reasons needs to store each connection identity address for each failed attempt user logins to take an temporary action like “Block” or “Secure Accounts”

What’s Flowchart of this program and how it works

What’s Default Variables and Methods to call these layers
Example 1

$_SERVER is an super global variable containing information such as headers, paths, and script locations.
The entries in this array are created by the web server

‘$_SERVER’ is also like global variable has a couples of methods is responsible to get all network connection header information’s

<?php
echo 'User IP Address - '.$_SERVER['REMOTE_ADDR'];  
?>

Output

Advertisement
User IP Address - ::1

Things You Need to Know

Always The ‘REMOTE_ADDR’  in localhost does not return the IP address of the client, and the main reason behind is to use the proxy. such as type of situation, we will try another way to get the real IP address of the user in PHP.
if we’ve tried this on localhost environments such as “XAMPP” or “MAMP” or “APPSERV” or any other apache software’s we will get this result and if we’ve tried to upload this code online host and entered the website this will return with the real IP Address so we should test that online

 

Variable Methods or Indices

PHP_SELF
The filename of the currently executing script, relative to the document root

GATEWAY_INTERFACE
What revision of the CGI specification the server is using

Advertisement

‘SERVER_ADDR’
The IP address of the server under which the current script is executing.

‘SERVER_NAME’
The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host.

‘SERVER_SOFTWARE’
Server identification string, given in the headers when responding to requests.

‘SERVER_PROTOCOL’
Name and revision of the information protocol via which the page was requested

Advertisement

‘REQUEST_METHOD’
Which request method was used to access the page such as  ‘GET‘, ‘HEAD‘, ‘POST‘, ‘PUT‘.

REQUEST_TIME
The timestamp of the start of the request.

‘REQUEST_TIME_FLOAT’
The timestamp of the start of the request, with microsecond precision.

‘QUERY_STRING’
The query string, if any, via which the page was accessed.

Advertisement

DOCUMENT_ROOT
The document root directory under which the current script is executing, as defined in the server’s configuration file.

‘HTTP_ACCEPT’
Contents of the Accept: header from the current request, if there is one.

‘HTTP_ACCEPT_CHARSET’
Contents of the Accept-Charset: header from the current request, if there is one. Example: ‘iso-8859-1,*,utf-8‘.

‘HTTP_ACCEPT_ENCODING’
Contents of the Accept-Encoding: header from the current request, if there is one. Example: ‘gzip‘.

Advertisement

‘HTTP_ACCEPT_LANGUAGE’
Contents of the Accept-Language: header from the current request, if there is one. Example: ‘en‘.

‘HTTP_CONNECTION’
Contents of the Connection: header from the current request, if there is one. Example: ‘Keep-Alive‘.

‘HTTP_HOST’
Contents of the Host: header from the current request, if there is one.

‘HTTP_REFERER’

Advertisement

The address of the page if any which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify as a feature.

Learn More about indicates of $_SERVER  on PHP documentation

Creating The Function

Let’s getting started to create a simple function let us know who’s  on remote using getenv function

getenv is built-in function in PHP available for versions  (PHP 4, PHP 5, PHP 7, PHP 8)

Advertisement
<?php 
function get_client_ip() {
   $ipaddress = '';
   if (getenv('HTTP_CLIENT_IP'))
      $ipaddress = getenv('HTTP_CLIENT_IP');
   else if(getenv('HTTP_X_FORWARDED_FOR'))
      $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
   else if(getenv('HTTP_X_FORWARDED'))
      $ipaddress = getenv('HTTP_X_FORWARDED');
   else if(getenv('HTTP_FORWARDED_FOR'))
      $ipaddress = getenv('HTTP_FORWARDED_FOR');
   else if(getenv('HTTP_FORWARDED'))
      $ipaddress = getenv('HTTP_FORWARDED');
   else if(getenv('REMOTE_ADDR'))
      $ipaddress = getenv('REMOTE_ADDR');
   else
      $ipaddress = 'UNKNOWN';
   return $ipaddress;
}

?>

Excuting Function

<?php 
echo get_client_ip();
?>

 

Documentations
getenv 
$_SERVER 

 

Advertisement

 

Trending

Exit mobile version