PHP
Another technique for redirecting people to new pages is PHP, a scripting
language that can be embedded in your HTML documents. This technique
is normally used to redirect people to the new destination based on a
variable value, for instance the browser used by the viewer or any other
PHP variable.
Several important things to note about PHP scripts:
- PHP is case sensitive, print is different from PRINT.
- PHP statements appear between <? ?>.
- PHP statements end with a semi-colon (;).
- You can include comments in your PHP code by beginning the line or
lines with // characters.
- Your PHP files should have a .php extension rather than a
.html extension. If scripts have a .html extension the server will
not know that they are PHP files and will not parse and execute the
PHP commands.
Syntax is:
<?
header("Location: location");
exit;
?>
where
| header() |
is a function is used to send raw HTTP headers. |
| Location: |
is a special-case header call that sends this header back to the
browser
and also returns a REDIRECT (302) status code to the browser.
|
| location |
is the URL that should be loaded. |
| exit |
makes sure that code below does not get executed
when redirecting. |
For example,
<?
header("Location: http://www.utexas.edu/its/");
exit;
?>
tells the browser to load http://www.utexas.edu/its/ and stop reading
this string of code.
Note: remember to write this code before any output operation.
Otherwise, your header will be generated by the Web server and this
line would be interpreted as text.
Advantages
- Control over the redirection without contacting the system administrator.
- Correct URL address appears in the browser's location box.
- New location can be on a different server.
Disadvantages
- User is not informed about the site change.
- On ITS Web servers, files to be read from or written to must have
the same ownership of the PHP script being executed, or the directory
in which the script resides must be owned by the owner of the PHP script.
- Also, on ITS Web servers, PHP scripts cannot execute external system
programs.
More Information
For more information on PHP, see Dynamic Web Sites with
PHP.
|