Si vous avez besoin de détecter le navigateur ou le système d’exploitation de vos utilisateurs, voici le code !
// Simple browser and OS detection script. This will not work if User Agent is false.
$agent = $_SERVER['HTTP_USER_AGENT'];
// Detect Device/Operating System
if(preg_match('/Linux/i',$agent)) $os = 'Linux';
elseif(preg_match('/Mac/i',$agent)) $os = 'Mac';
elseif(preg_match('/iPhone/i',$agent)) $os = 'iPhone';
elseif(preg_match('/iPad/i',$agent)) $os = 'iPad';
elseif(preg_match('/Droid/i',$agent)) $os = 'Droid';
elseif(preg_match('/Unix/i',$agent)) $os = 'Unix';
elseif(preg_match('/Windows/i',$agent)) $os = 'Windows';
else $os = 'Unknown';
// Browser Detection
if(preg_match('/Firefox/i',$agent)) $br = 'Firefox';
elseif(preg_match('/Mac/i',$agent)) $br = 'Mac';
elseif(preg_match('/Chrome/i',$agent)) $br = 'Chrome';
elseif(preg_match('/Opera/i',$agent)) $br = 'Opera';
elseif(preg_match('/MSIE/i',$agent)) $br = 'IE';
else $bs = 'Unknown';
// Action when OS/Device is found
if($os=='Linux') { echo 'This is done when the OS/Device is Linux'; }
if($os=='Mac') { echo 'This is done when the OS/Device is MacOS'; }
if($os=='iPhone') { echo 'This is done when the OS/Device is iPhone'; }
if($os=='iPad') { echo 'This is done when the OS/Device is iPad'; }
if($os=='Droid') { echo 'This is done when the OS/Device is Droid'; }
if($os=='Unix') { echo 'This is done when the OS/Device is Unix'; }
if($os=='Unknown'){ echo 'This is done when the OS/Device is unknown. This is the failsafe for when an OS/Device is not detected.'; }
// Action when Browser is detected
if($br=='Firefox'){ echo 'This is done when the Browser is Firefox'; }
if($br=='Chrome') { echo 'This is done when the Browser is Chrome'; }
if($br=='Opera') { echo 'This is done when the Browser is Opera'; }
if($br=='MSIE') { echo 'This is done when the Browser is Microsoft Internet Explorer'; }
if($br=='Unknown'){ echo 'This is done when the Browser is Unknown. This is the failsafe for when a browser type is not detected'; }
// Action when both browser and OS/Device are detected
if($os=='Linux' && $br=='Firefox') { echo 'This is done when the Browser is Firefox and the OS is Linux.'; }
if($os=='Unknown' && $br=='Unknown'){ echo 'This is done when the Browser is Unknown and the OS is Unknown. This is the catch-the-rest failsafe'; }
MISE A JOUR du 12 Février 2019 (merci à Sébastien pour ces fonctions)
3 fonctions beaucoup plus complète que mon code précédent
function detect_browser() {
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$browser = "Inconnu";
$browser_array = array( '/mobile/i' => 'Handheld Browser',
'/msie/i' => 'Internet Explorer',
'/trident/i' => 'Internet Explorer',
'/firefox/i' => 'Firefox',
'/safari/i' => 'Safari',
'/chrome/i' => 'Chrome',
'/edg/i' => 'Edge',
'/opera/i' => 'Opera',
'/netscape/i' => 'Netscape',
'/maxthon/i' => 'Maxthon',
'/konqueror/i' => 'Konqueror'
);
foreach ($browser_array as $regex => $value)
if (preg_match($regex, $user_agent))
$browser = $value;
return $browser;
}
/**
* Récupérer la véritable adresse IP d'un visiteur
*/
function get_ip() {
// IP si internet partagé
if (isset($_SERVER['HTTP_CLIENT_IP'])) {
return $_SERVER['HTTP_CLIENT_IP'];
}
// IP derrière un proxy
elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
// Sinon : IP normale
else {
return (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '');
}
}
function detect_os() {
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$os_platform = "Inconnu";
$os_array = array( '/windows nt 10/i' => 'Windows 10',
'/windows nt 6.3/i' => 'Windows 8.1',
'/windows nt 6.2/i' => 'Windows 8',
'/windows nt 6.1/i' => 'Windows 7',
'/windows nt 6.0/i' => 'Windows Vista',
'/windows nt 5.2/i' => 'Windows Server 2003/XP x64',
'/windows nt 5.1/i' => 'Windows XP',
'/windows xp/i' => 'Windows XP',
'/windows nt 5.0/i' => 'Windows 2000',
'/windows me/i' => 'Windows ME',
'/win98/i' => 'Windows 98',
'/win95/i' => 'Windows 95',
'/win16/i' => 'Windows 3.11',
'/macintosh|mac os x/i' => 'Mac OS X',
'/mac_powerpc/i' => 'Mac OS 9',
'/linux/i' => 'Linux',
'/ubuntu/i' => 'Ubuntu',
'/iphone/i' => 'iPhone',
'/ipod/i' => 'iPod',
'/ipad/i' => 'iPad',
'/android/i' => 'Android',
'/blackberry/i' => 'BlackBerry',
'/webos/i' => 'Mobile'
);
foreach ($os_array as $regex => $value)
if (preg_match($regex, $user_agent))
$os_platform = $value;
return $os_platform;
}