1. Trang chủ
  2. » Công Nghệ Thông Tin

Secure PHP Development- P147 pptx

5 186 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 103,51 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Testing the Voting Tool The first step in testing y our vote application is to develop a poll form.. In this sam-ple case, we will develop a simsam-ple poll form that asks voters whether

Trang 1

3 Install the VOTE applications From the ch20directory of the CD-ROM, extract ch20.tar.gzin %DocumentRoot% This will create a directory called vote in your document root Configure %DocumentRoot%/vote/ apps/vote.conffor path and database settings The applications are installed in the %DocumentRoot%/vote/appsdirectory, and the templates are stored in %DocumentRoot%/vote/apps/templates

Your MySQL server is hosted on the intranet Web server; therefore, it can

be accessed via localhost However, if this is not the case, you can easily modify the database URLs in each application’s configuration files For example, the vote.conf file has a MySQL database access URL such as the following:

$VOTE_DB_URL = ‘mysql://root:foobar@localhost/VOTE’; Suppose, for example, that your database server is called db.domain.com, and that the username and password for accessing the VOTE database are adminand db123, respectively (You will create both during this installa-tion process.) In such a case, you would modify the database access URL

in the vote.confconfiguration file as follows:

$VOTE_DB_URL =

‘mysql://admin:db132@db.domain.com/VOTE’;

4 Set file/directory permissions Make sure you have changed file and

direc-tory permissions such that your internet Web server can access all the files Once you have performed the preceding steps, you are ready to test your application

Testing the Voting Tool

The first step in testing y our vote application is to develop a poll form In this sam-ple case, we will develop a simsam-ple poll form that asks voters whether they like the current Web site This form, which is provided on the CD-ROM in (ch20/apps/ vote/sample_polls/website_poll.html), is shown in Figure 20-3

Figure 20-3: A sample Web site poll form.

Trang 2

If you examine the source of this Web form, you will notice the following HTML form code:

<form action=”/vote/apps/vote.php” target=_blank method=”POST”>

<font face=”Verdana” size=”1”>How do you rate this site? <p>

<input type=radio name=”vote” value=”1”>Great, very informative<br>

<input type=radio name=”vote” value=”2”>Good, has good info <br>

<input type=radio name=”vote” value=”3”>OK, needs a bit of improvement<br>

<input type=radio name=”vote” value=”4”>Poor, needs a lot of improvement

<p>

<input type=submit value=”Vote”>

<input type=hidden name=”poll_id” value=”1”>

</font>

</form>

Notice that the form action line is set to /vote/apps/vote.php, as it is needed to call the vote application In addition, note that each vote radio button is called

“vote” and has a numeric value (1–4) This is needed to collect vote data Finally, note a hidden form field called poll_id, which is set to 1 This number identifies the form in the vote.conf file’s $choicesPerPollarray, which is shown here:

$choicesPerPoll = array(

//POLL ID => NUMBER OF CHOICES

1 => 4,

2 => 7 );

This array in vote.conf determines the maximum number of options per polling form Here, our Web site polling form (poll_id 1) has four options, as shown in the aforementioned HTML form, so the $choicesPerPollarray has the same number specified

Now, if you select any of the voting options for the Web site form and click the Vote button, your vote will be stored in the VOTEStable in the VOTE database

You will be given a cookie so that you cannot vote again until the COOKIE_

EXPIRATION_TIMEtime specified in vote.confexpires

As soon as you click the Vote button, you will see a pop-up window that shows the current poll results (i.e., including your vote) This page is shown using a results template stored in the templates directory (%DocumentRoot%/vote/

apps/templates) The name of the template is specific to each poll_id For exam-ple, a poll form with poll_id must have a template called 001.html in the

%DocumentRoot/%vote/apps/templates directory Because each poll has its own results template, you can customize each poll’s results as desired

Trang 3

The basic structure of a results template is as follows:

<! BEGIN mainBlock >

{1_VOTE_COUNT} {1_VOTE_PERCENT}

{2_VOTE_COUNT} {2_VOTE_PERCENT}

{n_VOTE_COUNT} {n_VOTE_PERCENT}

{TOTAL_VOTES}

<! END mainBlock >

Each of the tags within the braces is replaced with respective vote data For example, {1_VOTE_COUNT} is replaced with the total number of votes cast for option #1 in a poll The {1_VOTE_PERCENT}tag is replaced with the percentage of votes cast for option #1 in a poll The {TOTAL_VOTES} tag is replaced with the grand total of votes cast in a poll Figure 20-4 shows a sample results page for the Web site poll described in the preceding example

Figure 20-4: A sample Web site’s poll results.

So far, our example poll form has used multiple radio button options However, the vote tool also supports multiple checkbox options, for polls in which you want visitors

to cast multiple votes that identify their preferences from a group of items For exam-ple, Figure 20-5 shows a poll form that asks users to select one or more languages This form can be found in the sample_pollsdirectory as language_poll.html

Trang 4

Figure 20-5: A sample language poll form using checkboxes.

The source for this form looks as follows:

<form action=”/vote/apps/vote.php” target=_blank method=”POST”>

What languages do you write code? (check all that applies)<p>

<input type=checkbox name=”vote[]” value=”1”>PHP<br>

<input type=checkbox name=”vote[]” value=”2”>Perl<br>

<input type=checkbox name=”vote[]” value=”3”>C<br>

<input type=checkbox name=”vote[]” value=”4”>C++<br>

<input type=checkbox name=”vote[]” value=”5”>Java<br>

<input type=checkbox name=”vote[]” value=”6”>Python<br>

<input type=checkbox name=”vote[]” value=”7”>Smalltalk<br>

<input type=submit value=”Vote”<br>

<input type=hidden name=”poll_id” value=”2”>

Here, notice that the vote field name is not votebut vote[], to indicate that we are returning an array of options The values are still numeric

When this poll form is submitted with multiple selections, each vote is added in the database Figure 20-6 shows an example results page (displayed using tem-plates/002.html)

Trang 5

Figure 20-6: A favorite language poll results page.

Summary

In this chapter, you learned how to develop a vote application that could be used to poll your Web site visitors about issues related to your Web site or other matters about which you are interested to know their opinions This is a nifty tool to have for most Web sites

Ngày đăng: 07/07/2014, 07:20

TỪ KHÓA LIÊN QUAN