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 7 of 7
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Undefined index errors

    am getting this error:

    Notice: Undefined index: HTTP_CLIENT_IP in C:\xampp\htdocs\series\script\index.php on line 3

    Notice: Undefined index: HTTP_X_FORWARDED_FOR in C:\xampp\htdocs\series\script\index.php on line 4
    127.0.0.1

    With this code :

    PHP Code:
    <?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_adress $http_client_ip;
      
    } else if (!empty(
    $http_x_forwarded_for)) {
     
    $ip_address $http_x_forwarded_for;

    } else {
      
    $ip_address $remote_addr;
    }

    echo 
    $ip_address;

    ?>
    Last edited by Inigoesdr; 09-13-2011 at 09:48 PM. Reason: Useless title

  • #2
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,495
    Thanks
    72
    Thanked 105 Times in 104 Posts
    This may be helpful, and make sure your host supports PHP.


    In PHP 4.2.0 and later, the default value for the PHP directive register_globals is off. This is a major change in PHP. Having register_globals off affects the set of predefined variables available in the global scope. For example, to get DOCUMENT_ROOT you'll use $_SERVER['DOCUMENT_ROOT'] instead of $DOCUMENT_ROOT, or $_GET['id'] from the URL http://www.example.com/test.php?id=3 instead of $id, or $_ENV['HOME'] instead of $HOME.

    Using the available PHP Reserved Predefined Variables, like the superglobal arrays, is preferred.
    EDIT: by the way

    PHP Code:
    if (!empty($http_client_ip)) {
    $ip_adress $http_client_ip
    should be

    PHP Code:
    if (!empty($http_client_ip)) {
    $ip_address $http_client_ip

  • #3
    New Coder
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Not solve the problem

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    If you read the error, it's saying there's no such index as 'HTTP_CLIENT_IP' in the $_SERVER array. Which is true, there isn't. Dunno if there was maybe a value in the old HTTP_SERVER_VARS, which is depreciated, but there's certainly no index like that in any php version I've used. Where did you get these values from?

    Also, I don't see why you would use 3 checks for an IP address? $_SERVER['REMOTE_ADDR'] should hold the remote IP contained in the header.

    Basically, remove the parts looking for the unknown indexes and your script will work fine, but where did you get these values from?

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Anything using HTTP_* in the $_SERVER array is simply the headers that were sent with the request, and they are all optional. You can't depend that they are going to be there. Those two you mention are generally only used when there is a proxy between the user and the server. That said, you can change your code to this to resolve the notices:
    PHP Code:
    $http_client_ip = isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : '';
    $http_x_forwarded_for = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : ''

  • Users who have thanked Inigoesdr for this post:

    Thenewphp (09-13-2011)

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,421
    Thanks
    62
    Thanked 535 Times in 522 Posts
    Quote Originally Posted by Thenewphp View Post
    am getting this error:

    Notice: Undefined index: HTTP_CLIENT_IP in C:\xampp\htdocs\series\script\index.php on line 3

    Notice: Undefined index: HTTP_X_FORWARDED_FOR in C:\xampp\htdocs\series\script\index.php on line 4
    Quote Originally Posted by myfayt View Post
    This may be helpful, and make sure your host supports PHP.
    I think its fair to say that when PHP is outputting error messages, then yes the host does support PHP. Advising the op to check this will simply waste their time.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #7
    New Coder
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    the isset solve the problem by the way i got his code on a video tutorial the link is : http://www.thenewboston.com/?p=2863&pOpen=tutorial


  •  

    Posting Permissions

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