用到的元件是 jenssegers/agent
$agent = new Agent();
$useragant = $agent->getUserAgent();
switch (true) {
case $agent->isMobile():
$device_type = 'Mobile';
break;
case $agent->isTablet():
$device_type = 'Tablet';
break;
case $agent->isDesktop():
$device_type = 'Desktop';
break;
case $agent->isRobot():
$device_type = 'Robot';
break;
default:
$device_type = 'Unknow';
break;
}
// $agent->isRobot()能區分部分爬蟲但不全面
// 包含spider|Spider的也認為是爬蟲
$isnot_robot = $device != 'Robot' && !$agent->match('spider');
$device_type;// Mobile|Tablet|Desktop|Robot
$device = $agent->device();// iphone|nexus|WebKit|Samsung..(或false)
本作品採用《CC 協議》,轉載必須註明作者和本文連結