serbanghita / Mobile-Detect

Mobile_Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.

Home Page:http://mobiledetect.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Need advice on `No valid user-agent has been set` PHP Fatal error

trendoman opened this issue · comments

A server randomly receives requests that have no user-agent, mostly from similar IPs (VPN?) e.g.

    [USER] => xxx
    [HOME] => /home/xxx
    [SCRIPT_NAME] => /index.php
    [REQUEST_URI] => /
    [QUERY_STRING] => 
    [REQUEST_METHOD] => GET
    [SERVER_PROTOCOL] => HTTP/1.1
    [GATEWAY_INTERFACE] => CGI/1.1
    [REMOTE_PORT] => 57950
    [SCRIPT_FILENAME] => /home/xxx/public_html/index.php
    [SERVER_ADMIN] => admin@xxx.xx
    [CONTEXT_DOCUMENT_ROOT] => /home/xxx/public_html
    [CONTEXT_PREFIX] => 
    [REQUEST_SCHEME] => https
    [DOCUMENT_ROOT] => /home/xxx/public_html
    [REMOTE_ADDR] => 198.235.24.130
    [SERVER_PORT] => 443
    [SERVER_ADDR] => xx.xx.xx.xx
    [SERVER_NAME] => www.xxx.xx
    [SERVER_SOFTWARE] => Apache
    [SERVER_SIGNATURE] => 
    [PATH] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
    [HTTP_HOST] => www.xxx.xx
    [HTTPS] => on
    [SCRIPT_URI] => https://www.xxx.xx/
    [SCRIPT_URL] => /
    [UNIQUE_ID] => xxx-xx
    [FCGI_ROLE] => RESPONDER
    [PHP_SELF] => /index.php
    [REQUEST_TIME_FLOAT] => 1699157949.7019
    [REQUEST_TIME] => 1699157949

What is the proper reaction to such situations - handle exceptions, fill-in a user agent before instantiating MD or else? Please advise. Thanks a lot! (I have masked a few fields, but did not delete any rows) It's as is from print_r($_SERVER, true) output.

same here

I am reading a few postings on Stack that basically revolve around fact that absent user-agent (not empty string, but completely missing one) is a valid request.
https://stackoverflow.com/questions/24274789/is-lack-of-user-agent-in-http-request-valid

A posting reported that visitors with a missing UA successfulle performed purchases on website (possibly this is a disinformation lol)
https://stackoverflow.com/questions/71527200/how-am-i-getting-users-with-no-user-agent-string

So, meanwhile I am going to try setting it explicitly to an empty string e.g.

if( false === $MB->hasUserAgent() ){ $MB->setUserAgent(''); }

As of https://github.com/serbanghita/Mobile-Detect/releases/tag/4.8.04 the rule is that if there is no User-Agent provided (even after we tried to auto-initialize from known $_SERVER['...'] variables, this will throw an exception, because it's actually an exceptional situation and you don't want to go on with the processing.

The recommendation is the following:

$detect = new MobileDetect();

$isMobile = false;
try {
    $isMobile = $detect->isMobile();
} catch (\Detection\Exception\MobileDetectException $e) {
  // do nothing, or log this to your error logs.
}

var_dump($isMobile);