Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-21-2012, 02:10 PM   PM User | #1
sssunny
New Coder

 
Join Date: Jul 2012
Posts: 18
Thanks: 4
Thanked 0 Times in 0 Posts
sssunny is an unknown quantity at this point
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

?>
sssunny is offline   Reply With Quote
Old 12-21-2012, 02:55 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
sssunny (12-23-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:20 AM.


Advertisement
Log in to turn off these ads.