简体   繁体   English

未定义索引:WordPress中的HTTP_HOST

[英]Undefined index: HTTP_HOST in wordpress

I am getting in my logs the following error from wordpress: "Undefined index: HTTP_HOST". 我在日志中从wordpress中收到以下错误:“未定义索引:HTTP_HOST”。 Of course, the file is requesting $_SERVER['HTTP_HOST'], but how can this be seeing as website is accessed via Apache? 当然,文件正在请求$ _SERVER ['HTTP_HOST'],但是当通过Apache访问网站时如何看待呢? I've verified via var_dump that this var exists and is set. 我已经通过var_dump验证了此var存在并已设置。

I want to clear this error for my logs and also to find out how this could be possible. 我想清除我的日志中的错误,并找出可能的情况。 I don't have a wordpress CLI cron, so what could this be? 我没有wordpress CLI cron,这可能是什么? I would point out that this is a random request that generated this error. 我要指出的是,这是一个产生此错误的随机请求。 It does not occur when I browse the blog. 当我浏览博客时,它不会发生。

Here is the call stack (... = /): Call Stack: 这是调用堆栈(... = /):调用堆栈:

0) 17,
1) ... var... www2... wordpress... wp-blog-header.php 16, 
2) ... var... www2... wordpress... wp-includes... template-loader.php 74, 
3) ... var... www2... wordpress... wp-content... themes... oneengine... page.php 1, 
4) get_header 45, 
5) locate_template 477, 
6) load_template 501, 
7) ... var... www2... wordpress... wp-content... themes... oneengine... header.php 134, 
8) wp_nav_menu 328, 
9) _wp_menu_item_classes_by_context 549

An illegal request might not include it, so it's always possible. 非法请求可能不包含该请求,因此它始终是可能的。

best way to prevent is to check if it exists before using, as with every variable. 最好的预防方法是像使用每个变量一样,在使用前检查它是否存在。

<?php
if (array_key_exists('HTTP_HOST', $_SERVER) {
  //use $_SERVER['HTTP_HOST']
}
?>

Or, since it is an illegal request, you could block it. 或者,由于这是非法请求,您可以将其阻止。

<?php
if (!array_key_exists('HTTP_HOST', $_SERVER) {
    if (!headers_sent()) {
        $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
        header($protocol . ' 404 Not Found');
    }

    echo '<h1>Not Found</h1>';
    exit;
}
?>

It would seem that maybe this is due to a false request. 似乎这可能是由于错误的请求。 If the client does not send this header, then it does not exist on the server as well, even though it is apache. 如果客户端不发送此标头,则即使它是apache,它在服务器上也不存在。 Must be from script bots. 必须来自脚本机器人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM