PHP $_server[' http_user_agent ' to determine whether to access the site for mobile phones

Source: Internet
Author: User
The code is as follows Copy Code

$uAgent = $_server[' http_user_agent '];

$osPat = "mozilla|m3gate|winwap|openwave| Windows nt| Windows 3.1|95| blackcomb|98| me| X window|ubuntu| longhorn| Aix| Linux| amigaos| beos| Hp-ux| openbsd| freebsd| netbsd| os/2| osf1| SUN ";

if (Preg_match ("/($osPat)/i", $uAgent))//winwap A browser that simulates WAP mobile internet; Openwave| back to the PC operating system

{

echo "Computer access";

}

Else

{

echo "Mobile Access";

}

Echo ' <br/> '. $uAgent;

Now we're going to complete the case as a document.

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
}

}


/**
* Custom header function to filter for possible security risks
*
* @param string 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
{
@header ($string, $replace, $http _response_code);
}
}

JavaScript uses navigator.useragent to get it, but a non-intelligent machine does not seem to support JS.

JS-enabled mobile clients can also use the following code simple to determine whether the Windows computer access

The code is as follows Copy Code

if (navigator.platform.indexOf (' Win32 ')!=-1) {
Go to PC
}else{
Go to Phone
}

More about judging Mobile Access articles http://www.111cn.net/phper/php-cy/44574.htm

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.