- Home
- Learn Linux
- Learn Electronics
- Raspberry Pi
- Programming
- Projects
- LPI certification
- News & Reviews
I've been redesigning my watkissonline web site. You should not notice many differences yet, as it's really just preparation for updating it in future. Although I'd simplified the menu system to make it easier to maintain the menu was still being manually updated, so any changes had to be made across all files in the entire site. Excluding the pages that are dynamically generated (i.e. the blog) that is over 30 pages that need updating.
I've decided to convert the pages from static html pages, to php code which dynamically creates part of the config using php include files.
The pages have now all changed suffix from .html to .php. Although it's possible to set the web site to server pages with .html through the php parser, I prefer to have php scripts identifiable as such. I have however used Apache mod_rewrite Rewrite directives to map all the old urls to the new urls, so users shouldn't notice any (except that the url in the browsers location will change).
The mod_rewrite rules include some static rules for the base files such as:
RewriteRule ^/ebooks.html$ /ebooks.php [R,L]
and also some generic RewriteRules such as:
RewriteRule ^/info/(.*)\.html$ /info/$1.php [R,L]
The first entry only maps a single entry. I didn't use a generic rule for the main directory as I also had some older .html entries that I wanted to remap differently. It was a fairly small number so it was easier just to use individual entries.
The second entry maps all .html files in the info directory to .php files. The page name is matched by the (.*) part, which is substituted into the new name using $1.
One of the things I've done is to separate the menu into a separate php file. For example if the file is called menu.php then it can be included into a php file using :
include("menu.cfg");
As long as the file is in the include_path it will then be inserted into the file and any dynamic content generated. The problem is that the file wasn't in the include_path. In my case I have one include file that then inserts another include file. E.g. menu.cfg will then include another include file based on a variable in the actual page. If the file is not in the include_path then you can normally call the file by using a path relative to your current position. For example:
include("menu/menu.php");
So if the page was index.php and it called /menu/menu.php that would work.
To complicate things I don't just have pages served from my DocumentRoot directory, but also from a /info directory.
The correct way to add a directory is to append it to the include_path directory using the ini_set command. The only problem with that is to include an absolute directory you would end up hard coding the entire DocumentRoot path part e.g. /var/www which may change if the server is reconfigured, or the code is reused.
I therefore added the following script which adds the appropriate directory using the DOCUMENT_ROOT server variable.
ini_set("include_path", ini_get("include_path").";".$_SERVER['DOCUMENT_ROOT']."/menu");
This command also uses the ini_get command, so that it will append to, rather than overwrite, any existing directories.
Finally, I had another problem in that I also needed to check to see if the file existed before trying to include it. Again I used the document_root variable to work out the actual directory for the file before checking if it existed.
if (file_exists($_SERVER['DOCUMENT_ROOT']."/menu/$thisconfig.cfg")) {include "$thisconfig.cfg";}
This will only work if the DocumentRoot/menu directory has already been added to the include_path.
The site is pretty much an exact copy of what it was before, but generating the menu dynamically. I'll be using this to make it easier to update the content in the future.