programming

How To Get Remote IP Address in PHP

By Hamdy Gamal

June 08, 2021

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

Output

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

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

‘‘ What revision of the CGI specification the server is using

‘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

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

‘‘ 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.

‘‘ 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‘.

‘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’

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)

Excuting Function

 

Documentations getenv  $_SERVER