Log in

View Full Version : Mobile Device Re-Direct Code


Jason Dunn
07-22-2003, 09:00 PM
Webmasters, listen up: if you'd like to create a version of your site for mobile devices, the first step is to create code that will detect the type of device hitting the site (or, more accurately, its browser). The sniffer code to do so, developed by Fabrizio Fiandanese, looks like this:<!><br /><br />&lt;?<br /><br />if (stristr($ua, "Windows CE") or stristr($ua, "AvantGo") or stristr($ua,<br />"Mazingo") or stristr($ua, "Mobile") or stristr($ua, "T68") or stristr($ua,<br />"Syncalot") or stristr($ua, "Blazer") )<br />{<br /> $DEVICE_TYPE="MOBILE";<br />}<br /><br />if (isset($DEVICE_TYPE) and $DEVICE_TYPE=="MOBILE")<br />{<br /> $location='mobile/index.php';<br /> header ('Location: '.$location);<br /> exit;<br />}<br /><br />?><br /><br />You can also use Javascript version that detects any Windows-CE client:<br /><br />&lt;script><br />var isCE = navigator.appVersion.indexOf("Windows CE")>0;<br />if (isCE)<br />{<br /> window.location.href="mobile/";<br />}<br />&lt;/script><br /><br />Place the above at the very top of your index page, and when a mobile device hits it, it will be re-directed to your mobile page. The industry standard is <a href="www.domain.com/mobile/index.htm">www.domain.com/mobile/index.htm</a> (or .php, .asp, etc.). From there it's a matter of building the appropriate components into your mobile site. At the very least, if you're running a retail business, include contact information, your business hours, etc. If your Web site is dynamic, with your content separated from the templates, you should be able to include your data in a mobile device-friendly format.<br /><br />It's also a good idea to include a link on your mobile page back to the desktop home page, this time with a version that doesn't have the sniffer code - for those people who are running more capable mobile device browsers, or desperately need something off your site.

entropy1980
07-22-2003, 09:09 PM
Thank you....I had been using somthing similar but had a few problems with those darn OSS browsers :?

PhatCohiba
07-22-2003, 09:14 PM
Thanks Jason.

Now if we can only get more sites to worry about reformating.

-John

merlin
07-22-2003, 09:20 PM
I've been using this code for ASP pages:



&lt;%

' This specifically tests for Pocket IE on Pocket PC 2002 by searching for the "PPC" string within
' the User Agent field. "PPC" is only inclued in the USER AGENT header field on Pocket PC 2002.

userAgent = Request.ServerVariables("HTTP_USER_AGENT")


isPocketPc = false



' This check to see if the browser is any version of MSIE on Windows CE. That way, if the browser
' gets updated, this should still detect that it is from a Windows CE machine.

if(userAgent &lt;> "" AND (InStr(userAgent, "PPC") > 0)) then

isPocketPc = true

end if

if isPocketPc then


' If the user is connecting with a pocket pc, then we redirect them to another page.

response.redirect("mobile/index.htm")

end if
%>

entropy1980
07-22-2003, 09:23 PM
I've been using this code for ASP pages:



&lt;%

' This specifically tests for Pocket IE on Pocket PC 2002 by searching for the "PPC" string within
' the User Agent field. "PPC" is only inclued in the USER AGENT header field on Pocket PC 2002.

userAgent = Request.ServerVariables("HTTP_USER_AGENT")


isPocketPc = false



' This check to see if the browser is any version of MSIE on Windows CE. That way, if the browser
' gets updated, this should still detect that it is from a Windows CE machine.

if(userAgent &lt;> "" AND (InStr(userAgent, "PPC") > 0)) then

isPocketPc = true

end if

if isPocketPc then


' If the user is connecting with a pocket pc, then we redirect them to another page.

response.redirect("mobile/index.htm")

end if
%>


The problem I see is that PPC is sometimes the UA of Mac's so some Mac browsers start getting the mobile version...

Iglesiab
07-22-2003, 09:25 PM
It's also a good idea to include a link on your mobile page back to the desktop home page

Could someone tell me where this is on the mobile pocketpcthoughts.com. I must keep overlooking it and a few times I really could have used it. Thanks

bdeli
07-22-2003, 09:41 PM
Cool - thnx!

Jason Dunn
07-22-2003, 09:45 PM
It's also a good idea to include a link on your mobile page back to the desktop home page

Could someone tell me where this is on the mobile pocketpcthoughts.com. I must keep overlooking it and a few times I really could have used it. Thanks

Oh, uh, well, umm... :oops: Give us a few minutes. :wink:

dean_shan
07-22-2003, 09:45 PM
Thank you Fabrizio :werenotworthy: I have been trying to figure out how to do this for the longest time. Many thanks! :D

Duncan
07-22-2003, 09:52 PM
This will be very useful - thanks for spreading the wealth Jason! :)

medic119
07-22-2003, 10:44 PM
Instead of using page based code, I've been using mod_rewrite in the .htaccess file. I may not be exactly correct, but it works well.


RewriteCond %{HTTP_USER_AGENT} "Windows CE" [NC,OR] #Windows CE and Pocket PC
RewriteCond %{HTTP_USER_AGENT} "NetFront" [NC,OR] #PalmSource WebBrowser 2.0
RewriteCond %{HTTP_USER_AGENT} "Palm OS" [NC,OR] #Eudora Web Browser for Palm
RewriteCond %{HTTP_USER_AGENT} "Blazer" [NC,OR] #Handspring Blazer Browser
RewriteCond %{HTTP_USER_AGENT} "Elaine" [NC,OR] #RIM Devices
RewriteCond %{HTTP_USER_AGENT} "^WAP.*$" [NC,OR] #WAP Browsers
RewriteCond %{HTTP_USER_AGENT} "Plucker" [NC,OR] #Plucker Offline download client
RewriteCond %{HTTP_USER_AGENT} "AvantGo" [NC] #AvantGo Service
RewriteRule ^index\.php http://www.yourdomain.com/mobilepage.php [L,R]

lonesniper
07-22-2003, 10:50 PM
This is very helpful code. Thanks!!
Now all I need is some content on my site that people would want to read and read on their mobile device. :D

beyondallcom
07-22-2003, 11:10 PM
Instead of using page based code, I've been using mod_rewrite in the .htaccess file. I may not be exactly correct, but it works well.


RewriteCond %{HTTP_USER_AGENT} "Windows CE" [NC,OR] #Windows CE and Pocket PC
RewriteCond %{HTTP_USER_AGENT} "NetFront" [NC,OR] #PalmSource WebBrowser 2.0
RewriteCond %{HTTP_USER_AGENT} "Palm OS" [NC,OR] #Eudora Web Browser for Palm
RewriteCond %{HTTP_USER_AGENT} "Blazer" [NC,OR] #Handspring Blazer Browser
RewriteCond %{HTTP_USER_AGENT} "Elaine" [NC,OR] #RIM Devices
RewriteCond %{HTTP_USER_AGENT} "^WAP.*$" [NC,OR] #WAP Browsers
RewriteCond %{HTTP_USER_AGENT} "Plucker" [NC,OR] #Plucker Offline download client
RewriteCond %{HTTP_USER_AGENT} "AvantGo" [NC] #AvantGo Service
RewriteRule ^index\.php http://www.yourdomain.com/mobilepage.php [L,R]


do you replace ^index\.php with your main page in the root?

Thanks,
Kevin

dMores
07-22-2003, 11:21 PM
while i do know *some* asp, i believe the first code is for php.

can anyone "translate" that to asp?

medic119
07-23-2003, 12:42 AM
do you replace ^index\.php with your main page in the root?

Thanks,
Kevin

Yes..
Example: if the root page is mypage.html use ^mypage\.html

Be aware though, thats just a code snippet for that section of my .htaccess file.

You need to have the line RewriteEngine on somewhere near the top to turn on mod_rewrite and you have to have mod_rewrite installed on your apache server.

summoner
07-23-2003, 06:21 AM
I've tried the asp code given here, and the javascript, but apparently they don't work for WM2003... any ideas?

Ariel

DavidHorn
07-23-2003, 09:14 AM
Thank you very much! This'll be really useful for SmartGamer.org.

FredMurphy
07-25-2003, 03:57 PM
This could be useful - I've just found out that Microsoft's own ASP.NET controls (in particular the asp:LinkButton) which used to work with PIE on PocketPC2002 no longer work when accessing the page from PIE on WM2003! :roll:

Dalantech
08-05-2003, 02:32 PM
Excellent post!

Now I'm going to have to create a special set of mobile friendly templates for UBB Threads :)

entropy1980
08-05-2003, 02:35 PM
I am getting problems when trying to use the code.... namely nothing is happening, I have debugged it but it seems to not detect anything what am I missing? I am referring to the first set of code (the php not javascript). Thanks!!

codepunk
08-05-2003, 08:06 PM
The ASP.NET version (C#) is this:

if( Request.Browser("IsMobileDevice") == true )
{
Response.Redirect( "MobilePage.aspx");
}

Although I've read something somewhere that this messes up search engine crawlers.

-Mark Schmidt

Falstaff
10-28-2003, 02:19 AM
Does that first piece of code detect Palm OS, Linux, and Symbian browsers? Thanks

cklaszlo
01-29-2004, 05:28 PM
Steven, our Project Manager, noticed that the original auto detect code was missing some info.

Here is our fix and seams to work:


$ua = $HTTP_USER_AGENT;
if (stristr($ua, "Windows CE") or stristr($ua, "AvantGo") or stristr($ua,
"Mazingo") or stristr($ua, "Mobile") or stristr($ua, "T68") or stristr($ua,
"Syncalot") or stristr($ua, "Blazer") or stristr($ua, "NetFront"))
{
$DEVICE_TYPE="MOBILE";
}

if (isset($DEVICE_TYPE) and $DEVICE_TYPE=="MOBILE")
{
$location='http://pda.thrillnetwork.net/';
header ('Location: '.$location);
exit;
}

We have this running on our beta test site. We will be releasing the live site in the next few days. If you want to see the code working send me a PM and we can talk. Thanks.

Dalantech
03-10-2005, 10:38 PM
...for my site, and you need to define what $ua is before you can use it:

$ua = @find_environmental("HTTP_USER_AGENT");

Then you can do a string compare on the value of $ua to determine if someone is surfing from a mobile browser...

cklaszlo
03-11-2005, 08:24 PM
We have it running on our site now for the last year.

www.thrillnetwork.com

browse from a PDA links to:

mobile.thrillnetwork.com

We just added our complete Database of Rides and Parks :)

This works for Blackberry, PPC, Plam OS. Very cool. :)

twinleaf
01-30-2008, 05:32 AM
Hi,

I used Medic119's code and it worked great for several weeks, but then it stopped working (website behaved as the directives were not there-- did not redirect to the mobile folder). Could this have anything to do with the fact that my host upgraded to Apache 2.0?

Any insight would be appreciated.

Thanks.

beradrian
10-10-2008, 05:17 PM
Here are a few methods how to detect a mobile device: http://beradrian.wordpress.com/2008/10/10/mobile-device-recognition/

beradrian
10-11-2008, 12:23 AM
I described in here a few methods for detecting a mobile device: http://beradrian.wordpress.com/2008/10/10/mobile-device-recognition/