PHP script to Find out IP address

IP address in PHP We can get the Internet Protocol (IP) address of any visitor by using PHP. Finding the IP address is very important requirement for many scripts where we store the members or visitors details. For security reason we can store IP address of our visitors who are doing any purchases or doing any type of transaction online. We can using the IP address to find out geographical location of the visitor. Some time based on the IP address we can redirect the browser to different areas of the site.

What is my IP address ?

Here is the code to display your ip address.
echo "IP address= $ip";
The above code will display this IP address=

Geographical location using IP address

Submit the above form to see your physical location identified by using your IP address. Any other IP address can be used to get its geographical location.

Proxy Server Address

Some visitors browse from behind proxy servers. We can collect the address of proxy along with the IP address of client by using this code. However this address can easily be manipulated hence while logging it is better to store both the address of the visitor.
echo "Proxy address=$ip2";
Proxy address=Not found

Other Server variables

$_SERVER is an array and it contains lot of information as supplied by server and one of the element is 'REMOTE_ADDR' which gives us the IP address. Here is another example of getting the server name using $_SERVER
You can display all other elements of Array by using display elements of array. Here is a sample code.
while (list ($key, $val) = each ($_SERVER)) { 
echo "$key -> $val <br> ";

Dynamic IP and Static IP Address

Each time we connect to internet through our Internet Service Provider ( ISP ) we get one IP address from a pool of IP address available to our ISP. This pool of IP address were assigned to your ISP by Internet Assigned Numbers Authority ( IANA ) a department of  Internet Corporation for Assigned Names and Numbers (ICANN )
So your IP address keep changing from time to time
It is like using a hired vehicle each day. Your cab operator will give you a vehicle each time you ask with a different number plate.

Domain Name System ( DNS)

If someone wants to know what is the vehicle your are using today then he has to ask your cab operator to know this. After knowing this details he can contact you. This is the job your Domain Name system (DNS) server does all the time. It maintains a list of websites with matching IP address. When we enter the website name at address bar of web browser, it connects to DNS ( server ) and ask for corresponding IP address. When ever such request for a web site comes, your DNS server at your ISP gets the corresponding IP address and connects your browser to that IP address to reach the website.

What happens when the IP address is not available at our ISP DNS? It search for matching record in high level ( root ) DNS and these information gets updated time to time

You can ask your ISP to provide static IP address at an extra cost.

Static IP address

It is like having an exclusive vehicle for your own use all the time so your vehicle number plate does not change. You will have one fix IP address each time you connect to internet. This is required when you have some specific application like VOIP ( Voice over IP ), data centre, web hosting, Virtual Private Network ( VPN) etc.

Web Hosting

We must use a fix IP address to have web hosting services. But a single IP address can be shared by more than one site.

Storing IP address in MySQL database table

After collecting the IP address we can store in mysql table. Along with IP address we can store referrer , browser details etc.
We can convert the dot formatted IP address to number by using ip2long function and store in MySQL database.
This is IPV4 address and for next generation IPV6 address we have to change the field length of our database table

IP address of google crawler

This page reads the IP address of yours or any other visitors of this page. Google crawler (spider or googlebot ) when visit this page to index then its IP address is also shown to it. We can find out what the crawler has stored by visiting cached version of this page.

In google search box enter this line.
scroll down and see the IP address of google bot.

PHP How to get IP address using Python ?

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net



    I need a script similar to that of the one at


    nice.. can you show how to get the full details for the network addressed including the gateway and sub-net mask? thank you


    You cannot find out somone's subnet mask, or gateway externally. Both are part of their internal network, all of which shares the same external IP.


    To John - Thanks for that info, now go and make it. Anto, type ipconfig in msdos or as is probably more apt.....winipcfg in START then RUN.
    ajay joshi


    thanx.. can you show how to get the full details for ip tracing/ip trapping


    You say, "recording the geographical location of the visitor can be done this way" How can I get the user's geographical location to redirect the user to the correct info for their region?


    Once IP address is known then the location can be collected by using available geo location tables. These tables are available at cost and can be updated regularly.


    can you show how to get the full details for the network addressed


    how to run the above code?...
    Pham Nguyen


    why do i use the code $ip=@$REMOTE_ADDR; on localhost. $ip= Please lest me know how about on the host server?


    i want to design cyberoam through please tell me some guideline
    PHP Developer


    Looking to find out how to look up who owns the IP and what the IP owner's address is. Thanks!
    kiran malvi


    This ip function are most usefull for anyone. so thanks.
    Mahendra Rajgude


    Really useful info. Thanks.
    Mutahir shah


    Please someone could provide me how to conver IP address to address where the user is loged in.


    Thanks worked.


    I have 14 tracts that I want the visitors to my website to be able to send to their friends. Please can you let me have a script attached to be bottom of the page telling visitors to send to their friends.


    I am trying to determine where to place this piece of code in my .php file. The page has html and php code inside of it. I would like to track IP addresses and have them stored to my .csv file, which I already have working with fwrite.


    how to fetch the ip address of user filling a form in php sites? I want the exact coding please let me know about this....


    Why those code shows different ips in differnt time.. Even i am visiting this page with same network and same phone and even same browser? Plz answer me.


    Your ISP assign you different IP each time you start connecting to internet. This is known as dynamic IP. In contrast if you take static IP from your ISP , they will charge more for this but you can have your own services like VOIP, web hosting etc.


    i want to know the ip address of the computer, who chat with me from another network ?


    how to fetch permanent IP address of a computer..?? is it possible..??


    you can also use a ip geo-location pluggin like the one in bricksett cms. it takes users ip address and then gives you the country code.


    thank you...
    it's short and useful.


    Can you help me i want full script can you send me my email address please ( thank you for helping me )


    Hi i was using above ip program, but my result arrived this format :IP address= ::1


    If you can run PHP script , then you can run this code, there is no extra code required.

    Post your comments , suggestion , error , requirements etc here

    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer