请注意有关$ _SERVER [' HTTP_X_FORWARDED_FOR']的消息

时间:2014-05-21 16:50:29

标签: php

我有自编的CMS,它不会返回正确的用户IP地址,如下所示:

define("USER_IP", $_SERVER['HTTP_X_FORWARDED_FOR']); 

它给了我以下通知:

Notice: Undefined index: HTTP_X_FORWARDED_FOR in /var/www/p00000/data/www/domain.ru/core/maincore.php on line 30

和第20行=到行定义(" USER_IP" ...

一些问题:

  • 我该怎么做才能摆脱这条通知消息?
  • 如何正确获取用户的IP地址?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
   define("USER_IP", $_SERVER['HTTP_X_FORWARDED_FOR']); 
} else {
  define("USER_IP", $_SERVER['REMOTE_ADDR']); 
 ...