Last active
December 2, 2023 18:39
-
-
Save softiconic/5d6aaf13bf787b9b3bc90f1aa8c9741a to your computer and use it in GitHub Desktop.
Detect if the user is on a laptop or mobile device using browser checks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getBrowser() | |
{ | |
$user_agent = $_SERVER['HTTP_USER_AGENT']; | |
$browser = "N/A"; | |
$browsers = [ | |
'/msie/i' => 'Internet explorer', | |
'/firefox/i' => 'Firefox', | |
'/safari/i' => 'Safari', | |
'/chrome/i' => 'Chrome', | |
'/edge/i' => 'Edge', | |
'/opera/i' => 'Opera', | |
'/mobile/i' => 'Mobile browser', | |
]; | |
foreach ($browsers as $regex => $value) { | |
if (preg_match($regex, $user_agent)) { | |
$browser = $value; | |
} | |
} | |
return $browser; | |
} | |
$scbrowser = getBrowser(); | |
if ($scbrowser == 'Safari') { ?> | |
safari | |
<?php } else { ?> | |
others | |
<?php } ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment