...

View Full Version : PHP Redirect for Mobile Devices (Width Based)



Psionicsin
08-30-2011, 10:00 PM
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:
<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.

Inigoesdr
08-30-2011, 10:32 PM
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?

Psionicsin
08-30-2011, 10:41 PM
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.

elricho
09-08-2011, 11:01 AM
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

xiong_ster123
09-13-2011, 04:07 AM
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.

OpalCat
01-05-2012, 03:36 AM
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.

Spookster
01-05-2012, 03:58 AM
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/3680463/mobile-redirect-using-htaccess

OpalCat
01-05-2012, 11:02 PM
That .htaccess thing works on my phone. :D 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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum