Open In App

How to identify server IP address in PHP ?

Last Updated : 01 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

What is an IP Address ? IP Address or Internet Protocol Address is a numerical value assigned to every device on network which uses the Internet Protocol for Communication.
An IP address serves two major functions:

  • Network/Host interface identification
  • Location addressing

Static IP addresses that do not change very often that are provided to servers. The ISP provides a unique IP address to a home machine that is dialing via modem, and the IP address is unique for that session and it may change the next time for the machine.

How to identify your server’s IP address: The $_SERVER is an array in PHP that contains the information regarding the headers, paths and script locations. The web server itself creates the entries of this array. Though it does not guarantee every web server will provide the contents of these arrays, servers may usually omit some of the $_SERVER array contents.
In order to obtain the IP address of the server one can use [‘SERVER_ADDR’], it returns the IP address of the server under the current script is executing.

Another method is using the [‘REMOTE_ADDR’] in the $_SERVER array. [‘REMOTE_ADDR’] is only used for getting the IP address for the local servers although the output produced will be the same as using [‘SERVER_ADDR’] for the local server IP address.

Example 1: This example identify the servers IP address using [‘SERVER_ADDR’].




<?php
  
// PHP program to obtain IP address of
// the server
  
// Creating a variable to store the
// server address
$ip_server = $_SERVER['SERVER_ADDR'];
  
// Printing the stored address
echo "Server IP Address is: $ip_server";
  
?>


Output:

Server IP Address is: ::1

Example 2: This example identify the servers IP address using [‘REMOTE_ADDR’].




<?php
  
// PHP program to obtain IP address of
// the server
  
// Create a variable to store the
// server ip address
$ip = $_SERVER['REMOTE_ADDR'];
  
// Printing the stored address
echo "IP Address is: $ip", "<br>";
  
?>


Output:

Server IP Address is: ::1

Note: If you try to run the above code on any online IDE it would either return a runtime error or no output, as private domains don’t share their IP, try running on localhost or a server. For localhost, if ipv4 loopback address is used then it will give 127.0.0.1 and if ipv6 loopback address is used then it will give ::1.

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.



Similar Reads

How to get Client IP address and MAC address in Laravel ?
What is a MAC address?MAC is the abbreviation of “Media Access Control” and it is a 48-bit physical address associated with every networking device. It is printed on the NIC (Network Interface Card) and is globally unique for every networking device. MAC address is used by the data-link layer to route a data packet from source to destination. How t
2 min read
Node.js server.address() Method
The server.address() is an inbuilt application programming interface of class Socket within tls module which is used to get the bound address of the server. Syntax: const server.address() Parameters: This method does not accept any parameter. Return Value: This method returns the bound address containing the family name, and port of the server. How
3 min read
How to uniquely identify computers visiting web site in JavaScript?
If you want to identify computers visiting your website, you will have to store the cookies of your website on the visiting computers. Cookies: In simple words, HTTP cookies are small notes which store the user's preferences about a particular website. For example, when a user shops online without logging in, he may put some items in the cart. Even
4 min read
How to identify which element scroll is being used using JavaScript ?
Given many elements inside the DOM, the task is to find out which element is being currently scrolled using JavaScript. Approach: This problem can be easily solved using JavaScript. We will add the 'scroll' event listener to all the required elements. The scroll event is fired whenever a particular element is being scrolled. Thus, we can easily fin
3 min read
How to identify unused CSS definitions from multiple CSS files in a project ?
In this article, we will learn how to identify unused CSS definitions from multiple CSS files in a project. What are unused CSS definitions? Unused CSS definitions are the stylesheets that are completely useless for rendering and loading the page. If you remove these unused CSS definitions then nothing will happen to your webpage. Why you should re
2 min read
Difference between Apache Tomcat server and Apache web server
Apache Tomcat server: Apache Tomcat is a web container. It allows the users to run Servlet and JAVA Server Pages that are based on the web-applications. It can be used as the HTTP server. The performance of the Tomcat server is not as good as the designated web server. It can be used as separate product with its own internal Web-server. It can also
2 min read
Difference between Web Server and Mail Server
Web Server : Web Server, as the name suggests, is a server software where web content is stored and uses HTTP protocol and other protocols to respond to user requests that is being made over WWW. Its main function is to display the content of the website through storing, processing, and delivering webpages within the website to users. Example: Apac
4 min read
How to install and configure Apache Web Server on Godaddy Server?
GoDaddy VPS is a shared server that provides computational services, databases, storage space, automated weekly backups, 99% uptime, and much more. It’s a cheaper alternative to some other popular cloud-based services such as AWS, GPC, and Azure. Apache HTTP Server is an open-source web server software widely used by web hosting companies to provid
2 min read
Server Side Rendering vs Client Side Rendering vs Server Side Generation
In the world of web development, there are several approaches to rendering web pages: server-side rendering, client-side rendering, and server-side generation. Each approach has its own advantages and disadvantages, and choosing the right one for your project depends on your specific needs and goals. In this blog, we’ll explore the differences betw
4 min read
Difference Between Web server and Application server
A server is a central repository where information and computer programs are held and accessed by the programmer within the network. Web server and Application server are kinds of the server which employed to deliver sites and therefore the latter deals with application operations performed between users and back-end business applications of the or
2 min read