开发者

Detect iOS version with PHP

For detecting Internet Explorer I use his line.

<?php if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) 开发者_运维问答{ };?>

How do I detect iOS 5.


I had a similar problem but needed to get the version number in iPhone and iPad using $_SERVER['HTTP_USER_AGENT']. I suppose it is not completely reliable, but it worked for me:

<?php 

$version = preg_replace("/(.*) OS ([0-9]*)_(.*)/","$2", $_SERVER['HTTP_USER_AGENT']);

// for example you use it this way

if ($version > 5){
// do something
}


?> 


The HTTP_USER_AGENT will return the following:

Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7

If you are trying to detect iOS 5, do the following:

 <?php if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone OS 5_0') !== false) { };?>


If this is possible, get_browser() will be the way to do it.

However, since a User-Agent string is easily spoofable, and the list of browsers/OSes is a constantly moving target, this will never be 100% reliable. In order for this to work at all, you must keep the php_browscap.ini file up to date (see note on the linked manual page).


Simple and easy

<?php
   $browser = get_browser(null, true);
   echo $browser['platform'];
?> 

The docs say

Note:

In order for this to work, your browscap configuration setting in php.ini must point to the correct location of the browscap.ini file on your system.

browscap.ini is not bundled with PHP, but you may find an up-to-date » php_browscap.ini file here.

While browscap.ini contains information on many browsers, it relies on user updates to keep the database current. The format of the file is fairly self-explanatory.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜