Приглашаем посетить
Чуковский (chukovskiy.lit-info.ru)

21.5 WML applications with CGI and databases

Table of Contents

Previous Next

21.5 WML applications with CGI and databases

21.5.1 Using Perl script to generate WML pages

From the structure of WAP and WML (see Fig. 21.1), we know that the communications between the WAP gateway and the WAP sites are using the standard HTTP. There is no reason why we cannot use CGI techniques on these WAP sites to deliver a WML page to the WAP gateway. In fact, in this section, we are going to do just that.

We will show you how to construct Perl and PHP scripts to deliver WML pages to a mobile device. Also, by opening up the CGI techniques to WML pages, a new chapter of applications can be created. All the skills that you have learned from this book are applicable to mobile devices. We will also show you how to connect to a MySQL database and build database applications at the end of this chapter. First, let's see how to develop a Perl script on a server that can deliver a WML page to the WAP gateway.

Like almost all server scripting languages, a Perl page is resident inside a server. When it is called, the server will execute all the Perl statements before sending the resulting page to the browser. To use Perl on the micro-browser of mobile devices, all you have to do is to instruct Perl to output all the necessary headers in WML.

Yes! The whole process is as simple as that. One thing you may need to change is the "Content-type" for WML.

As a reminder, when you send a Web page to a browser such as IE or NS using Perl, you need to send the magic string



Print "Content-type: text/html\n\n";

along with the Web page headers. For the WAP gateway, the magic string is



print "Content-type: text/vnd.wap.wml\n\n";

After this magic string, you can construct the usual WML headers by Perl and send them off. A typical code fragment in Perl is



print "<?xml version=\"1.0\"?>\n";
print "<!DOCTYPE wml PUBLIC \"-//WAPFORUM// DTD WML 1.3//EN\"
       \"http://www.wapforum.org/DTD/wml_1.3.xml\">\n";
print "<wml>\n";

Consider a simple "Hello World" example as follows:



Example: ex21-20.pl - Using Perl On WML Pages

 1: #! /usr/bin/perl
 2: print "Content-type: text/vnd.wap.wml\n\n";
 3:
 4: print "<?xml version=\"1.0\"?>\n";
 5: print "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.3//EN\"
 6:        \"http://www.wapforum.org/DTD/wml_1.3.xml\">\n";
 7: print "<wml>\n";
 8: print "  <card id=\"card1\" title=\"Perl Script\">\n";
 9: print "    <p>Hello World! </p>\n";
10: print "  </card>\n";
11: print "</wml>\n";

This is a simple "Hello World!" WML page generated by Perl script. After the Perl heading and the magic string in lines 12, we use a series of print statements in Perl to generate a WML page. As for all Perl programs, you may need to use

\" to generate the double quote " inside a print statement

\n to generate a new line

As you may say, there are too many print statements in the page. Can we use the here document feature of Perl? Yes! You can. Consider the following example:



Example: ex21-21.pl - Using Perl On WML Pages II

 1: #! /usr/bin/perl
 2: print "Content-type: text/vnd.wap.wml\n\n";
 3:
 4: print << "myWMLDoc";
 5:  <?xml version="1.0"?>
 6:  <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.3//EN"
 7:        "http://www.wapforum.org/DTD/wml_1.3.xml">
 8: <wml>
 9:  <card id="card1" title="Perl Script">
10:   <p>
11:     Hello, I know how to write
12:     Perl script to generate WML pages for
13:     mobile devices.
14:   </p>
15:  </card>
16: </wml>
17:
18: myWMLDoc

After the Perl heading and the magic string, the here document is defined in line 4. From line 5 to the end of the here document, you are free to write any WML statements. As a simple example, our WML page contains only one card to display one message.

If you activate this page using your mobile device as

http://www.pwt-ex.com/chap21a/ex2121.pl

you will see Fig. 21.50 on your mobile screen. This page can also be called within a WML page using the go element such as <go href="">.

Figure 21.50. Perl script and WML

graphics/21fig50.gif


You have now established a bridge to integrate Perl and WML together. All CGI techniques mentioned in Part IV related to Perl can also be applied to WML pages.

As a practical example, the following is a Perl program to build a WAP counter to count the number of visits to a WAP site.

21.5.2 Building a WAP page counter using Perl

To build a WAP page counter to count the number of visitors is straightforward. Basically, you need to open a counter file (a text file) and perform the following tasks:

  • Open the counter file and read the data when the page is called.

  • Increment the data by 1.

  • Store the data back to the file.

  • Generate the WML page to display the data.

In Chapter 15, we learned that a page counter can be built using Perl by the following code fragment (see ex15-07.pl):



#!usr/bin/perl
open(filehandle,"+<counter.txt");
$countNumber = <filehandle>;
$countNumber++;
seek(filehandle,0,0); ## rewind the file to the beginning
print (filehandle "$countNumber");
close(filehandle);

Now to generate a WML page to display the count data $countNumber is easy. An implementation is shown in the example below:



Example: ex21-22.pl - Building A WAP Page Counter With Perl

 1: #!usr/bin/perl
 2: open(filehandle,"+<counter.txt");
 3: $countNumber = <filehandle>;
 4: $countNumber++;
 5: seek(filehandle,0,0); ## rewind the file to the beginning
 6: print (filehandle "$countNumber");
 7: close(filehandle);
 8:
 9: print "Content-type: text/vnd.wap.wml\n\n";
10: print << "myWMLDoc";
11: <?xml version="1.0"?>
12: <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.3//EN"
13:        "http://www.wapforum.org/DTD/wml_1.3.xml">
14: <wml>
15: <card id="card1" title="$countNumber Visitor">
16:   <p>
17:      ABC WAP Site <br />
18:      All Bargain Holidays Are Here!
19:   </p>
20: </card>
21: </wml>
22:
23: myWMLDoc

Every time this page is called, the data ($countNumber) inside the text file counter.txt are read (see lines 23). After adding 1 to the variable $countNumber, the data are stored back to the text file so that we have an up-to-date counter.

The rest of the program is to generate a WML page to include the counter data $countNumber. This WML page contains only one card and the counting data are output in title field of card1 (line 15) so that they will appear at the top of the mobile screen. A screen shot of this example is shown in Fig. 21.51.

Figure 21.51. A WAP page counter using Perl

graphics/21fig51.gif


Another popular server scripting language is PHP. To generate a WML page using PHP is simple: all you need is to output WML statements using PHP echo or print functions. We will show you more than that. The following is a discussion of how to develop Mobile Internet applications with PHP.

21.5.3 WAP and/or Web browser detection with PHP

If you are running an online shop for the Web community, one simple and straightforward way to expand your business to the WAP community is to write some WML pages to introduce your business to that community or mobile phone users. In order to save the resources of the business and utilize existing facilities, the first thing you may want is a page to detect the environment of the users and redirect them to the appropriate pages (WAP or Web).

There are a number of ways to detect the browser type (WAP or Web). Many people perform the detection by searching the environment variable HTTP_USER_AGENT to see whether it contains any substring of the word "Mozilla." Consider the following example:



Example: ex21-23.php - WAP And WEB Browser Detection I

 1: <?PHP
 2:  $webredirect = "http://www.pwt-ex.com/chap21a/web.htm"; // A Web Page
 3:  $wapredirect = "http://www.pwt-ex.com/chap21a/wap.wml"; // A WML Page
 4:
 5:  if(strpos(strtoupper($HTTP_USER_AGENT),"ZILLA") > 0)
 6:  {
 7:    header("Location: ".$webredirect);
 8:  }
 9:  else
10:  {
11:    header("Location: ".$wapredirect);
12:  }
13: ?>

This PHP program appears clear and tightly written. Lines 23 define the two redirections to the Web and WAP pages depending on the browser type.

The if statement in line 5 performs the comparison. First, the program converts the environment variable $HTTP_USER_AGENT to upper case and then searches for the position of a substring "ZILLA." Comparison with "ZILLA" instead of the whole word "MOZILLA" is a good move to eliminate a zero value so that only a bigger than 0 condition is needed at the end of line 5.

Finally, the two header redirections are employed to redirect the user to the appropriate Web or WAP page.

From a practical point of view, this program is poorly written, since most micro-browsers are not Mozilla based. In addition, not all Web browsers in the market are Mozilla type. The program will have more value if the comparison is performed on the variable $HTTP_ACCEPT and looks for the MIME type. Consider the example below:



Example: ex21-24.php - WAP And WEB Browser Detection II

 1: <?PHP
 2:  $webredirect = "http://www.pwt-ex.com/chap21a/web.htm"; // A Web Page
 3:  $wapredirect = "http://www.pwt-ex.com/chap21a/wap.wml"; // A WML Page
 4:
 5:  if(strpos(strtoupper($HTTP_ACCEPT),"VND.WAP.WML") > 0)
 6:  {
 7:    header("Location: ".$wapredirect);
 8:  }
 9:  else
10:  {
11:    header("Location: ".$webredirect);
12:  }
13: ?>

In this PHP program, the comparison in line 5 is looking for a match with the MIME type of the WAP page. If the browser accepts the WAP type, it should have no problem displaying WML pages. Some screen shots of this example are shown in Figs 21.52 and 21.53.

Figure 21.52. WAP and Web browser detection I

graphics/21fig52.gif


Figure 21.53. WAP and Web browser detection II

graphics/21fig53.gif


This example works simply because the WAP gateway passes the HTTP_ACCEPT value to the program. Not all gateways are designed to pass this value to your site. In this case, you may need to do it the hard way. That is, to tackle the HTTP_USER_AGENT string directly to determine the actual identity of the phone. Before that, consider a simple PHP page:



Example: ex21-25.php - WAP Browser Detection

 1: <?
 2:  header("Content-type: text/vnd.wap.wml");
 3:  echo("<?xml version=\"1.0\"?>\n");
 4:  echo("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.3//EN\"
 5:     \"http://www.wapforum.org/DTD/wml_1.3.xml\">\n\n");
 6: ?>
 7: <wml>
 8:  <card id="init" title="Client Info">
 9:  <p>
10:    <?
11:     echo("HTTP_USER_AGENT" . $HTTP_USER_AGENT ."<br />");
12:    ?>
13:  </p>
14:  </card>
15: </wml>

This PHP program generates a WML page. The first statement (line 2) sends out the "Content-type" of a WML page. Lines 35 generate a complete WML page header so that cards and other WML elements can be defined.

In fact, this is a simple PHP script to print out the HTTP_USER_AGENT string to show the identity of the mobile phone (or device). The PHP echo statement in line 11 gets the string value and outputs it as ordinary WML text. If you run this program on Nokia's 7110 and 3330 phones, you will see Fig. 21.54 and Fig. 21.55 respectively.

Figure 21.54. Mobile phone identity I

graphics/21fig54.gif


Figure 21.55. Mobile phone identity II

graphics/21fig55.gif


From these figures, we can see that the HTTP_USER_AGENT variable for these Nokia phones carries the Nokia trademark. Therefore, this trademark can be used to detect the identity of the phone and the micro-browser type. At a minimum level, we can use it to detect the browser on Nokia phones. Consider the example below:



Example: ex21-26.php - More WAP Browser Detection

 1: <?PHP
 2:   $webredirect = "http://www.pwt-ex.com/chap21a/web.htm"; // A Web Page
 3:   $wapredirect = "http://www.pwt-ex.com/chap21a/wap.wml"; // A WML Page
 4:
 5:   if(strpos(strtoupper($HTTP_ACCEPT),"VND.WAP.WML") > 0)
 6:   {
 7:     header("Location: ".$wapredirect);
 8:   }
 9:   else
10:   {
11:     $browser=substr(trim($HTTP_USER_AGENT),0,4);
12:     if( $browser=="Noki" ||
13:         $browser=="Eric" ||
14:         $browser=="UP.B" )
15:     {
16:       header("Location: ".$wapredirect);
17:     }
18:     else
19:     {
20:       header("Location: ".$webredirect);
21:     }
22:    }
23: ?>

This example is a modification of ex21-25.php to add detection on some mobile phones. Line 11 is used to extract the first four characters of the variable HTTP_USER_AGENT. This string is used to compare with the following strings:

  • Noki Nokia phones

  • Eric Ericsson (or Sony Ericsson) phones

  • UP.B Motorola phones (most Motorola phones use UP.Browser)

If the gateway fails to deliver the HTTP_ACCEPT string and the browser type is picked up by these three types of phones, a redirection to a WAP page is activated in line 16. Otherwise, we assume that the redirection is to a Web page. You should put more phone detections inside the if statement defined in lines 1214. More information on mobile company trademarks and HTTP_USER_AGENT strings can be found on the site http://allnetdevices.com.

To be able to distinguish a WAP or Web browser is not just for redirection. The technique provides a fundamental framework for programmers to target specific devices. For example, a rich set of functions and features for mobile phones recommended by the WAP Forum is known as the Wireless Telephony Application Interface (WTAI) standard (www.wapforum.org). The WTAI specifications are dedicated to controlling your mobile phone with programming techniques. Functions recommended by the WTAI can be used if a WTAI-compliant micro-browser is detected.

As a simple example, consider the following WML go element:



<go href="wtai://wp/mc;12345678">Tom Tel:(12345678)</go>
<go href="wtai://wp/mc;22334455">Anna Tel:(12345678)</go>

If you develop a WML page with these lines, you can click on the underlined text and make a phone call to the people you want.

Have you thought about sending an email using your mobile phone? A WML page to send a short email message sometimes is quite handy since you can use it anywhere at any time. Also, the email message will be waiting in the recipient's inbox whenever he or she is available in the office or at home. That is, an email message will never get a "Busy Tone."

21.5.4 Emailing with your mobile phone: mobile email

The basic idea of sending email messages using a WAP or WML page is simple. First, you create a WML page with some text boxes to obtain the "To" and "Message" fields. The data are then sent to a server script such as Perl or PHP. Finally, all you have to do is to construct the server script to deliver the email for you.

Before we consider a WML page to deliver mobile email, we need to know how to pass variables from a WML page to CGI script. One simple way to do this is to use the query string. As a reminder, the following anchor <a> element can pass data from a WML or XHTML page to CGI script:



<a href="http://www.pwt-ex.com/chap21a/exAA-BB.php?var1=
   data1&var2=data2">Activate a PHP Script </a>

In normal circumstances, when the CGI script, e.g., exAA-BB.php, is called, it contains the following two name/variable pairs:



$var1 = data1
$var2 = data2

For some reason, a number of mobile phones, WAP gateways, and/or micro-browsers accept only one name/value pair of the query string. In this case, we may need to consolidate all the "From," "Subject," "To," and "Message"" fields of an email into one name/value pair as



<a href="http://www.pwt-ex.com/chap21a/mobile_email.php?
  QST=$from;$subject;$to;$message">Send Mobile Email</a>

It is only a simple process to separate the query string QST into variables using PHP. In this case, the delimiter is a simple semi-colon ";." Consider the mobile email example below:



Example: ex21-27.wml - Mobile Email Using WML

 1: <?xml version="1.0"?>
 2: <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.3//EN"
 3: "http://www.wapforum.org/DTD/wml_1.3.xml">
 4: <wml>
 5:  <card id="index" title="Mobile Email" newcontext = "true">
 6:  <p>
 7:   From:    <input name="Fr" value="jo@www.pwt-ex.com" maxlength="30" />
 8:   Subject: <input name="Su" value="Mobile Email" maxlength="20" />
 9:   To:      <input name="To" value="info@www.pwt-ex.com" maxlength="30" />
10:   Msg:     <input name="Mg" value="Mobile Email" maxlength="80" />
11:   <a href =
12:     "http://www.pwt-ex.com/chap21a/ex21-27.php?QST=$Fr;:;$Su;:;$To;:;$Mg">
13:    Send</a>
14:  </p>
15:  </card>
16: </wml>

Only 16 lines of code are needed to do this job. This WML page contains only one card with four text input fields. They are the usual email headers From, Subject, To, and Message corresponding to the text boxes defined in lines 7, 8, 9, and 10. Once these boxes are filled in and the Send button is clicked, the anchor <a> statement in lines 1113 is called. The following action (see line 12)

http://www.pwt-ex.com/chap21a/ex21-27.php?QST=$Fr;:;$Su;:;$To;:;$Mg

would activate the PHP program ex21-27.php with a query string containing all the email input data separated by a special delimiter ;:;.

The function of the PHP program ex21-27.php is simple. First, it will capture the query string and then separate and store it into variables $From, $Subject, $To, and $Body. After that a simple call to the PHP mail function



mail($To,$Subject,$Body,$Headers);

would deliver the email to the recipient. Since the email originated from a mobile phone (or device), we generally call it mobile email.

The program code of the PHP script ex21-27.php is listed below:



Example: ex21-27.php - The PHP Script For ex21-27.wml

 1: <?
 2:   header("Content-type: text/vnd.wap.wml");
 3:   echo("<?xml version=\"1.0\"?>\n");
 4:   echo("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.3//EN\"
 5:      \"http://www.wapforum.org/DTD/wml_1.3.xml\">\n\n");
 6: ?>
 7: <wml>
 8: <card id="PHPScript" title="Mobile Email">
 9: <p>
10:
11:   <?php
12:    list ($From, $Subject, $To, $Body) = split (';:;', $QST);
13:    $Headers = "From: " . $From;
14:
15:    if (mail($To,$Subject,$Body,$Headers))
16:    {
17:      echo ("The Mobile Email Has Been Sent To: " . $To);
18:    } else {
19:      echo ("Sorry!<br />");
20:      echo ("Unable To Send Your Mobile Email To: " . $To);
21:    }
22:  ?>
23:
24: </p>
25: </card>
26: </wml>

This PHP program is also easy to understand. Lines 16 are used to define the necessary headers for a WML page using PHP. After that, we define a WML card called "Mobile Email." Inside this card, a block of PHP statements are used to deliver the email.

First, the following PHP list command is used to extract the email data from the query string $QST into the $From, $Subject, $To, and $Body variables (line 12):



list ($From, $Subject, $To, $Body) = split (';:;', $QST);

Note that the delimiter used in ex21-27.wml must match the delimiter inside the split() function.

Since the normal PHP mail function mail() doesn't have a "From" field, the header variable $Header in line 13 is used to construct the address of the sender. Finally, a simple call to the mail() function would deliver the email. Depending on the returned value of the mail() function, a message is displayed to show the result of the delivery.

In this example, Jo sends an email message to John Smith on his mobile phone. The process is shown in Figs 21.5621.58. When John Smith arrives his office, an email message is waiting for him on his machine as illustrated in Fig. 21.59.

Figure 21.56. Mobile email I

graphics/21fig56.gif


Figure 21.58. Mobile email III

graphics/21fig58.jpg


Figure 21.59. Email arrives in the inbox of Outlook Express

graphics/21fig59.jpg


Figure 21.57. Mobile email II

graphics/21fig57.gif


Databases are important tools for many commercial and non-commercial applications. As the final section of this chapter, let's consider how to implement an m-business "Last Minute Offer" with a database.

21.5.5 M-business with a database

Databases and their applications are big subjects and have occupied a significant area in this book. In this section, we will only show you how to apply database (MySQL) techniques to implement an m-business.

The m-business we are going to build is called "Last Minute Offer" targeting mobile phone customers. Suppose the company has an online business (Web type) with a good customer database. The new m-business is offering a special discount on a single product for a very short period of time to its members with a mobile device. The site is members only and they will need a user name and password to log in. The user name and password will be checked against the values in the customer database.

For our step-by-step demonstration, we will implement this m-business in two modules. The first module is to implement a WAP framework of the site. This framework, basically, has the following functions:

  • Display the site "Last Minute Offer."

  • Obtain the user name and password of a member.

  • Check the user name and password against the data inside a customer database.

  • Allow only a valid customer to log in.

Due to the small-screen nature of most mobile phones, the interface of the m-business is a simple one. Consider the following WML page:



Example: ex21-28.wml - WML Interface For An M-Business

 1: <?xml version="1.0"?>
 2: <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.3//EN"
 3: "http://www.wapforum.org/DTD/wml_1.3.xml">
 4: <wml>
 5: <card id="index" title="Company Name" newcontext = "true">
 6: <p>Members Only Site
 7:  Name:      <input name="userId" value="johnsmith" />
 8:  Password:  <input name="passId" value="johnsmith" />
 9:  <a href="http://www.pwt-ex.com/chap21a/ex21-28.php?QST=$userId;:;$passId">
10:   login</a>
11: </p>
12: </card>
13: </wml>

This is a simple WML page with one card and two text boxes. The text boxes are used to obtain the user name and password. When the underlined text "login" is clicked on, the PHP program ex21-28.php is called with the following query string as input (see line 9):



ex2128.php?QST=$userId;:;$passId

Again, the user name and password are consolidated as one query argument. A simple split() function can be used to split them in the PHP program.

The PHP program ex21-28.php is the main processing engine for this example. This program is, in fact, a database application to perform the following tasks:

  • Obtain the input user name $userId and password $passId.

  • Connect to the MySQL database buss_db and the customer profile table cust_profile.

  • Execute an SQL statement to locate the $userId and obtain the related data.

  • Compare the password in the database against the variable $passId for a match.

This PHP program is listed below:



Example: ex21-28.php - The PHP Script For ex21-28.wml

 1: <?
 2:   header("Content-type: text/vnd.wap.wml");
 3:   echo("<?xml version=\"1.0\"?>\n");
 4:   echo("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.3//EN\"
 5:      \"http://www.wapforum.org/DTD/wml_1.3.xml\">\n\n");
 6: ?>
 7: <wml>
 8: <card id="card1" title="Company Name">
 9: <p>
10: <?php
11:
12:   list ($userId,$passId) = split (';:;', $QST);
13:
14:   if ($passId && $userId)
15:   {
16:     $passId = md5($passId);
17:
18:     $db = mysql_connect("www.pwt-ex.com", "johnsmith","johnsmith");
19:     mysql_select_db("buss_db",$db);
20:
21:     $query = "SELECT * FROM cust_profile WHERE user_Id='$userId'";
22:     $result = mysql_query ($query) or die ("SQL Query Error..");
23:
24:     $row = mysql_fetch_array ($result);
25:
26:     if ($passId == $row['password'])
27:     {
28:         echo("Thank you! <br /> Enjoy Your Visit");
29:     }
30:     else
31:     {
32:        echo "Sorry! Wrong Password <br />";
33:     }
34:     mysql_free_result ($result);
35:   }
36:   else
37:   {
38:      echo "Sorry! Access Denied <br />";
39:   }
40: ?>
41: </p>
42: </card>
43: </wml>

The first six lines are used to construct the necessary WML header using PHP. The rest of the program contains one card and one PHP program block (lines 1040).

First, the user name and password are extracted from the query string $QST in line 12. Lines 1819 are used to make the connection to the database buss_db and the customer profile table cust_profile. The next two statements (lines 2122) are used to execute the SQL statement



SELECT * FROM cust_profile WHERE user_Id='$userId'

so that the database information matches the $userId stored in array variable $result. By calling the function mysql_fetch_array() in line 24, the password data from the database are stored in the variable $row['password']. This value can then be used to compare to the $passId (line 26). If there is a match, we know that the user is a member of the site: if there is no match, we have a wrong password case. Please note that the passwords are compared as MD encrypted strings. Some screen shots of this example are shown in Figs 21.60 and 21.61.

Figure 21.60. A login page using WML I

graphics/21fig60.gif


Figure 21.61. Login successful

graphics/21fig61.gif


Now we can implement the second module to change this framework into an m-business site. First, we would like to change the company name to "Last Minute Offer." Instead of the "Thank you" message as in 21.61, a stunning special offer is needed to attract the customers. For this example, we use the following business message:



500x DVD Players
40 Dollars Each
Buy One?

Right under this message, you may also need to construct Yes and No buttons. This message and the buttons can be implemented as the following PHP strings:



$llst = "500x DVD Players <br /> 40 Dollars Each<br />Buy One?<br />";
$yes = "<a href=\"http://www.pwt-ex.com/chap21a/ex2129.php?QST=yes;:;"
             .$userId.";:;" .$passId."\">Yes</a>";
$no = "<a href=\"http://www.pwt-ex.com/chap21a/ex2129.wml\">No</a>";

For example, when you execute the echo function echo($yes) in a WML page, you will see an underlined text "Yes." This text is in fact an anchor element <a> to link to the program ex21-29.php and ultimately trigger the purchase.

First, let's see the interface part of this m-business page. The page is very similar to ex21-28.wml and is listed below:



Example: ex21-29.wml - Implement The M-Businss "Last Minute Offer"

 1: <?xml version="1.0"?>
 2: <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.3//EN"
 3: "http://www.wapforum.org/DTD/wml_1.3.xml">
 4: <wml>
 5: <card id="index" title="Last Minute Offer" newcontext = "true">
 6: <p>Members Only Site <br />
 7:   Name: <input name="userId" value="johnsmith" />
 8:   Password: <input name="passId" value="johnsmith" />
 9:   <a href=
10:   "http://www.pwt-ex.com/chap21a/ex21-29.php?QST=no;:;$userId;:;$passId">
11:    login</a>
12: </p>
13: </card>
14: </wml>

If you compare this page with ex21-28.wml, you will find that the only major difference is in line 10. That is, the query string QST has one more argument. The first argument of QST contains the word "no." This is used to indicate that the user hasn't bought anything yet.

In order to accommodate the business, we need to convert the example ex21-28.php program to ex21-29.php and make some major changes. Apart from the special offer message to attract customers, you may also need to perform some file operations to store the order permanently. Consider the program code as follows:



Example: ex21-29.php - The PHP Script For ex21-29.wml

 1: <?
 2:   header("Content-type: text/vnd.wap.wml");
 3:   echo("<?xml version=\"1.0\"?>\n");
 4:   echo("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.3//EN\"
 5:      \"http://www.wapforum.org/DTD/wml_1.3.xml\">\n\n");
 6: ?>
 7: <wml>
 8: <card id="card1" title="Last Minute Offer">
 9: <p>
10:  <?php
11:
12:   list ($bu,$userId,$passId) = split (';:;', $QST);
13:
14:   $llst = "500x DVD Players <br /> 40 Dollars Each<br />Buy One?<br />";
15:   $buyone = "One DVD Player -- 40 Dollars";
16:
17:   $yes = "<a href=\"http://www.pwt-ex.com/chap21a/ex21-29.php?QST=yes;:;"
18:              .$userId.";:;" .$passId."\">Yes</a>";
19:   $no = "<a href=\"http://www.pwt-ex.com/chap21a/ex21-29.wml\">No</a>";
20:
21:   if ($passId && $userId)
22:   {
23:    $passId = md5($passId);
24:
25:    $db = mysql_connect("www.pwt-ex.com", "johnsmith","johnsmith");
26:    mysql_select_db("password",$db);
27:
28:    $query = "SELECT * FROM password WHERE name='$userId'";
29:    $result = mysql_query ($query) or die ("SQL Query Error..");
30:
31:    $row = mysql_fetch_array ($result);
32:
33:    if ($passId == $row['password'])
34:    {
35:      if ($bu =="no")
36:      {
37:        echo($llst);
38:        echo($yes . " ------ ". $no);
39:      }
40:      if ($bu =="yes")
41:      {
42:        $today = getdate();
43:        $timeSt = $today['mday'] ." ". $today['month'] ." ".
44:                  $today['year'] ." ". $today['hours'] .":".
45:                  $today['minutes'].":". $today['seconds'];
46:
47:        $ordSt = "Ordered by: ".$userId .
48:                 "\r\n -- Password Check OK --\r\n".
49:                 $timeSt ."\r\n". $buyone ."\r\n";
50:
51:        $filename ="mobileorder.txt";
52:        $fd = fopen($filename,"a");
53:        fwrite($fd,$ordSt);
54:        fwrite($fd,"\r\n==============================\r\n");
55:        fclose($fd);
56:
57:      echo("Thank you and Confirmed!<br/>Delivery Within 7 Days<br />");
58:      }
59:    }
60:   else
61:   {
62:     echo "Sorry! Wrong Password <br />";
63:    }
64:    mysql_free_result ($result);
65:   }
66:   else
67:   {
68:      echo "Sorry! Access Denied <br />";
69:   }
70:   ?>
71: </p>
72: </card>
73: </wml>

The first part of this program is to construct the strings

$llst

Special offer (or discount) string (line 14)

$yes

The Yes button (line 17)

$no

The No button (line 19)

$buyone

When the "Yes" button is clicked, this string will be written into a file as a permanent record (line 15)


The major part of this program is in lines 3558. When this program is called by ex21-29.wml, the variable $bu carries a "no" value to indicate that no purchase has been made. In this case, line 37 is used to display the special offer message stored in variable $llst. The next line is to display the Yes and No buttons together waiting for a user input.

When the user presses the Yes button, the anchor element <a> in lines 1718 turns out to be



<a href="http://www.pwt-ex.com/chap21a/ex2129.php?
    QST=yes;:;$userId;:;$passId">Yes</a>"

Therefore the same program ex21-29.php is called with a "yes" value stored in the variable $bu. This situation will lead to the execution of the PHP program block defined in lines 4058. The tasks of this program block are:

  • Build the data and time string $timeSt (lines 4245) so that the purchase order has a time stamp.

  • Build the ordering string $ordSt. This order string contains the following detailed information:

    - Who makes the order.

    - When the order was made.

    - Password checking status.

    - Details of the special offer.

When this information is ready, the following simple file operation would write the purchase order into the file mobileorder.txt as a permanent record:



$filename ="mobileorder.txt";
$fd = fopen($filename,"a");
fwrite($fd,$ordSt);
fwrite($fd,"\r\n==============================\r\n");
fclose($fd);

Together with the information in the database, the records in the file can then be used for delivery and making charges. Some screen shots of this example are shown in Figs 21.6221.64. Some of the records inside the file mobileorder.txt are shown in Fig. 21.65.

Figure 21.62. The login page for M-Business

graphics/21fig62.gif


Figure 21.64. Confirmation message for the order

graphics/21fig64.gif


Figure 21.65. The permanent purchase record

graphics/21fig65.gif


Figure 21.63. Login successful

graphics/21fig63.jpg


    Table of Contents

    Previous Next