8

Mobile Safari is a very capable browser, and it can handle my website as it is perfectly. However, there are a few elements on my page that could be optimized for browsing using this device; such as serving specific thumbnails that are smaller than the desktop counterparts to help fit more content into the screen.

I would like to know how I can detect Mobile Safari (all versions, preferably) using PHP, so then I can serve a) a specific css file and b) different sized image thumbnails.

different
  • 2,343
  • 3
  • 24
  • 30

6 Answers6

9

Thanks Joe, I read that page and found the WebKit detection library (in JavaScript). I changed the code to suit my needs.

For anyone that's interested, here's my solution.

<?php

/* detect Mobile Safari */

$browserAsString = $_SERVER['HTTP_USER_AGENT'];

if (strstr($browserAsString, " AppleWebKit/") && strstr($browserAsString, " Mobile/"))
{
    $browserIsMobileSafari = true;
}

?>
different
  • 2,343
  • 3
  • 24
  • 30
3
$_SERVER['HTTP_USER_AGENT']  

That will give you the user-agent string back which you can compare to mobile safari.

p.s. http://wurfl.sourceforge.net/ WURFL may help you determine which UAs you want.

GavinCattell
  • 3,863
  • 20
  • 22
2

Compare the user agent string with the one of a Safari Mobile uses:

Safari Mobile User Agent String

fresskoma
  • 25,481
  • 10
  • 85
  • 128
Joe Scylla
  • 701
  • 3
  • 5
  • your link seems no longer to be valid.. it just redirects to the front-page of safari for developers. – matdumsa Mar 22 '10 at 15:58
  • 1
    This is an updated link to the information: http://developer.apple.com/safari/library/documentation/AppleApplications/Reference/SafariWebContent/OptimizingforSafarioniPhone/OptimizingforSafarioniPhone.html – Jeff Winkworth Apr 15 '10 at 13:09
  • Link is dead. Please consider quoting the necessary portions for events such as this. – cjhill Mar 06 '18 at 15:05
  • Solutions with just a link are not valid, as a matter of fact, please follow guidelines how to answer these. Always add context with a link or better: just add context, as a link often expire. – Demian Jan 24 '21 at 15:34
0
<?php

// detect Safari only!

$string = $_SERVER['HTTP_USER_AGENT'];

if (strstr($string, " AppleWebKit/") && strstr($string, " Safari/") && !strstr($string, " CriOS"))
    {
        echo 'See in Safari only';
    }

?>   
0

I have published a new mode to detect devices in any programming language (JSP, PHP, Perl, Python.....), it's called Apache Mobile Filter is an Apache module (http://modules.apache.org/search.php?id=1787) that detect mobile device and also can adapt the images to the screen size of device.

For more info: http://www.idelfuschini.it/it/apache-mobile-filter-v2x.html

0

Mozilla/5.0 (webOS/1.0; U; en-US) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/1.0 Safari/525.27.1 Pre/1.0 that is the browser for the palm Pre, and the word 'Mobile' is not there.

I am working on making my detect work fully with all newer capable browsers. After looking at the mytouch, G1, Palm Pre, droid and others, (but not all) I am now confident this is workable for all new phones:

if(preg_match("/applewebkit/i", $_SERVER['HTTP_USER_AGENT']) && preg_match("/(mobile|pre)/i", $_SERVER['HTTP_USER_AGENT'])) header("Location: http://simplefoodie.com/iphone/?carryover=".urlencode($_SERVER[REQUEST_URI]));

Andrew Deal
  • 91
  • 1
  • 1