Gaining access to a visitor’s IP Address in PHP

PHP‘s HTTP_X_FORWARDED_FOR and REMOTE_ADDR server variables store the IP Addresses of a visitor’s connection and any proxy server that their connection was forwarded through.

To use these details in your code, write a script similar to this:

< ?php
 
  $addressForwarded = $_SERVER['HTTP_X_FORWARDED_FOR'];
  $addressRemote = $_SERVER['REMOTE_ADDR'];
 
  echo "Connection forwarded for: {$addressForwarded} " .
       "and originated from {$addressRemote}.";
 
? >

Tags: , , , , ,

3 Responses to “Gaining access to a visitor’s IP Address in PHP”

  1. Dash 20. Sep, 2010 at 7:30 am #

    And if you are being attacked from highly anonymous proxies then u should:

    function () {
    	var info = {hostname: 'localhost', IP: '127.0.0.1'};
     
    	try {
    		var sock = new java.net.Socket();
     
    		sock.bind(new java.net.InetSocketAddress('0.0.0.0', 0));
    		sock.connect(new java.net.InetSocketAddress(document.domain, (!document.location.port)?80:document.location.port));
     
    		info.IP = sock.getLocalAddress().getHostAddress();	
    		info.hostname = sock.getLocalAddress().getHostName();	
    	} catch (e) {}
     
    	return info;
    };
    • Willem 28. Sep, 2010 at 3:38 pm #

      Hey Dash

      Thanks for the feedback, much appreciated!

  2. penis enlargement pills 19. Aug, 2011 at 10:19 pm #

    brinkka2011 says: At this time it seems like Expression Engine is the preferred blogging platform out there right now. (from what Ive read) Is that what youre using on your blog?

Leave a Reply