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",
"Vk-","Voda","Voxtel","VX","WAP","Wellco","Wig Browser","Wii","Windows CE",
$Is_mobile =False
foreach ($Mobile_agentsAs$device)  { 
         if  (stristr ($ user_agent, $device))  { 
             $is_mobile = true;  
    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

if (is_mobile()) { 
} 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))) { 

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: 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.