PHP detects the code of the mobile browser

Source: Internet
Author: User
Tags versions
 
 
  1. <?php
  2. function Is_mobile () {
  3. Returns true if one of the specified mobile browsers is detected
  4. $regex _match= "/(Nokiaiphoneandroidmotorola^mot\-softbankfomadocomokddiup\.browserup\.link);
  5. $regex _match.= "Htcdopodblazernetfrontheliohosinhuaweinovarracoolpadwebostechfaithpalmsource";
  6. $regex _match.= "blackberryalcatelamoiktouchnexiansamsung^sam\-s[cg]h^ Lgeericssonphilipssagemwellcombunjalloomaui ";
  7. $regex _match.= "Symbiansmartphonemidpwapphonewindows ceiemobile^spice^bird^zte\-longcospantechgionee^sie\- Portalmmm ";
  8. $regex _match.= "jig\s browserhiptop^ucweb^benqhaier^lctopera\s*mobiopera\*mini320x320240x320176x220";
  9. $regex _match.= ")/I";
  10. return isset ($_server[' http_x_wap_profile ']) or isset ($_server[' http_profile ')) or Preg_match ($regex _match, Strtolower ($_server[' http_user_agent '));
  11. }
  12. /*
  13. Allow the user a way to force either the "full or mobile versions of the" Site-use a Get parameter on requests:
  14. Include likes to both versions of the site W/the Special Force mode parameters, ' mobile ' and ' full ':
  15. <a href= "Http://www.example.org/?mobile" >view mobile site</a>
  16. <a href= "Http://www.example.org/?full" >view full site</a>
  17. Always check for ' mobile ' or ' full ' parameters before accounting to any user-agent conditions:
  18. */
  19. if ($_get[' mobile ']) {
  20. $is _mobile = true;
  21. }
  22. if ($_get[' full ']) {
  23. $is _mobile = false;
  24. }
  25. if ($is _mobile) {
  26. It ' s a mobile browser, do something
  27. Header ("Location:http://www.yoursite.com/mobile");
  28. } else {
  29. It ' s not a mobile browser, do something else
  30. Header ("Location:http://www.yoursite.com/desktop");
  31. Or instead of a redirect, simply build HTML below
  32. }
  33. ?>




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.