Home > Backend Development > PHP Tutorial > How Nginx determines mobile phone

How Nginx determines mobile phone

WBOY
Release: 2016-07-06 13:51:58
Original
1538 people have browsed it

Plan to use Nginx for caching to relieve backend pressure. The website has two versions: mobile phone and desktop. Currently, PHP is used for simple judgment. The code is as follows:

<code>function isMobile(){
    
    $ua = $_SERVER['HTTP_USER_AGENT'];
    
    $ismobile = preg_match('/Android|iPhone|IEMoble|Mobile/i', $ua);
    
    $ismobile = preg_match('/iPad|Tablet/i', $ua) ? 0 : $ismobile;
    
    $ismobile = isset($_COOKIE['ismobile']) ? 1 : $ismobile;
    
    return $ismobile;
    
}</code>
Copy after login
Copy after login

The idea of ​​​​judgment in PHP is as follows:

  1. Determine whether the UA has the word Android|iPhone|IEMoble|Mobile, if it does, it is a mobile phone

  2. Then determine whether there is the word iPad|Tablet in UA, if it is, it is not a mobile phone

  3. If there is a cookie named ismobile, regardless of whether the UA is forced to be a mobile phone
    What I hope to achieve is to implement the above judgment logic directly on Nginx and set it as cache key (of course it can be done at the same time It is better to send additional headers to the backend, so as to ensure that the judgment results of the front and back ends are absolutely consistent, haha)

Because my English is very poor and I don’t know much about Nginx syntax, etc., I would like to ask an expert if you can give me a DEMO. Thank you!

Reply content:

Plan to use Nginx for caching to relieve backend pressure. The website has two versions: mobile phone and desktop. Currently, PHP is used for simple judgment. The code is as follows:

<code>function isMobile(){
    
    $ua = $_SERVER['HTTP_USER_AGENT'];
    
    $ismobile = preg_match('/Android|iPhone|IEMoble|Mobile/i', $ua);
    
    $ismobile = preg_match('/iPad|Tablet/i', $ua) ? 0 : $ismobile;
    
    $ismobile = isset($_COOKIE['ismobile']) ? 1 : $ismobile;
    
    return $ismobile;
    
}</code>
Copy after login
Copy after login

The idea of ​​​​judgment in PHP is as follows:

  1. Determine whether the UA has the word Android|iPhone|IEMoble|Mobile, if it does, it is a mobile phone

  2. Then judge whether there is the word iPad|Tablet in UA, if it is, it is not a mobile phone

  3. If there is a cookie named ismobile, regardless of whether the UA is forced to be a mobile phone
    What I hope to achieve is to implement the above judgment logic directly on Nginx and set it as cache key (of course it can be done at the same time It is better to send additional headers to the backend, so as to ensure that the judgment results of the front and back ends are absolutely consistent, haha)

Because my English is very poor and I don’t know much about Nginx syntax, etc., I would like to ask an expert if you can give me a DEMO. Thank you!

Poor English is not the reason. I basically failed in English. Shouldn’t it be the same as Google? At worst, I'll just use a translator.
Make good use of search, and many problems can be solved.
This is the first article I found by searching Google for "nginx determines mobile phone": Portal

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template