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

PHP 检测手机浏览器的代码

WBOY
WBOY원래의
2016-06-21 08:55:29879검색
<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으로 문의하세요.