In php, $ _ SERVER ['HTTP _ USER_AGENT '] determines whether the website is accessed by mobile phone-PHP source code

Source: Internet
Author: User
HTTP_USER_AGENT is a global variable used to obtain information about the user's browser. We can use the value obtained by HTTP_USER_AGENT to determine whether the user is a mobile browser or a computer accessing the network. HTTP_USER_AGENT is a global variable used to obtain information about the user's browser. We can use the value obtained by HTTP_USER_AGENT to determine whether the user is a mobile browser or a computer accessing the network.

Script ec (2); script

The Code is as follows:

$ UAgent = $ _ SERVER ['HTTP _ USER_AGENT '];

$ OsPat = "mozilla | mshortate | 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) // a browser that simulates WAP mobile Internet access; openwave | The following is a pc operating system

{

Echo "computer access ";

}

Else

{

Echo "mobile access ";

}

Echo'
'. $ UAgent;

Now we are fully writing the instance into a file.

The Code is as follows:


$ 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'

Related Article

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.