PHP and JavaScript determine whether a guest terminal is a computer or a mobile phone

Source: Internet
Author: User

When users use mobile devices such as mobile phones to access the site, we can detect the user terminal type through the program, if it is a mobile phone user, then guide the user to the mobile site adapted to the mobile screen. This article will show you how to use PHP and JavaScript code to determine the user terminal type.

PHP version

We use PHP's $_server[' http_user_agent ' to get the user agent of the mobile phone user's browser, and then match the existing various mobile browser proxy libraries, if it contains matching keywords, it is judged as mobile (mobile terminal) users.


function Is_mobile () {
$User_agent =$_server[' Http_user_agent '];
$Mobile_agents =Array"240x320","Acer","Acoon","Acs-","Abacho","Ahong","Airness","Alcatel","Amoi",
"Android","Anywhereyougo.com","Applewebkit/525","applewebkit/532","Asus","Audio",
"Au-mic","Avantogo","Becker","BenQ","Bilbo","Bird","BlackBerry","Blazer","Bleu",
"Cdm-","Compal","Coolpad","Danger","Dbtel","Dopod","Elaine","Eric","Etouch","Fly",
"Fly_","Fly-","Go.web","Goodaccess","Gradiente","Grundig","Haier","Hedy","Hitachi",
"HTC","Huawei","Hutchison","Inno","ipad","iPAQ","iphone","ipod","Jbrowser","Kddi",
"Kgt","KWC","Lenovo","LG","Lg2","Lg3","Lg4","Lg5","Lg7","Lg8","Lg9","Lg-","Lge-","Lge9","Longcos","Maemo",
"Mercator","Meridian","Micromax","MIDP","Mini","Mitsu","Mmm","MMP","Mobi","Mot-",
"Moto","Nec-","NetFront","Newgen","Nexian","Nf-browser","Nintendo","Nitro","Nokia",
"Nook","Novarra","Obigo","Palm","Panasonic","Pantech","Philips","Phone","Pg-",
"PlayStation","Pocket","Pt-","Qc-","Qtek","Rover","Sagem","Sama","Samu","Sanyo",
"Samsung","Sch-","Scooter","Sec-","Sendo","Sgh-","Sharp","Siemens","Sie-","SoftBank",
"Sony","Spice","Sprint","SPV","Symbian","Tablet","Talkabout","Tcl-","Teleca","Telit",
"Tianyu","Tim-","Toshiba","TSM","Up.browser","Utec","Utstar","Verykool","Virgin",
"Vk-","Voda","Voxtel","VX","WAP","Wellco","Wig Browser","Wii","Windows CE",
"Wireless","XDA","Xde","ZTE");
$Is_mobile =False
foreach ($Mobile_agentsAs$device)  { 
         if  (stristr ($ user_agent, $device))  { 
             $is_mobile = true;  
             break; 
        } 
    } 
    return $ IS_MOBILE; 

The above code in the function Is_mobile () to determine the user terminal type, the collection of today's various phones http_user_agent attributed to the array $mobile_agents, and to match. Just call the function is_mobile () when you use it. As shown in the following code, when a matching user is accessing the phone, the page jumps to the website mobile version of m.helloweba.com.

 
if (is_mobile()) { 
    header(‘Location:http://m.helloweba.com‘); 
} else { 
    echo ‘请使用手机访问.‘; 

JavaScript Edition

You can also include a JavaScript script directly on the front page to determine the user's terminal type. JavaScript also obtains user-agent information from the browser and then matches the existing User-agent repository.

 
if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iOS|iPad|Backerry| 
WebOS|Symbian|Windows Phone|Phone)/i))) { 
    location.replace("http://m.helloweba.com") 
}else{ 
    document.write("请使用手机访问."); 

The above code is not very perfect, interested friends are welcome to add.

Of course, we can also use a responsive layout to match a variety of screens, which can save development costs, but when the customer on the mobile site function needs, for the independent mobile site is the best use of the site at the entrance to determine the user access terminal type, generally we in the main station home on the judgment, If you are a mobile phone visitor, jump to the mobile page, otherwise access the page as normal PC.

PHP and JavaScript determine whether a guest terminal is a computer or a mobile phone

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.