Detect mobile device php code (mobile phone access)

Source: Internet
Author: User

Php code

The code is as follows: Copy code

// Use the instance

Include 'mobile _ Detect. Php ';
$ Detect = new Mobile_Detect ();

// Check for any mobile device.
If ($ detect-> isMobile ())

// Check for any tablet.
If ($ detect-> isTablet ())

// Check for any mobile device, excluding tablets.
If ($ detect-> isMobile ()&&! $ Detect-> isTablet ())

If ($ detect-> isMobile ()&&! $ Detect-> isTablet ())

// Alternative to $ detect-> isAndroidOS ()
$ Detect-> is ('androidos ');

// Batch usage
Foreach ($ userAgents as $ userAgent ){
$ Detect-> setUserAgent ($ userAgent );
$ IsMobile = $ detect-> isMobile ();
}

// Version check.
$ Detect-> version ('iPad '); // 4.3 (float)


Php determines Mobile access

The code is as follows: Copy code

Ua = strtolower ($ _ SERVER ['http _ USER_AGENT ']);

$ Uachar = "/(nokia | sony | ericsson | mot | samsung | sgh | lg | philips | panasonic | alcatel | lenovo | cldc | midp | mobile | wap)/I ";

If ($ ua = ''| preg_match ($ uachar, $ ua ))&&! Strpos (strtolower ($ _ SERVER ['request _ URI ']), 'wap '))
{
$ Loaction = 'wap /';

If (! Empty ($ Loaction ))
    {
Ecs_header ("Location: $ Loactionn ");

Exit;
    }

}

 

/**
* The Custom header function is used to filter potential security risks.

* @ Param string content

* @ Return void
**/
Function ecs_header ($ string, $ replace = true, $ http_response_code = 0)

If (strpos ($ string, '../upgrade/index. Php') = 0)
    { 
Echo '<script type = "text/javascript"> window. location. href = "'. $ string. '"; </script> ';
    } 
$ String = str_replace (array ("r", "n"), array ('',''), $ string );
 
If (preg_match ('/^ s * location:/is', $ string ))
    { 
@ Header ($ string. "n", $ replace );
 
Exit ();
    } 
 
If (emptyempty ($ http_response_code) | PHP_VERSION <'4. 3 ')
    { 
@ Header ($ string, $ replace );
    } 
Else www.111cn.net
    { 
@ Header ($ string, $ replace, $ http_response_code );
    } 

Js code

Test code:

The code is as follows: Copy code

Var isIPhone =/iPhone/I. test (navigator. userAgent ),
IsIPad =/iPad/I. test (navigator. userAgent ),
IsAndroid =/android/I. test (navigator. userAgent );
Var isIOS = isIPhone | isIPad;
Alert (
"IPhone? "+ IsIPhone +" tr "+
"IPad? "+ IsIPad +" tr "+
"Android? "+ IsAndroid +" tr "+
"IOS? "+ IsIOS
);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.