Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for $_SERVER['HTTP_USER_AGENT'] to not be set?

I've just been looking through a website's error_log and one of the error's that has been logged a few times is:

[21-Jun-2011 12:24:03] PHP Notice: Undefined index: HTTP_USER_AGENT in /home/ukevents/public_html/lib/toro.php on line 130

The line this pertains to in toro.php is:

private function ipad_request() {
    return strstr($_SERVER['HTTP_USER_AGENT'], 'iPad');
}

Is it possible for $_SERVER['HTTP_USER_AGENT'] to not be set by a HTTP request?

like image 322
Martin Bean Avatar asked Jun 24 '11 08:06

Martin Bean


People also ask

What is $_ server Http_user_agent?

The variable we are interested in right now is $_SERVER['HTTP_USER_AGENT'] . Note: $_SERVER is a special reserved PHP variable that contains all web server information. It is known as a superglobal. See the related manual page on superglobals for more information.

How do I get Useragent in PHP?

To get the user agent in php, you can use the $_SERVER super global variable and access the key 'HTTP_USER_AGENT'. The User-Agent request header is a string that lets servers and network peers identify various information like the application, operating system, vendor, and/or version of the requesting user agent.

Can you have identical user agents?

If a different person on the Internet with the same configuration accesses your website, then their user agent will be the same.

How can I get user agent in laravel 8?

In this snippet, we'll see how to get user-agent from user request in laravel. $agent = $request->header('user-agent'); $agent = request()->header('user-agent'); $agent = Request::header('user-agent'); All of these code snippet return the User-agent in Laravel.


5 Answers

Yes, it's possible, this a HTTP header sent (or not sent) by client, and you should not rely on it. From php manual:

Contents of the User-Agent: header from the current request, if there is one

So the correct code would be:

private function ipad_request() {
    return isset($_SERVER['HTTP_USER_AGENT']) && strstr($_SERVER['HTTP_USER_AGENT'], 'iPad');
}
like image 128
Maxim Krizhanovsky Avatar answered Oct 25 '22 19:10

Maxim Krizhanovsky


Yes. Any browser or user-agent can choose not to send the User-Agent header. If they do not send that header, $_SERVER['HTTP_USER_AGENT'] won't be set.

Use isset() to ensure that $_SERVER['HTTP_USER_AGENT'] is set.

private function ipad_request() {
  if(!isset($_SERVER['HTTP_USER_AGENT'])) return false;

  return strstr($_SERVER['HTTP_USER_AGENT'], 'iPad') !== false;
}
like image 32
Andrew Moore Avatar answered Oct 25 '22 18:10

Andrew Moore


Yes, it's possible, but it never happens for a regular request.

All browsers do send a browser string in the request, so any request that arrives without one comes from some other program. Even all well-behaving bots send a browser string, so you don't have to be concerned about not showing up in search engines either.

like image 36
Guffa Avatar answered Oct 25 '22 18:10

Guffa


PHP docs says:

'HTTP_USER_AGENT' Contents of the User-Agent: header from the current request, if there is one.

(relevant part italicised) so it would appear it might not always be set.

like image 22
ChrisA Avatar answered Oct 25 '22 19:10

ChrisA


An example where HTTP_USER_AGENT is undefined is if the request coming from GoDaddy's 404 page handler for your site where you have set the handler to be one of your pages.

like image 26
Anono Mouser Avatar answered Oct 25 '22 19:10

Anono Mouser