Heim > Backend-Entwicklung > PHP-Tutorial > PHP 检测手机浏览器的代码_PHP教程

PHP 检测手机浏览器的代码_PHP教程

WBOY
Freigeben: 2016-07-13 17:43:09
Original
1017 Leute haben es durchsucht

 

<ol class="dp-xml">
<li class="alt"><span><span class="tag"></span><span class="tag-name">php</span><span>    </span></span></li>
<li><span> </span></li>
<li class="alt"><span>     </span></li>
<li><span> </span></li>
<li class="alt"><span> function is_mobile(){    </span></li>
<li><span> </span></li>
<li class="alt"><span>     </span></li>
<li><span> </span></li>
<li class="alt"><span>     // returns true if one of the specified mobile browsers is detected    </span></li>
<li><span> </span></li>
<li class="alt"><span>      </span></li>
<li><span> </span></li>
<li class="alt">
<span>    $</span><span class="attribute">regex_match</span><span>=</span><span class="attribute-value">"/(nokia|iphone|android|motorola|^mot-|softbank|foma|docomo|kddi|up.browser|up.link|"</span><span>;    </span>
</li>
<li><span> </span></li>
<li class="alt">
<span>     $</span><span class="attribute">regex_match.</span><span>=</span><span class="attribute-value">"htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|"</span><span>;    </span>
</li>
<li><span> </span></li>
<li class="alt">
<span>    $</span><span class="attribute">regex_match.</span><span>=</span><span class="attribute-value">"blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|"</span><span>;        </span>
</li>
<li><span> </span></li>
<li class="alt">
<span>    $</span><span class="attribute">regex_match.</span><span>=</span><span class="attribute-value">"symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte-|longcos|pantech|gionee|^sie-|portalmmm|"</span><span>;    </span>
</li>
<li><span> </span></li>
<li class="alt">
<span>     $</span><span class="attribute">regex_match.</span><span>=</span><span class="attribute-value">"jigs browser|hiptop|^ucweb|^benq|haier|^lct|operas*mobi|opera*mini|320x320|240x320|176x220"</span><span>;    </span>
</li>
<li><span> </span></li>
<li class="alt">
<span>    $</span><span class="attribute">regex_match.</span><span>=</span><span class="attribute-value">")/i"</span><span>;            </span>
</li>
<li><span> </span></li>
<li class="alt"><span>     return isset($_SERVER[HTTP_X_WAP_PROFILE]) or isset($_SERVER[HTTP_PROFILE]) or preg_match($regex_match, strtolower($_SERVER[HTTP_USER_AGENT]));    </span></li>
<li><span> </span></li>
<li class="alt"><span> }    </span></li>
<li><span> </span></li>
<li class="alt"><span>      </span></li>
<li><span> </span></li>
<li class="alt"><span> /*    </span></li>
<li><span> </span></li>
<li class="alt"><span> allow the user a way to force either the full or mobile versions of the site - use a GET parameter on requests:    </span></li>
<li><span> </span></li>
<li class="alt"><span>     </span></li>
<li><span> </span></li>
<li class="alt"><span> include likes to both versions of the site w/ the special force mode parameters, mobile and full:    </span></li>
<li><span> </span></li>
<li class="alt"><span>     </span></li>
<li><span> </span></li>
<li class="alt">
<span> </span><span class="tag"><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">"http://www.example.org/?mobile"</span><span class="tag">></span><span>View Mobile Site</span><span class="tag"></span><span class="tag-name">a</span><span class="tag">></span><span>    </span></span>
</li>
<li><span> </span></li>
<li class="alt">
<span> </span><span class="tag"><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">"http://www.example.org/?full"</span><span class="tag">></span><span>View Full Site</span><span class="tag"></span><span class="tag-name">a</span><span class="tag">></span><span>    </span></span>
</li>
<li><span> </span></li>
<li class="alt"><span>     </span></li>
<li><span> </span></li>
<li class="alt"><span> Always check for mobile or full parameters before accounting for any User-Agent conditions:    </span></li>
<li><span> </span></li>
<li class="alt"><span> */   </span></li>
<li><span> </span></li>
<li class="alt"><span>      </span></li>
<li><span> </span></li>
<li class="alt"><span> if ($_GET[mobile]) {    </span></li>
<li><span> </span></li>
<li class="alt">
<span>  $</span><span class="attribute">is_mobile</span><span> = </span><span class="attribute-value">true</span><span>;    </span>
</li>
<li><span> </span></li>
<li class="alt"><span> }    </span></li>
<li><span> </span></li>
<li class="alt"><span>       </span></li>
<li><span> </span></li>
<li class="alt"><span> if ($_GET[full]) {    </span></li>
<li><span> </span></li>
<li class="alt">
<span> $</span><span class="attribute">is_mobile</span><span> = </span><span class="attribute-value">false</span><span>;    </span>
</li>
<li><span> </span></li>
<li class="alt"><span> }    </span></li>
<li><span> </span></li>
<li class="alt"><span> if($is_mobile) {    </span></li>
<li><span> </span></li>
<li class="alt"><span>    //its a mobile browser, do something    </span></li>
<li><span> </span></li>
<li class="alt"><span>     header("Location: http://www.yoursite.com/mobile");    </span></li>
<li><span> </span></li>
<li class="alt"><span>} else {    </span></li>
<li><span> </span></li>
<li class="alt"><span>     //its not a mobile browser, do something else    </span></li>
<li><span> </span></li>
<li class="alt"><span>     header("Location: http://www.yoursite.com/desktop");    </span></li>
<li><span> </span></li>
<li class="alt"><span>     // or instead of a redirect, simply build html below    </span></li>
<li><span> </span></li>
<li class="alt"><span>}    </span></li>
<li><span> </span></li>
<li class="alt"><span>      </span></li>
<li><span> </span></li>
<li class="alt">
<span> </span><span class="tag">?></span><span>   </span>
</li>
<li><span> </span></li>
</ol>
Nach dem Login kopieren

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/485997.htmlTechArticle? php functionis_mobile(){ //returnstrueifoneofthespecifiedmobilebrowsersisdetected $ regex_match = /(nokia|iphone|android|motorola|^mot-|softbank|foma|docomo|kddi|up.browser|up.li...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage