TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

How to Get The IP Address in PHP

How to Get The IP Address in PHP with examples, php file, php session, php date, php array, php form, functions, time, xml, ajax, php mysql, regex, string, oop, chop(), bin2hex(), addslashes(), addcslashes() etc.

<< Back to HOW

How to get the IP address in PHP?

Many times we need to get the IP address of the visitor for different purposes. It is very easy to collect the IP address in PHP. PHP provides PHP $_SERVER variable to get the user IP address easily. We can track the activities of the visitor on the website for the security purpose, or we can know that who uses my website and many more.

The simplest way to collect the visitor IP address in PHP is the REMOTE_ADDR. Pass the 'REMOTE_ADDR' in PHP $_SERVER variable. It will return the IP address of the visitor who is currently viewing the webpage.

Note: We can display this IP address on the webpage and also even can store in database for many other purposes such as - for security, redirecting a visitor to another site, blocking/banning the visitor.

Get the IP address of the website

$_SERVER['REMOTE_ADDR'] - It returns the IP address of the user currently visiting the webpage.

For example

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

Output

User IP Address - ::1

But sometimes the REMOTE_ADDR does not return the IP address of the client, and the main reason behind is to use the proxy. In such type of situation, we will try another way to get the real IP address of the user in PHP.

<?php
	function getIPAddress() {
	//whether ip is from the share internet
   	 if(!empty($_SERVER['HTTP_CLIENT_IP'])) {
        		$ip = $_SERVER['HTTP_CLIENT_IP'];
    	}
	//whether ip is from the proxy
	elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        		$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
   	 }
//whether ip is from the remote address
	else{
       		 $ip = $_SERVER['REMOTE_ADDR'];
   	 }
   	 return $ip;
}
$ip = getIPAddress();
echo 'User Real IP Address - '.$ip;
?>

Output

User IP Address - ::1

Flowchart:

The flowchart for the above program will be like given below.

How to get the IP address in PHP

Get the IP address of the website

We can also get the IP address of any website by its URL. Pass the URL of the website inside gethostbyname() function.

For example

<?php
$ip_address = gethostbyname("www.google.com");
echo "IP Address of Google is - ".$ip_address;
echo "</br>";
$ip_address = gethostbyname("www.TheDeveloperBlog.com");
echo "IP Address of javaTpoint is - ".$ip_address;
?>

Output

IP Address of Google is - 172.217.166.4
IP Address of javaTpoint is - 95.216.57.234





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf