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; } ?> Quote 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) { ... } Quote 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 Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.