Posted By

o0110o on 10/20/12


php function ip address

Versions (?)

Who likes this?

1 person have marked this snippet as a favorite


A function to get the real IP address.

 / Published in: PHP

This function performs several checks to make sure it's getting the correct IP address.

  1. function get_ip(){
  2. if(!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } // Check if the IP is from a shared internet connection
  3. else if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } // Check if the IP is passed from a proxy
  4. else{ $ip = $_SERVER['REMOTE_ADDR']; } // The real IP address
  5. return $ip;
  6. }

Report this snippet  

You need to login to post a comment.