Hide parts of an IP

Discussion in 'PHP' started by viperxx, Jun 26, 2007.

  1. #1
    my website currently display comments made from guests and have their username as their IP using <?php echo $_SERVER['REMOTE_ADDR']; ?>
    this shows their complete IP, how do i make it so it shows something like 71.XX.XXX.432
     
    viperxx, Jun 26, 2007 IP
  2. KalvinB

    KalvinB Peon

    Messages:
    2,787
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could explode the IP string on the periods so you get an array with four entries. You then display $ip[0] . ".xxx.xxx." . $ip[3]
     
    KalvinB, Jun 26, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    
    function hide_ip($ipaddress)
    {
    	return preg_replace(
    		'/\.(\d{1,3})\.(\d{1,3})\./e',
    		'"." . str_repeat("X", strlen("$1")) . "." . str_repeat("X", strlen("$2")) . "."',
    		$ipaddress
    	);
    }
    
    PHP:
    Usage example:
    
    echo hide_ip('192.168.1.2'); // Prints: 192.XXX.X.2
    
    PHP:
     
    nico_swd, Jun 26, 2007 IP