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. 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.
Script ec (2); script
I set Windows devices as PCs. After all, my blog is intended for Chinese users. Most home devices still use Windows systems.
The principle is to determine the user agent submitted by the browser.
The Code is as follows: |
|
// Obtain the 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 the 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:
The Code is as follows: |
|
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; } |