The Fragility of HTTP User-Agent String Parsing
Relying on $_SERVER['HTTP_USER_AGENT'] for browser detection is technically possible, but it is fundamentally unreliable for anything beyond rough heuristics. The string provided by the client is not a standardized specification; it is a marketing document written by various browser vendors with little regard for consistency or interoperability.
The core issue lies in how browsers identify themselves. Historically, and unfortunately still today, many browsers prepend "Mozilla/5.0" to their User-Agent strings to ensure compatibility with legacy web standards that assumed the client was Mozilla-based. This creates immediate ambiguity when you attempt to distinguish between Firefox, Safari, or legacy Internet Explorer.
Consider the specific example of Internet Explorer 7 on Windows NT 6.0 (Vista). The server receives a string beginning with Mozilla/4.0, followed by (compatible; MSIE 7.0.... If your logic simply checks for the presence of "mozilla" using stripos, it will incorrectly identify an IE 7 user as Firefox or another Mozilla-based engine. Similarly, older versions of Safari historically included "Mozilla" in their headers to signal support for certain CSS properties and JavaScript behaviors.
Attempting to parse these strings directly with regular expressions or simple string functions is a recipe for brittle code that breaks the moment a browser vendor updates their header format. This fragility makes it ill-advised to use $_SERVER['HTTP_USER_AGENT'] as the sole source of truth for rendering critical resources like CSS files.
The get_browser() Function and Its Limitations
The PHP manual provides the get_browser() function, which attempts to parse the User-Agent string against a database of known signatures. While this approach is more structured than writing your own parser, it introduces new problems.
The most significant limitation is that get_browser relies on static data files that are not updated in real-time. If a browser vendor releases a version with a completely new or obfuscated User-Agent string, the function will likely fail to identify it correctly until the database file is manually updated by the PHP distribution team. Furthermore, because the parsing logic is embedded within the PHP core, you have no control over how edge cases are handled or whether specific signatures are prioritized.
Using get_browser for CSS differentiation is particularly dangerous. If the function misidentifies a user agent due to an outdated signature database, your application will serve the wrong stylesheet. This results in layout shifts, missing features, or broken designs that can only be resolved by clearing caches and waiting for the next PHP update cycle.
Why User-Agent Detection Fails for CSS
The specific use case of serving different CSS files based on browser detection is a pattern that should be avoided regardless of the detection method used. The reason is twofold: performance and reliability.
When you check if(stripos($_SERVER['HTTP_USER_AGENT'],"mozilla")!==false) to serve mozilla.css, you are making an assumption that "Mozilla" implies a specific rendering engine or layout requirement. As noted in the source material, Safari and even modern Firefox can trigger this flag incorrectly depending on the version and configuration. Serving mozilla.css to a user whose browser does not actually require it bloats your payload. Serving the wrong file to a user whose browser does need special handling (like legacy IE) breaks their experience.
The correct architectural approach is progressive enhancement. Your default stylesheet should target modern standards-compliant browsers. You then use media queries or specific feature detection checks within the CSS itself, rather than relying on PHP to select entirely different files.
If you must support legacy environments like Internet Explorer 7, it is better to serve a polyfill script or a fallback stylesheet that adds missing capabilities, rather than trying to block out modern users with an exclusionary User-Agent check. The performance cost of every request checking the User-Agent string against a complex logic tree also compounds as your list of "special cases" grows.
Recommendation
Do not use $_SERVER['HTTP_USER_AGENT'] for precise browser detection if the output depends on accurate identification of the rendering engine. Do not rely on get_browser() for dynamic asset serving, as its static database cannot keep pace with rapid browser evolution.
Instead, design your assets to be resilient. Assume the worst-case scenario for legacy browsers and ensure your application degrades gracefully. If a specific piece of functionality is required only by Internet Explorer 7, encapsulate it in a script that loads conditionally based on feature detection (e.g., checking if document.querySelectorAll behaves correctly) rather than string parsing. This shifts the burden from the server to the client logic, ensuring that your application remains robust regardless of how browser vendors choose to label their software.