Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder Psionicsin's Avatar
    Join Date
    Aug 2010
    Location
    Detroit, Michigan
    Posts
    346
    Thanks
    54
    Thanked 0 Times in 0 Posts

    PHP Redirect for Mobile Devices (Width Based)

    Is there a known PHP redirect script that uses the display width to determine if it's a mobile browser or not? Java doesn't quite work well enough, and user agents can be spoofed to a fault.

    SOmething like this, but in PHP would be awesome:
    Code:
    <script type="text/javascript">
    <!--
    if (screen.width <= 699) {
    document.location = "mobile.html";
    }
    //-->
    </script>
    But, as I said, it's pretty unreliable as java needs to load, and then be refreshed when on a mobile device.

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Quote Originally Posted by Psionicsin View Post
    Java doesn't quite work well enough, and user agents can be spoofed to a fault.
    JavaScript; there is a difference.

    User-agent is your best bet with PHP due to not being able to access the screen width. If someone is spoofing their user-agent intentionally why would it be a problem to show them your mobile page?

  • Users who have thanked Inigoesdr for this post:

    Psionicsin (08-30-2011)

  • #3
    Regular Coder Psionicsin's Avatar
    Join Date
    Aug 2010
    Location
    Detroit, Michigan
    Posts
    346
    Thanks
    54
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    JavaScript; there is a difference.

    User-agent is your best bet with PHP due to not being able to access the screen width. If someone is spoofing their user-agent intentionally why would it be a problem to show them your mobile page?
    Yes Javascript lol, sorry. And I guess you're right about the spoofing. If they do it, then it's their problem and not mine. Thanks for that.

  • #4
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Some devices will switch the width and the height when the user rotates the device 90 degrees as well .. You could use Handset Detection, its free for up to 10,000 detections per month and has a dead simple javascript you can include on your pages. Hope that helps. Disclaimer : I work there.

    Cheers
    Richard
    Last edited by Inigoesdr; 09-13-2011 at 02:44 PM.

  • #5
    New Coder
    Join Date
    Jan 2011
    Posts
    75
    Thanks
    1
    Thanked 2 Times in 2 Posts
    I know that this is off topic, but .htaccess works best if you want to redirect users to a mobile page. It will also detect what device they are using and will redirect to the appropriate page.

  • #6
    New Coder
    Join Date
    Aug 2011
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by xiong_ster123 View Post
    I know that this is off topic, but .htaccess works best if you want to redirect users to a mobile page. It will also detect what device they are using and will redirect to the appropriate page.
    Sorry to resurrect an old thread, but can you explain, as if to a child, how to do this? I have mysite.com and m.mysite.com and I would like them to automatically detect whether they're on a regular screen or a mobile screen and load the proper one automatically. Can't use javascript as apparently most mobile browsers don't do it... can't use css because there are images in the layout that make the page too large for mobile (even if display:none; they still load)... so php or .htaccess seem to be my only fallbacks. I've used several that have been suggested and NONE have worked.

  • #7
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by OpalCat View Post
    Sorry to resurrect an old thread, but can you explain, as if to a child, how to do this? I have mysite.com and m.mysite.com and I would like them to automatically detect whether they're on a regular screen or a mobile screen and load the proper one automatically. Can't use javascript as apparently most mobile browsers don't do it... can't use css because there are images in the layout that make the page too large for mobile (even if display:none; they still load)... so php or .htaccess seem to be my only fallbacks. I've used several that have been suggested and NONE have worked.
    You can use .htaccess to check for the HTTP_USER_AGENT and redirect but as was pointed out this is not reliable as a user can easily change it and some browsers may masquerade as something else.

    http://stackoverflow.com/questions/3...using-htaccess
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #8
    New Coder
    Join Date
    Aug 2011
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts
    That .htaccess thing works on my phone. Thanks. Now I just need to get some other people with smartphones to test it...

    I figure anyone who has enough tech savvy to change the user_agent on their phone can deal with the consequences. I also have a button at the top of the page that goes to the mobile site so people can do it manually if they decide to.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •