Team LiB
Previous Section Next Section

Serving WAP Content

As soon as you master WML, it is time to add the strengths and possibilities to the list of ingredients of this chapter. There are two aspects to that. First, the Web server must be configured properly to deliver WML content as expected by the clients. And second, special PHP code is used to create truly dynamic WML pages.

MIME Types

As with any other "foreign format" you create with PHPwhether it is JPEG or PNG graphics, PDF documents, or SWF moviesyou always have to send the correct MIME types along with your mobile content. Table 19.7 shows the relevant MIME types for WAP files:

Table 19.7. MIME Types for WAP Files

File Extension

Associated MIME Type







The file extension .wmls is used for WMLScript documents.

Web Server Configuration

First, your Web server has to send out the correct MIME types automatically; otherwise, you will get an error message in most of the available WAP browsers. Not all documents on your website may be dynamic, so it would create an unnecessary overhead to use PHP scripts for any document. Therefore, there might also exist files with the extension .wml you have to serve correctly. Images are mostly not created through scripts, so their MIME type must also be provided.

If you are using the Apache Web server, you will find a file mime.types in the conf directory of your Apache installation. Add these lines:

text/vnd.wap.wml     wml
text/vnd.wap.wmlscript     wmls
image/vnd.wap.wbmp     wbmp


Recent versions of Apache come automatically with these settings, so no further configuration is needed.

Microsoft's Personal Web Server (PWS), part of Windows 95, 98, and NT Workstation (and, with some limitations, on Windows ME), can also be configured to send out the new MIME type. This has to be registered systemwide. To do so, fire up Windows Explorer and select View, Options (on more recent versions, Tools, Options), and then click File Types. Click New and create the MIME types for the file extensions .wml, .wmls, and .wbmp. Restart the Web server.

The "big brother" of the PWS is Microsoft's IIS, originally translated as Internet Information Server, now Internet Information Services. Here, you have to go to the Control Panel, Administrative Tools, Internet Services Manager, and select the Properties entry in the context menu of the current website. Choose the HTTP Headers tab and click New Type. Now you can create the three MIME types (see Figure 19.13). Unlike with PWS, these MIME types are only for the Web server but are not (unnecessarily) created for the whole system. Finally, restart the server.

Figure 19.13. Setting the MIME Type for IIS.

After that, it's time to do a test of your configuration. Copy one of the .wml files from the previous sections to a directory on your Web server and load the file in one of your emulators (preferably the Nokia emulator because this is very strict about MIME types and will notify you of any error). If you see the content, you are ready for further examples.

Setting MIME Type from PHP

From a PHP point of view, you have to send out the MIME type manually. For a WML file, this can be done as follows:

    header("Content-type: text/vnd.wap.wml");

Remember that since the MIME type is part of the HTTP header, the header() function must be called before any WML output is sent to the browser. Alternatively, turn on output buffering. Listing 19.10 is a simple example where the whole WML is sent using PHP (the result can be seen in Figure 19.14):

Listing 19.10. A First PHP-Powered WML Page
    header("Content-type: text/vnd.wap.wml");
    $datetime = date("Y-m-d H:i:s");

    echo <<<END
    <?xml version="1.0"?>
    <card id="card1">
        WML created dynamically at $datetime

Figure 19.14. The current date and time is displayed.

Client Detection

Another important aspect is to detect whether a mobile device is present. This enables you to create a hybrid website. When a standard Web browser visits the home page, it is shown the actual website. If it's a WAP browser, the client is redirected to the special WAP site.

One way to do that is to read out the browser's identification string ($_SERVER["HTTP_USER_AGENT"]) and compare that to a list of known WAP browsers. However, this method requires frequent updating or good guessing. A much easier way is to check another server variable: $_SERVER["HTTP_ACCEPT"]. This contains a list of natively supported MIME types of the client. If this list contains the WML MIME type, text/vnd.wap.wml, this list is most likely capable of displaying WML files. If not, the device probably does not support mobile content. However, not all clients send out a correct value for HTTP_ACCEPT.

Probably the best way is to use a combination of both methods. Check for the WML MIME type, and if that does not work, check for the most widely used phone vendors (for example, Nokia).

The following script (Listing 19.11) does exactly this checking. If it is successful, that is, if the client supports WML content, the user is redirected to the (fictitious) WML homepage index.wml.

Listing 19.11. Web Browsers Get the Content, WML Users Are Redirected
    if (strpos($_SERVER["HTTP_ACCEPT"], "text/vnd.wap.wml") !== false ||
        strpos($_SERVER["HTTP_USER_AGENT"], "Nokia") !== false) {
      header("Location: index.wml");
    <!-- here goes "real" HTML content -->


A good technique is to use this detection on the home page only and to offer a special WAP-only home page (for example, using a dedicated third-level domain such as The reason is that the detection script could fail. For this case, you could offer an entry page that works all the time.

Displaying Graphics

One possible problem might arise when WBMP graphics are used and you do not have direct access to the Web server's configurationfor example, if your hoster forbids that. Without the correct MIME type, the WBMP graphics are not correctly displayed in the client browser, regardless of the file extension.

In this case, however, you can help yourself with a little PHP. The idea is simple: Read in a WBMP file with a PHP script and send it to the browser, enriched with the correct MIME type. The PHP function get_file_contents() (before PHP 4.3.0, readfile() can be used) comes in very handy:

    header("Content-type: image/vnd.wap.wbmp");

The following script (see Listing 19.12) takes the filename from the URL (GET parameter), reads in the graphic, and returns it with the correct MIME type:

Listing 19.12. WBMP Graphics Are Sent Out with the Correct MIME Type
    $filename = $_GET["img"];
    $filename = preg_replace("/|\\|:", "", $filename);
      //escape directory traversal parameters
    header("Content-type: image/vnd.wap.wbmp");

Call this script providing the URL of the graphic in the GET parameter src:


    Team LiB
    Previous Section Next Section