Develop a “GetMyIP” like script within a minute

by Vineet Sharma on December 16, 2008

in PHP




You must have seen sites like GetMyIP or WhatismyIPaddress that allows you to view your Network’s IP address. Well, you must have wondered how these scripts are developed that shows the IP address of the system you are working on. I bring you a basic script that does the same thing of showing IP address.

An Internet Protocol (IP) address is a numerical identification (logical address) that is assigned to devices participating in a computer network utilizing the Internet Protocol for communication between its nodes. Although IP addresses are stored as binary numbers, they are usually displayed in human-readable notations,such as 208.77.188.166 (for IPv4), and 2001:db8:0:1234:0:567:1:1 (for IPv6). The role of the IP address has been characterized as follows: “A name indicates what we seek. An address indicates where it is. A route indicates how to get there.”[link]

<?php
//Get IP
echo '<br><div align="center"><font size = "4">';
printf("Your IP address is: %s", $_SERVER['REMOTE_ADDR']);
echo '</div></font>';
?>

It is pretty simple to understand: <?php?> are opening & closing tags that tells your web browser where the PHP script has begun & where it is ended. Then there is a comment. Actually the heart of the script is this code

printf("Your IP address is: %s", $_SERVER['REMOTE_ADDR']);

$_SERVER is an array that contains all the information related to the web server. Here I used the  REMOTE_ADDR parameter that shows the IP address from which the user is viewing the current page.

You can see this script in action here

Related:

Leave a Comment

Previous post:

Next post: