Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    getting Undefined index error in php code.

    Notice: Undefined index: HTTP_CLIENT_IP in C:\xampp\htdocs\newpractice 1\video66.php on line 3

    Notice: Undefined index: HTTP_X_FORWARDED_FOR in C:\xampp\htdocs\newpractice 1\video66.php on line 4
    127.0.0.1

    <?php

    $http_client_ip= $_SERVER ['HTTP_CLIENT_IP'];
    $http_x_forwarded_for= $_SERVER ['HTTP_X_FORWARDED_FOR'];
    $remote_addr=$_SERVER['REMOTE_ADDR'];

    if(!empty($http_client_ip))
    {
    $ip_address=$http_client_ip;
    }

    else if(!empty($http_x_forwarded_for))
    {
    $ip_address=$http_x_forwarded_for;
    }

    else
    {
    $ip_address=$remote_addr;
    }
    echo $ip_address

    ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You need to use isset checks. Your server will guarantee nothing when it comes to the population of the $_SERVER superglobal, so you need to assume that nothing exists.
    PHP Code:
    if (isset($_SERVER['HTTP_CLIENT_IP']))
    {
        
    $ip_address $_SERVER['HTTP_CLIENT_IP'];
    }
    else if (isset(
    $_SERVER['HTTP_X_FORWARDED_FOR']))
    {
        
    $ip_address $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else if (
    $_SERVER['REMOTE_ADDR'])
    {
        
    $ip_address $_SERVER['REMOTE_ADDR'];

    Should do it.
    Although it makes no guarantee, I've never seen the REMOTE_ADDR not set in a webserver environment before.

  • Users who have thanked Fou-Lu for this post:

    sssunny (12-23-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •