简体   繁体   中英

Undefined index: HTTP_HOST in wordpress

I am getting in my logs the following error from wordpress: "Undefined index: HTTP_HOST". Of course, the file is requesting $_SERVER['HTTP_HOST'], but how can this be seeing as website is accessed via Apache? I've verified via var_dump that this var exists and is set.

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? 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. Must be from script bots.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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