Zend Certified Engineer

phpguru.org

Free PHP, Javascript and C# code

User Agents

...suck. How simple is that? A godawful mish mash of crap that somehow manages (most of the time) to identify a browser... They've been with us for years, since the first Mozilla browser I guess, and are likely to be here for a good while longer. There is some documentation to be found explaining the format here and more recently, here. But look at this...

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)

Ick. What a load of pants.

So far be it from me to go out on a limb and suggest a replacement, but thats what I'm gonna do.

User-Agent2:

Enter the User-Agent2: header. My version of the abomination. Let me explain. The people currently "making up" the PEAR rules seemingly insist on calling the packages "Foo2", where Foo is the package, which has undergone some significant and/or miraculous BC breaking change. Whether this is a good thing, or a bad thing I won't go into (bad bad bad), however I will mercilessly steal the idea and use for my all new User-Agent2: header plan.

So whats the format? Well, it's dead easy. Most text based protocols that have the concept of headers (eg. HTTP, MIME etc), also have the concept of parameters, which are bits added on to the end of the value, separated by semi-colons, which add extra meaning, or information. I intend to steal (notice the theme...) this idea, and use here. There will be no value to the User-Agent2: header, only parameters. Here's an example (which also happens to take advantage of header folding):

User-Agent2: a="Internet Explorer";
             v="6.0";
             p="Windows";
             pv="XP";
             s="N";
             l="en-GB"

See. How bloody obvious is that? Ok perhaps not. Some explanations:

  • a - Application, required. Gives the REAL name of the application, that people want to read.
  • v - Version, required. Gives the version of the application.
  • p - Platform, optional. Gives the platform that the application runs on. Potential values include: Windows, Mac, X11.
  • pv - Platform Version, optional. Gives the version of the platform that the application runs on. Given the myriad of potential platforms, this is an arbitrary string, but should retain instant readability. Eg. for Windows platform, this could be "3.11", "NT4.0", "2000" or "XP".
  • s - Security, optional. Gives the security of application. Not sure why I include this. Pointless IMO.
  • l - Localisation, optional. I guess some people might use this. I don't though. Btw, did you see the s? No zees here el-Yankee-Doodles.

See. How bloody obvious is that?

You might be asking, "What advantages does this offer?". Well stop asking that. Not only is it a heck of a lot easier to parse, but the introduction of a new header means you can use readable values for each of the fields. So instead of having mounds of code to do something that should be very simple (identifying the browser...), you could do it with one regex. For example...

<?php
$str
= <<<END
User-Agent2: a="Internet Explorer";
             v="6.0";
             p="Windows";
             pv="XP";
             s="N";
             l="en-GB"
END;

preg_match_all('#(a|v|p|pv|s|l)="([^"]*)"(?:\s*;\s*)?#i', $str, $matches);

print_r($matches);
?>

Not only this, but this new format allows for significant expandability. Need a colour (note the u...) identifier? Just add it in: c="Bright Pink".

So there you go. Food for thought. Tasty food too. Yum.

Link to me

If you use any of the code on this site (and if you don't I guess) or it makes your life easier, I'd appreciate a link - http://www.phpguru.org. Thanks.

RSS Feed for Comments

Comments

Author: Char-Lez Braden
Posted: 9th March 2005 07:18
Your idea is good.

But why stop with those short field names? Why not go ahead and use full descriptive names?

User-Agent2: application="Internet Explorer";
version="6.0";
platform="Windows";
platform_version="XP";
security="N";
localization="en-GB"

And now you reduce out even more machine useful only stuff.

Regards,
Char-Lez Braden
komori_san hotmail.com
Quote
Author: Char-Lez Braden
Posted: 9th March 2005 07:21
So I'm an accidental spammin mofo...

I like the idea of the number system on the bottom of the comments area, but in my sleep deprived state it took many attempts ot "get it"

I kept entering 202.

OK, dense, I know, now. But I almost gave up thinking the system was not working because it kept tellingme I was wrong. What are the odds of that? Me unable to copy 202?

Perhaps a little more description for the impared?

Thanks,
Char-Lez
komori_san hotmail
Quote
Author: Richard Heyes
Posted: 15th March 2005 22:11
Char-Lez Braden:
> Your idea is good.
>
> But why stop with those short field names? Why
> not go ahead and use full descriptive names?
>
> User-Agent2: application="Internet Explorer";
> version="6.0";
> platform="Windows";
> platform_version="XP";
> security="N";
> localization="en-GB"
>
> And now you reduce out even more machine useful
> only stuff.

A very good point.
Quote
Author: andre
Posted: 16th March 2005 03:02
user agent strings are dead. no one should be looking at them anymore. you can't even trust them, since the latest browsers have the capability/optional extensions to change them.

i believe that a web application should NOT have to look at the user-agent string. how many times have you seen a web site that tries to dish out different versions/CSS files based on your user agent string? and how ugly they look when a new browser version comes out?

well if you insist looking at a user-agent string...it's really up to you :)
Quote
Author: foreach
Posted: 17th February 2006 12:52
>Author: andre
>Posted: 16th March 2005 03:02

>user agent strings are dead. no one should be >looking at them anymore.

i just use the browserstring in combination with the ip , to identify a user,
session = md5(IP + browser);
thats too hard in many ways and so i decide to use only first 2 parts of the ip, 22.33. + browserstring, and so i can identifie you even if your ip changes,


Quote

Post Comment

Your name:
Your email:
(Don't worry, I won't spam you.)
Comments:
  Do not post support type questions please

 
CAPTCHA image If you can't read the CAPTCHA then press the submit button to get another. Your comment will re-appear (as if by magic...).
Captcha image
Last modified: 17:06 29th December 2006