首页 >php教程 >php手册 >PHP 检测手机浏览器的代码

PHP 检测手机浏览器的代码

WBOY
WBOY原创
2016-06-21 08:55:29888浏览
<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">"/(nokiaiphoneandroidmotorola^mot\-softbankfomadocomokddiup\.browserup\.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">"htcdopodblazernetfrontheliohosinhuaweinovarraCoolPadwebostechfaithpalmsource"</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">"blackberryalcatelamoiktouchnexiansamsung^sam\-s[cg]h^lgeericssonphilipssagemwellcombunjalloomaui"</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">"symbiansmartphonemidpwapphonewindows ceiemobile^spice^bird^zte\-longcospantechgionee^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">"jig\s browserhiptop^ucweb^benqhaier^lctopera\s*mobiopera\*mini320x320240x320176x220"</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>    //it's 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>     //it's 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>




声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn