Home >Backend Development >PHP Tutorial >PHP detects the code of mobile browser_PHP tutorial

PHP detects the code of mobile browser_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 17:43:091028browse

 

<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><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><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>
</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>

 

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