Skip to content

Instantly share code, notes, and snippets.

@viruthagiri
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save viruthagiri/002a90f291e5c4ef3435 to your computer and use it in GitHub Desktop.

Select an option

Save viruthagiri/002a90f291e5c4ef3435 to your computer and use it in GitHub Desktop.

Revisions

  1. @ChrisWiegman ChrisWiegman created this gist May 2, 2014.
    34 changes: 34 additions & 0 deletions get_ip.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    <?php

    function get_ip() {

    //Just get the headers if we can or else use the SERVER global
    if ( function_exists( 'apache_request_headers' ) ) {

    $headers = apache_request_headers();

    } else {

    $headers = $_SERVER;

    }

    //Get the forwarded IP if it exists
    if ( array_key_exists( 'X-Forwarded-For', $headers ) && filter_var( $headers['X-Forwarded-For'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ) {

    $the_ip = $headers['X-Forwarded-For'];

    } elseif ( array_key_exists( 'HTTP_X_FORWARDED_FOR', $headers ) && filter_var( $headers['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 )
    ) {

    $the_ip = $headers['HTTP_X_FORWARDED_FOR'];

    } else {

    $the_ip = filter_var( $_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 );

    }

    return $the_ip;

    }