php_mysql_apache [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

php_mysql_apache [Electronic resources] - نسخه متنی

Julie C. Meloni

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







Redirecting the User


Our simple script still has one major drawback. The form is reloaded whether or not the user guesses correctly. The fact that the HTML is hard-coded makes it difficult to avoid writing the entire page. We can, however, redirect the user to a congratulations page, thereby sidestepping the issue altogether.

When a server script communicates with a client, it must first send some headers that provide information about the document to follow. PHP usually handles this for you automatically, but you can choose to send your own header lines with PHP's header() function.

To call the header() function, you must be sure that absolutely no output has been sent to the browser. The first time content is sent to the browser, PHP sends out headers and it's too late for you to send your own. Any output from your document, even a line break or a space outside of your script tags, causes headers to be sent. If you intend to use the header() function in a script, you must make certain that nothing precedes the PHP code that contains the function call. You should also check any libraries that you might be using.

Listing 9.8 shows typical headers sent to the browser by PHP, beginning with line 3, in response to the request in line 1.

Listing 9.8 Typical Server Headers Sent from a PHP Script


1: HTTP/1.1 200 OK
2: Date: Mon, 11 Aug 2003 12:32:28 GMT
3: Server: Apache/2.0.44 (Unix) mod_ssl/2.0.44 OpenSSL/0.9.7a PHP/4.3.2
4: X-Powered-By: PHP/4.3.2
5: Connection: close
6: Content-Type: text/html

By sending a Location header instead of PHP's default header, you can cause the browser to be redirected to a new page, such as



header("Location: http://www.samspublishing.com");

Assuming that we've created a suitably upbeat page called congratsl, we can amend our number-guessing script to redirect the user if she guesses correctly, as shown in Listing 9.9.

Listing 9.9

Using header()

to Send Raw Headers



1: <?php
2: $num_to_guess = 42;
3: $num_tries = (isset($_POST[num_tries])) ? $num_tries + 1: 0;
4: $message = ";
5: if (!isset($_POST[guess])) {
6: $message = "Welcome to the guessing machine!";
7: } elseif ($_POST[guess] > $num_to_guess) {
8: $message = "$_POST[guess] is too big! Try a smaller number";
9: } elseif ($_POST[guess] < $num_to_guess) {
10: $message = "$_POST[guess] is too small! Try a larger number";
11: } else { // must be equivalent
12: header("Location: congratsl");
13: exit;
14: }
15: $guess = $_POST[guess];
16: ?>
17: <html>
18: <head>
19: <title>Listing 9.9 Using header() to Send Raw Headers</title>
20: </head>
21: <body>
22: <h1>
23: <?php echo $message ?>
24: </h1>
25: <p><strong>Guess number:</strong> <?php echo $num_tries ?></p>
26: <form action="<?php echo $_SERVER[PHP_SELF] ?>" method="POST">
27: <p><strong>Type your guess here:</strong>
28: <input type="text" name="guess" value="<?php echo $guess ?>">
29: <input type="hidden" name="num_tries" value="<?php echo $num_tries ?>">
30: <p><input type="submit" value="submit your guess"></p>
31: </form>
32: </body>
33: </html>

The else clause of our if statement on line 11 now causes the browser to request congratsl. We ensure that all output from the current page is aborted with the exit statement on line 13, which immediately ends execution and output, whether HTML or PHP.


/ 323