hyper/boʊl/e

a delightful dish of Studio Hyperset news and information

Detecting and automatically redirecting website visitors who visit a standard webpage / website on mobile, handheld, wireless, PDA, or cel / cell phone devices / browsers to a mobile version of the webpage or website

Posted by Quimby Melton on October 6, 2006

For an up-to-date list of posts/comments on this topic and a link to the most current version of the script(s), visit the MOBILE REDIRECT CODE DISCUSSION INDEX.

In developing the Studio Hyperset mobile website, I must have searched every combination of the following word group: “mobile, redirect, browser, automatic, detect, wireless, device, handheld, PDA, cell, cel, phone.” Just imagine the host of indefinite and definite articles I had to scatter in there like pepper to make phrases in the Google box!

I was trying to find hints on and/or scripts that would allow me to detect and automatically redirect website visitors who visited a standard webpage / website on mobile, handheld, wireless, PDA, or cel / cell phone devices / browsers to a mobile version of the webpage or website.

I found next to nothing; but using these three extremely helpful sites, Zytrax.com, a Nokia discussion forum, and a MSDN blog called Pla.NET Southeast!, I think I was able to piece together three bits of code that will

  • scan a visitor’s platform (using .asp HTTP_USER_AGENT strings)
  • scan a visitor’s browser type (using .php USER_AGENT strings)
  • and generally check to see what type of device the visitor is using

I hope these will allow me to detect and automatically redirect website visitors to the standard Studio Hyperset homepage on mobile, handheld, wireless, PDA, or cel / cell phone devices / browsers to the Studio Hyperset mobile page.

I want to share these scripts for development and troubleshooting; and in the best humanistic spirit of the WWW, I wanted to make them available in the hopes that others who share the need to detect and automatically redirect website visitors who visit a standard webpage / website on mobile, handheld, wireless, PDA, or cel / cell phone devices / browsers to a mobile version of the webpage or website will be saved the trouble I went through.

I’ve posted the scripts as a .zip file here: http://www.studiohyperset.com/resources/redirect.zip I make no proprietary claim over these scripts whether to sell them or claim credit from them. All I ask those who use the script to do is post a comment that you used the scripts and whether or not (and how) you improved them. This way, I can put out additional, improved versions of the scripts or, if necessary, drop one or two to concentrate on developing the remaining one or two.

About the scripts

mobile.php
Drawing on the list of Zytrax Mobile Browser ID (User-Agent) Strings, this .php file asks the visitor’s server to identify the browser s/he is using. If it’s one of the mobile browsers listed (Opera Mini, AvantGo, ReqwirelessWeb, &tc.), the user is redirected to the mobile version of Studio Hyperset. If the user’s browser isn’t on the list, s/he is passed on to the standard homepage. Of the three, I have the most faith in this one as the User Agents are well defined and quite specific.

mobile.asp
Drawing on the .asp code listed on the Nokia discussion forum – having added quite a few additional platforms to the list myself – this .asp file asks the visitor’s server to identify the platform s/he is using. If it’s one of the mobile platforms listed (Palm, PocketPC, Samsung, &tc.), the user is redirected to the mobile version of Studio Hyperset. I have a bit less faith in this one because it’s a general, trade-name list of platforms rather than a specific list of codes and the like. Still, though, it will probably work if the server can identify keywords in the User Agent string.

mobile2.asp
I’d be surprised if this one actually works alone. Still, I’ve included it as a catch all should the other two scripts miss something. The “if (Request.Browser["IsMobileDevice"] == “true” )” string just seems too simplistic to actually work. It’s too general, especially when compared to the specificity of the “mobile.php” script.

Remember, as the the pace of mobile technology development continues to increase, more and more platforms and browsers will need to be added to the list. I encourage us all to stay on top of these developments and add platforms and browsers to these code strings.

I invite comments, suggestions, additions, and ideas.

I hope this fills the void in the Internet where heretofore easy-to-use, reliable scripts of this sort could not be easily found.


23 Responses to “Detecting and automatically redirecting website visitors who visit a standard webpage / website on mobile, handheld, wireless, PDA, or cel / cell phone devices / browsers to a mobile version of the webpage or website”

  1. [...] Continuing the train of thought from an earlier posting, here’s a reliable and extensively populated Javascript browser sniffer: http://jsbrwsniff.sourceforge.net/index.html. However, it doesn’t redirect visitors, but one fluent in JS could insert this into the code. [...]

  2. [...] Updating this posting of October 6th, the .asp and .php user_agent scripts have been “finalized” in the Studio Hyperset webpage. Inserted into the pole position of the <head> section of the index page using the following three <script> tags, these scripts are called when the page loads: [...]

  3. [...] This is the final posting of a conversation begun October 8th and continued on November 12th re: detecting and automatically redirecting website visitors who visit a standard webpage / website on mobile, handheld, wireless, PDA, or cel / cell phone devices / browsers to a mobile version of the webpage or website. [...]

  4. The following code is a supervised version of
    your snippet. Place the following code in first
    line of your PHP/HTML pages and mobile devices
    will be redirected to $mobile

    0)
    {
    header(“location: $mobile”);
    echo ‘Mobile Device detected’;
    break;
    }

    }
    ?>


    …put your original html/php here…

  5. [...] Mobile Redirect Code Update February 5th, 2007 Thanks to a tip from Ralph, add this code to the header of your HTML/PHP homepage to reference the $mobile string in the [...]

  6. Adriaan said

    I have tried the code on my html website but it does not work.
    the code above is printed in my screen waht am i doing wrong?

  7. Adrian,

    This comment is a little vague, I’m afriad. Let me know the steps you’ve taken to implement the code, and I can be of more assistance. Also, the code printing on your screen is evidence that you’ve not called it correctly in the head of your html document.

    Start your troubleshooting there.

  8. hidip said

    thanks

  9. frank said

    One thing is to redirect, but is there code that will show something different ONLY on mobile devices?

    For example you can put within a NOSCRIPT tag “Welcome cell users with no javascript END NOSCRIPT

    BUt that wont work for ever cellphone. But I wish it did.

    Any thoughts on how to keep them on the main page but write code to show them something different?

  10. I don’t think there’s any way to make a certain portion of a certain page visible to only one browser or only one type of device. Perhaps the best thing would be to redirect certain devices (i.e., non-Javascript enabled phones) to a specific site just for them using the mobile redirect code. Others might have better ideas of what to do.

  11. Nicholas said

    Frank: There is a way to do this. You would set up a stylesheet for the web, then one for mobile. What you would do is put the hidden content in say a div. On the web style sheet, you would add the attribute “display: none;”. This would hide that div from anybody using a computer, but unless you have that same attribute on the same div in the mobile style sheet, it will show properly.

  12. Thanks for the tip, Nicholas.

  13. Ruben said

    Hi

    Sry for my lack of understanding here, but i couldn’t make any of the codes/script to work, could you please print me a whole version of the code inplantet in a html docoument? if thats possible?

    Thanks

  14. pda said

    pda…

    En mi opinión el iphone es el primer móvil de la próxima generación. ¿ La prueba? Me choca ver los móviles de toda la vida, son tan rudimentarios, feos. Sólo sirven para llamar y sms, el 99% de la gente no lo usa para otra cosa. Con el iphone es distin…

  15. david said

    Have you tryed a handset detection service like handsetdetection.com?

  16. [...] Redirect a mobile/PDA to a “lite homepage” Redirecting visitors who visit a standard webpage to a mobile version [...]

  17. [...] topic take a look at thefollowing links: Redirect a mobile/PDA to a “lite homepage” Redirecting visitors who visit a standard webpage to a mobile version…and in particular if you are a PHP, ASP programmer take a look here: Device detection using [...]

  18. [...] How write web page for smartphones, iPhones, etc? http://studiohyperset.wordpress.com/…-webpage-or-w/ and http://www.askdavetaylor.com/detect_…te_server.html will give some clues. Regards JIGNESH [...]

  19. roxminu said

    Forgive me for being so dumb.

    Should this be then the beginning of mobile.php?:

    <?php
    header(“location: $http://www.tomykospa.com/celular/”);
    echo ‘Mobile Device detected’;
    break;
    if(

    …and then all the rest?

    And should this mobile.php be the starting page?: index.php? for my web site?

    I would appreciate your help clarifying this.

    Thank you very much.

    Regards

  20. You’ve got it, Roxminu.

  21. roxminu said

    Nop, it doesn’t work.

    Check out: http://www.tomykospa.com

    It has the code to redirect, if you are using a cellphone, to:

    http://www.tomykospa.com/celular

    But it is not working.

    Please help!

  22. none said

    userAgent = Request.ServerVariables(“HTTP_USER_AGENT”)
    userAgent = lcase(userAgent)

    if Instr(userAgent, “mozilla”) <= 0 then
    if Instr(userAgent, "alcatel") then
    Response.Redirect("/wml/index.asp")
    elseif Instr(userAgent, "ericsson") then
    Response.Redirect("/wml/index.asp")
    elseif Instr(userAgent, "nokia") then
    Response.Redirect("/wml/index.asp")
    elseif Instr(userAgent, "panasonic") then
    Response.Redirect("/wml/index.asp")
    elseif Instr(userAgent, "phillips") then
    Response.Redirect("/wml/index.asp")
    elseif Instr(userAgent, "samsung") then
    Response.Redirect("/wml/index.asp")
    elseif Instr(userAgent, "sanyo") then
    Response.Redirect("/wml/index.asp")
    elseif Instr(userAgent, "wml") then
    Response.Redirect("/wml/index.asp")
    elseif Instr(userAgent, "up") then
    Response.Redirect("/wml/index.asp")
    elseif Instr(userAgent, "mot") then
    Response.Redirect("/wml/index.asp")
    elseif Instr(userAgent, "wap") then
    Response.Redirect("/wml/index.asp")
    elseif Instr(userAgent, "ppc") then
    Response.Redirect("/ppc/index.asp")
    elseif Instr(userAgent, "ce") then
    Response.Redirect("/ppc/index.asp")
    end if
    end if

  23. Hey – whats up. Thanks a bunch for the blog. I’ve been digging around for info, but i think i’m getting lost!. Google lead me here – good for you i guess! Keep up the good work. I will be coming back in a couple of days to see if there is any more info.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>