PHP: iPhone, iPad, Android, and PC devices
This article describes how PHP can easily determine iPhone, iPad, Android, and PC devices. We will share this with you for your reference. The details are as follows:
Because of work requirements, we need to know what kind of users have accessed our website. Now there are many mobile devices, let's take a look at the example of PHP used to judge iPhone, iPad, Android, and PC devices.
I set Windows devices as PCs. After all, my blog is intended for Chinese users, and most of my home devices still use Windows systems.
The principle is to determine the user agent submitted by the browser. The Code is as follows:
<? Php // get user agent $ agent = strtolower ($ _ SERVER ['HTTP _ USER_AGENT ']); // analyze data $ is_pc = (strpos ($ agent, 'windows nt '))? True: false; $ is_iphone = (strpos ($ agent, 'iphone '))? True: false; $ is_ipad = (strpos ($ agent, 'ipad '))? True: false; $ is_android = (strpos ($ agent, 'android '))? True: false; // output data if ($ is_pc) {echo "this is PC";} if ($ is_iphone) {echo "this is iPhone";} if ($ is_ipad) {echo "this is iPad";} if ($ is_android) {echo "this is Android" ;}?>
If you only determine whether the device is an iphone device, perform the following operations:
function get_device_type(){ $agent = strtolower($_SERVER['HTTP_USER_AGENT']); $type = 'other'; if(strpos($agent, 'iphone') || strpos($agent, 'ipad') ){ $type = 'ios'; } if(strpos($agent, 'android')){ $type = 'android'; } return $type;}