![]() |
|
Entire pages cached
The goal is to sevre PHP or HTML pages, if they've already been computed, without any access to the database, and with as little use of the PHP interpreter as possible.
This means that a request for http://www.dacode.org/2000/12/23/32,0,0,0.php3 shall bring this page if it exists, and http://www.dacode.org/gen.php3/2000/12/23/32,0,0,0.php3 otherwise. It shall also generate the page above for subsequents calls (what if after the coma is explained below).
First we will do this by configuring the Apache web server. Then we wil see some other solutions if we can't change the configuration of the web server.
With mod_rewrite
The generated files are put in a subdirectory of htdocs in order to avoid mixing them with the sources of daCode. It is the cache/ subdirectory by default, but this can be changed in the configuration file (configuration parameter htmldir).
So, the document is at the location: http://www.dacode.org/cache/2000/12/23/32,0,0,0.php3
In order to get it also at the location http://www.dacode.org/2000/12/23/32,0,0,0.php3 using mod_rewrite is the most logical solution.
Here are the rules on demo.dacode.org
RewriteEngine on RewriteCond /var/dacode.org/htdocs/cache%{REQUEST_URI} -f RewriteRule ^(.*)$ /var/dacode.org/htdocs/cache$1 [L] RewriteCond %{REQUEST_URI} ^/[0-9] [OR] RewriteCond %{REQUEST_URI} ^/index, [OR] RewriteCond %{REQUEST_URI} ^/section/ [OR] RewriteCond %{REQUEST_URI} ^/topic/ RewriteRule ^(.*)$ /var/dacode.org/htdocs/gen.php3$1 [L] |
If the user is authenticated, the output depends on his/her parameters. But we still can avoid much procesing. We use the same principle as above, but when writiing in the cache, some parts such as the autentication box are replaced by a PHP call. This way, the generated page needs very few processing by PHP.
Without mod_rewrite
No choice, we have to let the script appear in the adress. So we necessarily have $this->visiblenewsfile=1 in the config file. We will request the file http://www.dacode.org/gen.php3/2000/12/23/32,0,0,0.php3 which will seek the page /var/dacode.org/htdocs/cache/2000/12/23/32,0,0,0.php3 if it exists.
This way, te cache directory is not necessarily under htdocs/, as we do not request the pages. This case has been left out for coherence's sake.
You need, to do this, to rename gen.php3 as forum.php3, to change the definition of $this->newsfile in phplib/config.php3, and to tell the server a request on /forum/ shall be directed to /forum.php3. There are many ways to do that with Apache, for exemple with an Alias directive, or a MultiViews option in a .htaccess file.
dacode configuration
If you wish to use the cache, you need to set the parameter $this->htmdir in phplib/config.php3 to the desired value. This is a path relative to htdocs, and it must end by a trailing /. So, $this->htmldir = '/'; will put them in htdocs/cache. Please avoid the first solution, which may cause problems.
If this parameter is left empty, the cache is turned off.
Next, you have to give daCode the permission to create temporary files in this directory. To this end, the easiest way is to create the directory htdocs/cache, and to change its owner to the user under which the web server runs. This may be www-data, apache, nobody or something else, depending on the OS. If you do not have root permissions, you still can create the directory with the correct user by giving write permission to everybody on htdocs, then creating the cache directory with PHP. Then don't forget to reset permissions on htdocs!
The configuration parameter $this->visiblenewsfile controls wether the script name is visible in the URL. If set to 1, gen.php3 will appear in the URL. :>
User parameters
In any case, several parameters are passed in the URL to take users' preferences into account. Right now they are 3:
-
hide_sig: 1 if the user refuses to see signatures, 0 otherwise
-
score: minimal score for comments to be displayed.
-
theme: theme's number
For instance, http://www.dacode.org/2000/12/23/32,1,3,1.php3 means that the user choose the slashdot theme, he does not want to see the signatures and he only wants to see messages with a score higher than 3. With those parameters, all you have to do is bookmark the page http://www.dacode.org/index,1,3,1.php3