Code / PHP Snippets / Function: get_ip()

  1. <?
  2. function get_ip() {
  3.     /*
  4.         get_ip();
  5.  
  6.         Checks to see if an request is forwarded, ie by
  7.         proxy/cache and if so, returns the requesting IP
  8.         and not the IP of the proxy. Many, many people
  9.         just store the env superglobal REMOTE_ADDR which
  10.         often contains a proxy IP.
  11.  
  12.         Usage:        get_ip()
  13.         Returns:    Dotted Quad notation IPv4 IP
  14.     */
  15.     if ($_SERVER["HTTP_X_FORWARDED_FOR"]) {
  16.         /* The IP is forwarded, return this value */
  17.         return $_SERVER["HTTP_X_FORWARDED_FOR"];
  18.     } else {
  19.         return $_SERVER["REMOTE_ADDR"];
  20.     }
  21. }
  22. ?>

Download as Text : January 6th, 2009