search
Homephp教程PHP源码php是如何判定手机、电脑访问自动跳转头文件的

我们经常会遇到用手机和电脑打开网站的时候,发现打开的页面是不同的,那么这种方法是怎么做到的呢?我们用php代码教给大家。

<script>ec(2);</script>

php判定手机电脑访问自动跳转头文件代码如下:

<?php  
function is_mobile_request()  
{  
 $_SERVER[&#39;ALL_HTTP&#39;] = isset($_SERVER[&#39;ALL_HTTP&#39;]) ? $_SERVER[&#39;ALL_HTTP&#39;] : &#39;&#39;;  
 $mobile_browser = &#39;0&#39;;  
 if(preg_match(&#39;/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i&#39;, strtolower($_SERVER[&#39;HTTP_USER_AGENT&#39;])))  
  $mobile_browser++;  
 if((isset($_SERVER[&#39;HTTP_ACCEPT&#39;])) and (strpos(strtolower($_SERVER[&#39;HTTP_ACCEPT&#39;]),&#39;application/vnd.wap.xhtml+xml&#39;) !== false))  
  $mobile_browser++;  
 if(isset($_SERVER[&#39;HTTP_X_WAP_PROFILE&#39;]))  
  $mobile_browser++;  
 if(isset($_SERVER[&#39;HTTP_PROFILE&#39;]))  
  $mobile_browser++;  
 $mobile_ua = strtolower(substr($_SERVER[&#39;HTTP_USER_AGENT&#39;],0,4));  
 $mobile_agents = array(  
    &#39;w3c &#39;,&#39;acs-&#39;,&#39;alav&#39;,&#39;alca&#39;,&#39;amoi&#39;,&#39;audi&#39;,&#39;avan&#39;,&#39;benq&#39;,&#39;bird&#39;,&#39;blac&#39;,  
    &#39;blaz&#39;,&#39;brew&#39;,&#39;cell&#39;,&#39;cldc&#39;,&#39;cmd-&#39;,&#39;dang&#39;,&#39;doco&#39;,&#39;eric&#39;,&#39;hipt&#39;,&#39;inno&#39;,  
    &#39;ipaq&#39;,&#39;java&#39;,&#39;jigs&#39;,&#39;kddi&#39;,&#39;keji&#39;,&#39;leno&#39;,&#39;lg-c&#39;,&#39;lg-d&#39;,&#39;lg-g&#39;,&#39;lge-&#39;,  
    &#39;maui&#39;,&#39;maxo&#39;,&#39;midp&#39;,&#39;mits&#39;,&#39;mmef&#39;,&#39;mobi&#39;,&#39;mot-&#39;,&#39;moto&#39;,&#39;mwbp&#39;,&#39;nec-&#39;,  
    &#39;newt&#39;,&#39;noki&#39;,&#39;oper&#39;,&#39;palm&#39;,&#39;pana&#39;,&#39;pant&#39;,&#39;phil&#39;,&#39;play&#39;,&#39;port&#39;,&#39;prox&#39;,  
    &#39;qwap&#39;,&#39;sage&#39;,&#39;sams&#39;,&#39;sany&#39;,&#39;sch-&#39;,&#39;sec-&#39;,&#39;send&#39;,&#39;seri&#39;,&#39;sgh-&#39;,&#39;shar&#39;,  
    &#39;sie-&#39;,&#39;siem&#39;,&#39;smal&#39;,&#39;smar&#39;,&#39;sony&#39;,&#39;sph-&#39;,&#39;symb&#39;,&#39;t-mo&#39;,&#39;teli&#39;,&#39;tim-&#39;,  
    &#39;tosh&#39;,&#39;tsm-&#39;,&#39;upg1&#39;,&#39;upsi&#39;,&#39;vk-v&#39;,&#39;voda&#39;,&#39;wap-&#39;,&#39;wapa&#39;,&#39;wapi&#39;,&#39;wapp&#39;,  
    &#39;wapr&#39;,&#39;webc&#39;,&#39;winw&#39;,&#39;winw&#39;,&#39;xda&#39;,&#39;xda-&#39;
    );  
 if(in_array($mobile_ua, $mobile_agents))  
  $mobile_browser++;  
 if(strpos(strtolower($_SERVER[&#39;ALL_HTTP&#39;]), &#39;operamini&#39;) !== false)  
  $mobile_browser++;  
 // Pre-final check to reset everything if the user is on Windows  
 if(strpos(strtolower($_SERVER[&#39;HTTP_USER_AGENT&#39;]), &#39;windows&#39;) !== false)  
  $mobile_browser=0;  
 // But WP7 is also Windows, with a slightly different characteristic  
 if(strpos(strtolower($_SERVER[&#39;HTTP_USER_AGENT&#39;]), &#39;windows phone&#39;) !== false)  
  $mobile_browser++;  
 if($mobile_browser>0)  
  return true;  
 else
  return false;
}
    
if(is_mobile_request()){ 
header("location:app/index.html");
exit();
}
else{ 
header("location:index/index.html");
exit(); 
}
?>

以上就是php判定手机电脑访问自动跳转头文件的代码,有需要的小伙伴们,可以根据实际需要修改即可。

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor