Skip to content

Instantly share code, notes, and snippets.

@softiconic
Last active December 2, 2023 18:39
Show Gist options
  • Save softiconic/5d6aaf13bf787b9b3bc90f1aa8c9741a to your computer and use it in GitHub Desktop.
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.
<?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