PHP Undefined index: HTTP_USER_AGENT

前端 未结 2 1792
忘了有多久
忘了有多久 2020-12-16 10:56

The following code validates the user agent accessing the site however I am getting the error. What do I need to update to accommodate scenarios where there is no user agent

2条回答
  •  青春惊慌失措
    2020-12-16 11:20

    use isset:

    if( !isset( $_SERVER['HTTP_USER_AGENT'])){
        $name = "none";
    }else{
         $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
    
        if (preg_match('/opera/', $userAgent)) {
            $name = 'opera';
        } [... yourcode ...]
    }
    

提交回复
热议问题