According to this thread, and specially this post: https://stackoverflow.com/a/6595973/1125465, Microsoft as always shows off. The size of user agent, can be really, really huge.
I'm working on a little visitors library in php, and I want to store user agent information. I cannot decide on the data type and length.
So my question is: have you got any ideas, on how to shorten the user agent, to some "normal" size? (for example 256 chars).
Note: Developers use user agents for detecting the user browser, and operating systems. So according to the linked example, all the stupid numbers from M$ are just... Just are. As always, getting on our nerves. So the idea is to make a function that shorten the user agent string but is not losing the important information.
I think that such a function should:
- Not depend on future updates and new browsers (no hardcoded strings)
- Have a simple mechanism that decide what to delete (for example, if there is a number, comma, number, comma, number, comma, number, ..., it can delete it, it is not interesting).
- And at the end if all the operations still results in too long user agent (lets say 256 chars), there is nothing more to do, so just cut off the rest. This is one per million, so the data can be lost.
Additional note: I know, that I can make a function that get the browser, and OS type from user agent, and save only these values. But as always such a functions have hardcoded names, and if browser isn't recognized, it for example return "Unrecognized browser'. So in the future everyone must remember about updating these function. And if we save shorten user agent, the information isn't lost (as only the script that is reading the database must have new recognition system). But the entries in database are reliable and consistent, as should be.
UPDATE: As there should be some code, and there is a problem with idea, and not the problem with existing code, I will write some minimum code, that I wrote so far ;) :
<?php
function shorten($useragent, $maxsize = 256) {
$shorten = $useragent;
... // ?
$shorten = substr($shorten, 0, $maxsize); // the "last hope" cut
return $shorten;
}
echo shorten($_SERVER['HTTP_USER_AGENT']);
?>