×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Regular Contributor
    Join Date
    Aug 2003
    Location
    uk
    Posts
    232

    HowTO: user agent fields and phone idenfication

    As no one has ever answered in any useful amount of detail, how to write WAP pages that have custom
    content for each mobile phone, or how to do a page redirrection for a specific phone, I have decided to make my findings public.

    Some keywords for people searching the forums:
    user agent fields
    phone identification
    device detection
    platform detection
    page redirrection

    To recap what the User Agent fields are:
    They are sent by the browser or phone, etc, that are trying to access the page (HTML or WML etc), and are
    available to the various server side scripting languages for parsing.

    Options for WAP are JSP (java server side scripting),
    Php and perl (probably). There probably others too,
    but one of these should be on most web servers.

    I chose Php because, JSP isn't installed on many web servers, and getting the people who host web servers to change anything is much like trying to poke your own eyes out with a blunt spoon.

    I discounted Perl because it is Evil!

    To test if your web server supports Php, create a file called test.Php with the following contents:
    ---
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title>PHP TEST</title>
    </head>

    <body>
    <HR>
    <p>
    HTTP_USER_AGENT=[<?Php echo $HTTP_USER_AGENT;?>]
    </p>
    <HR>
    <p><b>
    PHP TEST
    </b></p>
    <HR>
    <?Php phpinfo();?>
    <HR>
    </body>
    </html>
    ---
    Note: that the Php bits are enclosed in
    <?Php ... Php script .. ?>

    Php is actually preprocessing the html file before its sent.

    How do we use it ?

    Well to use it in WAP, the first part of your WAP file must be:
    ---
    <?Php
    // send WML headers
    header("Content-type: text/vnd.wap.wml");
    echo "<?xml version=\"1.0\"?>";
    echo "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\""
    . " \"http://www.wapforum.org/DTD/wml_1.1.xml\">";
    ?>

    <wml>
    ---


    Then inside one of your WML cards you can put:
    ---
    <?Php
    if (strstr($HTTP_USER_AGENT, "Nokia6610"))
    {
    echo "You are browsing with a Nokia 6610";
    }
    ?>
    ---
    Or alternatively using Php mixed mode:
    ---
    <?Php
    if (strstr($HTTP_USER_AGENT, "Nokia6610"))
    {
    ?>

    <p><b>You are browsing with a Nokia 6610</b></p>

    <?Php
    }
    ?>
    ---
    Going back to page redirrection, you should be able to see that you can simply provide a custom page for each phone, all in the same wml file instead of redirecting to a page for each phone.

    For more info on Php => google!

    A complete working wml page follows, called it something like phptest.php and load it up in your phone, web browser, etc:
    ---
    <?php
    // send wml headers
    header("Content-type: text/vnd.wap.wml");
    echo "<?xml version=\"1.0\"?>";
    echo "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\""
    . " \"http://www.wapforum.org/DTD/wml_1.1.xml\">";
    ?>

    <wml>
    <card title="php test" id="main">

    <p>
    <?php echo "HTTP_USER_AGENT=$HTTP_USER_AGENT";?>
    </p>

    <p>
    Agent=<?php echo substr($HTTP_USER_AGENT, 0, strpos($HTTP_USER_AGENT, "/"));?>
    </p>

    <?php
    if (strstr($HTTP_USER_AGENT, "MSIE") ||
    strstr($HTTP_USER_AGENT, "Mozilla") ||
    strstr($HTTP_USER_AGENT, "Opera") ||
    strstr($HTTP_USER_AGENT, "Lynx"))
    {
    ?>

    <p>This Page is for Phones, stop looking at it with a web brower, You Space Monkey!</p>

    <?php
    }
    ?>

    <p>Are you using a Nokia Phone ?</p>

    <?php
    if (strstr($HTTP_USER_AGENT, "Nokia"))
    {
    ?>

    <p> => You appear to be using a Nokia Phone .. Smashing!</p>

    <?php
    }else{
    ?>

    <p> => You are not using a Nokia phone - Shame on you!</p>

    <?php
    }
    ?>

    <p>Excellent!</p>

    </card>
    </wml>
    ---

    Hope this helps some people.
    Alex
    Last edited by alex_crowther; 2004-02-23 at 18:08.

  2. #2
    Registered User
    Join Date
    Jan 2004
    Location
    Beverly Hills
    Posts
    19

    That sort of works

    That is a partial answer and a partial solution. In reality, the problem is much more complex than that. You have only scratched the surface of the diversity of phones out there. There are so many different phones with poorly documented specs and user agent strings.

    If you really want to detect devices, there is only one solution that I know of: Chiral Software's Mobile Web Module. This is an Apache server module which has the latest device profiles built in. You can try it out yourself by going to chiralsoftware.net on any web browser or WAP phone. You'll see that you get the right content, and images come out the right way.

    Even cooler, it automatically transforms HTML into WML, so you have one set of files (text and images) and one URL and you get both wired and wireless users, correctly every time.

    You can buy the software outright or you can sign up with a hosting company which has the software installed, so you'll get it as part of your WAP hosting package. Let me know if you have ay questions.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    17
    hi alex_crowther!

    this looks nice, however, i'm not really knowledgable in PHP. And i didn't really get how you get the user agent. And i'm also a beginner in making a wap site. Could you give me some more advise or information how i could get this "user-agent"? is it something that is recognized automatically by programming languages like java?

    thanks a lot!

  4. #4
    Registered User
    Join Date
    Jan 2004
    Location
    Beverly Hills
    Posts
    19

    Header fields in Java

    Of course Java supports it:

    http://java.sun.com/products/servlet...html#getHeader(java.lang.String)

    String userAgent = request.getHeader("user-agent");

    will do the trick.

    But, really, the user-agent fields is not enough to tell you what to do. If you want something that really detects the vast majority of devices and knows their profiles and reformats html and images, take a look at: http://chiralsoftware.net . Otherwise you will be spending a lot of time to get something that just sort of works.

Posting Permissions

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