Rumah > pembangunan bahagian belakang > tutorial php > PHP 检测手机浏览器的代码_PHP教程

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

WBOY
Lepaskan: 2016-07-13 17:43:09
asal
1000 orang telah melayarinya

 

<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>
Salin selepas log masuk

 

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...
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan