First off is there one? Or would I have to use javascript? I'd like to be able to make changes to which CSS is used, so frex I could load smaller fonts for a mobile device, or whatever.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
You will have to use JavaScript. The server knows nothing about highly client-side data like that. Fortunately, JavaScript can manipulate styles and style sheets quite easily, so it should be able to tackle your problem without even involving anything server-side.
In the case of mobile devices, another easy tool is setting the media type on a style sheet to handheld.
Take a look at WURFL.
As Ignacio Vazquez-Abrams suggested in his answer, it uses the user agent string to "guess" the resolution for known devices. If you're concerned about mobile devices that's probably as accurate as you can get without the help of a client-side script/app.
You can use $_SERVER['HTTP_USER_AGENT'] by using the following code:
Code:
Output
If tablet:
'is tablet'
If mobile:
'is mobile'
If desktop:
'is desktop'
There is no way to do this. If you want to detect what device is being used then you should examine
$_SERVER['HTTP_USER_AGENT']
for clues.You should look at Tera-WURFL, it is a PHP & MySQL-based software package that detects mobile devices and their capabilities. Here is the Tera-WURFL code that you would use to detect the browser width in PHP:
The solution I have has actually worked real well for years now - no plugins, no classes. Basically, while outputting the HEAD part of the doc, I output some JavaScript that gets the browser width and writes it to a cookie. Then, later, further down the page, I simply read the cookie that has the exact width stored using the $_COOKIE function in php. It has not failed me once in any scenario.