Reputation:
In an HTML page I have the following:
<link rel='stylesheet' href='retrievedCSS.php' type='text/css' media='all' />
Inside retrievedCSS.php
, I request a CSS resource from a CDN and I return it to the client.
Problem is CDN is quite "dynamic", so that if I directly execute retrievedCSS.php
using the address bar of a browser, I get a different resource based on its user-agent
string.
However, in my case, it's the server requesting the resource to the CDN, not a client.
Apart from appending it as a parameter (retrievedCSS.php?ua=myUserAgent
) from the HTML page, is there any other way I can get the user agent string of the user loading the HTML page in retrievedCSS.php
?
Upvotes: 1
Views: 4332
Reputation: 918
When you're accessing a web page, your browser is adding the User-Agent
to the request HTTP headers. As the CSS file is only another resource requested via HTTP, you should be able to access to header within the PHP file via:
$_SERVER['HTTP_USER_AGENT']
You can read about it in the PHP docs.
EDIT: That should even work for your dynamic CSS file, as the browser is adding the header to all of the HTTP requests.
Upvotes: 3
Reputation: 492
You can access the user agent string using the variable $_SERVER['HTTP_USER_AGENT']
Upvotes: 0