How to detect safari (iphone) browser in php?

by muriel.schmidt , in category: PHP General , 9 months ago

How to detect safari (iphone) browser in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lily , 9 months ago

@muriel.schmidt 

You can detect the Safari browser on an iPhone using the user agent string in PHP.


Here is an example code snippet to detect Safari on an iPhone:

1
2
3
4
5
6
7
8
9
$userAgent = $_SERVER['HTTP_USER_AGENT'];

if (strpos($userAgent, 'Safari') !== false && strpos($userAgent, 'iPhone') !== false) {
    // Safari browser on iPhone detected
    echo 'Safari browser on iPhone detected';
} else {
    // Not a Safari browser on iPhone
    echo 'Not a Safari browser on iPhone';
}


This code checks if the user agent contains the strings "Safari" and "iPhone". If both are found, it means that the request is coming from the Safari browser on an iPhone.