HTTP_USER_AGENT: Error log problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

HTTP_USER_AGENT: Error log problem

Post by simonmlewis »

Code: Select all

$ua = $_SERVER["HTTP_USER_AGENT"];
We use this to determine what the user is browsing with, but it's causing errors in our error log.
So is there a way to query it, IF the users browser or system allows it?
It seems a lot are from Bing searches.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: HTTP_USER_AGENT: Error log problem

Post by Celauran »

What sort of errors are you encountering? If you're just getting undefined index notices, wrap it in isset()
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: HTTP_USER_AGENT: Error log problem

Post by simonmlewis »

[text][Fri Jun 12 03:45:29 2015] [error] [client 00.00.00.00] PHP Notice: Undefined index: HTTP_USER_AGENT in /var/www/vhosts/site.co.uk/httpdocs/index.php on line 236[/text]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: HTTP_USER_AGENT: Error log problem

Post by Celauran »

isset() will fix that. You always want to use isset() on array indices that may not have been set.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: HTTP_USER_AGENT: Error log problem

Post by simonmlewis »

Like this?

Code: Select all

$ua = (isset($_SERVER["HTTP_USER_AGENT"]);
And then perhaps assign $ua as NULL beforehand?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: HTTP_USER_AGENT: Error log problem

Post by Celauran »

Something like this

Code: Select all

$ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Unknown user agent';
Post Reply