Приглашаем посетить
Добычин (dobychin.lit-info.ru)

Redirecting the User

Previous
Table of Contents
Next

Redirecting the User

Sometimes a user will request a pageeither directly by clicking on a link, or indirectly by submitting a formand we will want to send him to a different page before or after some processing. If the user fills in a form to create a new account and the form action attribute sends him to process_new_user.php, that script, after processing the form, might want to send him to welcome_new_user.php, as shown in Figure 7-6.

Figure 7-6. Page sequence when creating a new account.

Redirecting the User


If we want to do some processing and then send the user to a new page from within PHP script, we use a new function called header. This allows us to manipulate what HTTP headers are sent back to the client as we process a page. This function can be used to generate any HTTP headers, but we will use the Location: header for now. For example, our process_new_user.php might look similar to the following:

<?php

  // create new user account from $_POST information.
  // etc ...
  //
  $processing_error = create_new_user_account(
         $_POST['username'], $_POST['fullname'],
         $_POST['password']);

  //
  // now go and redirect to welcome page if no error.
  //
  if ($processing_error === FALSE)
  {
    header('Location: http://' . $_SERVER['HTTP_HOST']
                               . dirname($_SERVER['PHP_SELF'])
                               . '/welcome_new_user.php');
  }
  else
  {
    // send them back to the form entry page.
    header('Location: http://' . $_SERVER['HTTP_HOST']
                               . dirname($_SERVER['PHP_SELF'])
                               . '/create_account.php?err='
                               . $processing_error);
  }

?>

One critical thing to note with the header function is that there can be no text output before the header function. Any whitespace or other characters cause PHP to start sending an output stream and cause your header function call to generate an error. The following code

     <?php

  header('Location: http://' . $_SERVER['HTTP_HOST']
                             . '/welcome2.php');
?>

generates an error because of the spaces sent before the opening <?php tag. Any whitespace characters occurring outside of PHP section markers in any of the files included have the same problem!

Fortunately, we do not always have to be so cautious with our script files and whitespace. There is functionality in PHP called output buffering (see Chapter 21, "Advanced Output and Output Buffering," for more detail) that let us get around this in a more robust and usable fashion by delaying the sending of any output to the client until we explicitly say so.

Another option for redirecting users is via META headers sent to the client browser. Using this mechanism, you can tell browsers (that do not have redirecting turned off in some sort of user preference/option) to go to a new page after a number of seconds.

<?php

  echo <<EOH

<meta http-equiv='refresh' content='0;
        url="http://$_SERVER['HTTP_HOST']/welcome2.php"/>
EOH;

?>

Due to the fact that this can be turned off by users in some browsers and the less "automatic" feeling versus using the HTTP headers via the header function, we will mostly use the Location header when we wish to redirect in our PHP code.


Previous
Table of Contents
Next