...

View Full Version : Undefined index errors



Thenewphp
09-13-2011, 05:57 PM
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

$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;

?>

myfayt
09-13-2011, 06:39 PM
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


if (!empty($http_client_ip)) {
$ip_adress = $http_client_ip;


should be


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

Thenewphp
09-13-2011, 07:00 PM
Not solve the problem

BluePanther
09-13-2011, 07:53 PM
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?

Inigoesdr
09-13-2011, 08:27 PM
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:

$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'] : '';

tangoforce
09-13-2011, 08:42 PM
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



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.

Thenewphp
09-13-2011, 09:18 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum