linux - 错误 : Undefined index: HTTP_X_FORWARDED_FOR

标签 linux cakephp

这段代码是我写的

    $ips = preg_split("/,/", $_SERVER["HTTP_X_FORWARDED_FOR"]);
    $ip = $ips[0];
    if ($key === $ip && $val === env('SERVER_ADDR')) {
        $addr = env($ip);
        if ($addr !== null) {
            $val = $addr;
        }
    }

但我收到以下错误:

<b>Notice</b>:  Undefined index: HTTP_X_FORWARDED_FOR 

最佳答案

只是不要在不确定数组键总是存在的情况下使用它们。 基本 PHP 一对一

if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
    // now only try to access this key
}

Cake 中的替代方法是使用旨在自动检查内部存在性的包装方法。然后你可以直接读取值。在你的情况下 env() 检查那些服务器变量:

$result = env('HTTP_X_FORWARDED_FOR');

关于linux - 错误 : Undefined index: HTTP_X_FORWARDED_FOR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16168680/

相关文章:

CakePHP Canonical标签和html helper

linux - 循环读取 bash 文件中的多个查询

c++ - PHP 的 is_dir() 的 C++ 等价物是什么?

validation - CakePHP 中嵌套所需的规则

database - cakephp 错误。连接到数据库...不是... : Access denied for user 'my_app' @'localhost' (using password: YES)

angularjs - 发生服务器端更新时,如何更新 AngularJS 中的 $scope?

c++ - 如何将 JSON 发布到 Web 服务器?

c - 哪个线程处理信号?

linux - Centos 7如何更新libtiff到最新版本?

cakephp - 在 CakePHP 中翻译月日组合