ShashankGaurav Posted May 25, 2014 Share Posted May 25, 2014 <?php function ipchecker() { $http_client = $_SERVER['HTTP_CLIENT_IP']; $http_x_forwarderd_for = $_SERVER['HTTP_X_FORWARDED_FOR']; $http_remote_addr= $_SERVER['REMOTE_ADDR']; if (!empty($http_client)) { $ip_address=$http_client; } else if (!empty($http_x_forwarderd_for)) { $ip_address=$http_x_forwarderd_for; } else if (!empty($http_remote_addr)) { $ip_address= $http_remote_addr; } return $ip_address; } ?> Link to comment https://forums.phpfreaks.com/topic/288757-i-am-getting-undefined-index-error-when-i-am-trying-to-echo-out-the-functionhelp-me-i-am-a-php-newbie/ Share on other sites More sharing options...
Jacques1 Posted May 25, 2014 Share Posted May 25, 2014 Hi, you cannot do the empty() check after you've tried to assign the array entry to a variable, because it's already too late then. The index error happens right when you access a nonexistent entry. So you need to wrap the array expression itself into empty(): if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip_address = $_SERVER['HTTP_CLIENT_IP']; } Note that !empty($var) generally makes no sense if $var has been defined previously. This expression checks whether the variable exists and is truthy. Since it does exist, you can jump straight to the truthiness check: if ($var) { ... } Link to comment https://forums.phpfreaks.com/topic/288757-i-am-getting-undefined-index-error-when-i-am-trying-to-echo-out-the-functionhelp-me-i-am-a-php-newbie/#findComment-1480774 Share on other sites More sharing options...
ShashankGaurav Posted May 25, 2014 Author Share Posted May 25, 2014 Thanx for the help Link to comment https://forums.phpfreaks.com/topic/288757-i-am-getting-undefined-index-error-when-i-am-trying-to-echo-out-the-functionhelp-me-i-am-a-php-newbie/#findComment-1480782 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.