![]()
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Goal of the scriptThe goal of this PHP script is to provide a fast way of displaying web pages of this site without frames and tables, so that they can be printed without any troubles. Indeed, pages with fixed-width tables cannot be easily printed. On a programming point of view, there are 2 main constraints we want to avoid: not to write pages twice (one version with frames and tables and another version without) and to be able to (statically) include a footer at the bottom of all pages (different from the one used on real pages). The called script must be able to transform each page so that it would comply with these constraints. There are at least two methods to realize this script. One of them uses an environment variable that Apache provides to PHP: $HTTP_REFERER, and the other method uses the environment variable $PHP_SELF. This site uses the second method. We will see later why. Both methods only differ from the way to calculate the path to the file to be printed out. With $HTTP_REFERERPrincipleIt is very simple: actually, some web servers provide environment variables to (CGI) scripts, and one of those variables contains the URL of the page that has called the current page. This variable is called $HTTP_REFERER. If you click on the printer icon at the bottom of this page, you Internet browser and then the web server will send
to the This variable is really useful, but unfortunately, there are 2 problems with it. First of all, the browser agent sets it when it queries the web server to retrieve the web page, but unfortunately, many browsers do not set this variable. The second problems is that this variable is provided by Apache Web servers, but there is no guarantee that other Web servers do it as well. Consequently, a lot of visitors will never be able to use correctly the "printable format" button, because of the Web agent or the Web server. This is why this web site implements the second solution that does presents these drawbacks. Implementing the scriptThere are 2 things to do: write the script
This piece of code should be inserted in every page we want to be printable. For the script, the very first thing to do is to check the content of
If everything is all right, we parse this variable to get all relevant data (in particular the name of the source page) with the function parse_url:
$url is then an associative table that contains in particular a field called path. This field will be used
to determine the file to open. This field may not contain any valid name (file with a .php3 extension). This happens when the script
has been called from a
With $PHP_SELFPrincipleIt is basicly the same: we are going to use another environment variable ($PHP_SELF) to
calculate the path to the file to be printed out, but this variable corresponds to the name of the script
that is being executed. This means that this variable has only the proper value inside the page we want
to print. Therefore, we will have to pass this variable to The variable $PHP_SELF is very interesting because it always exists and is correctly set, because it is set by the PHP interpreter. Implementing the scriptIt is possible to pass variables to any script through URLs (GET method). For instance instance,
To write
Like the version with $HTTP_REFERER, it is possible no .php3 file is specified. In this case,
we must correct this by adding
From now on, and in these conditions, $nom is equivalent to $nom we got from the previous version of the script. What we still have to do is exactely the same. End of the scriptFrom $nom, it is easy to deduce the absolute local path to the document we want to print:
Now, we just have to parse the file and locate lines where we perform an "include" (those lines should be removed). One must take care of images, since we need to transform relative pathes into absolute pathes:
At the end of the script, we add a common footer. As this site is both in French and English, the footer must be changed according to the language. To do so, we simply check the name of the treated file to find a "uk_" in:
Useful functions
Download the code with $HTTP_REFERER Download the code with $PHP_SELF |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Copyright © 2000-2002 themanualpage.org - This site is submissive to the terms of the GNU GPL and FDL licences. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||