Search code examples
phpuser-agenterror-log

PHP Notice: Undefined index: HTTP_USER_AGENT in line 2


I have added a 3 line code at the top of the index page to eliminate the bad request which occurs in imac devices. The code is given below:

<?php
if (preg_match ("/CaptiveNetworkSupport/", $_SERVER["HTTP_USER_AGENT"])) {
header ("HTTP/1.0 400 Bad Request");
exit ();
}
?>

But I can see lots of "PHP Notice: Undefined index: HTTP_USER_AGENT in " errors in the error_log. How can I remove these logs?

Thanks!


Solution

  • Maybe if you test if the index exists before the preg_match:

        <?php
            if ( isset($_SERVER["HTTP_USER_AGENT"]) && preg_match ("/CaptiveNetworkSupport/", $_SERVER["HTTP_USER_AGENT"]) )
            {
                header ("HTTP/1.0 400 Bad Request");
                exit ();
            }
        ?>