Dynamic Linking with PHP

Dynamic linking and page generation is a very useful tool for your website. It makes every site follow a specific template for site consistency so its easy to change designs. Here is an example of one done in PHP.

switch ($page) { 
    case "news": 
    case "about": 
    case "contact": 

An example link to the “news” page would be http://domain.com/index.php?page=news. Going to this site would add news.html to the rest of the html.

Knowing this, you can go crazy and make some page specific commands!

Note: Google does not like pages that use this script. However, you can use the apache mod_rewrite module to change the url to be more cache friendly. I’m not quite sure how to do this.

For for information visit: [http://www.dotdragnet.com/content.php?aid=18](http://www.dotdragnet.com/content.php?aid=18)


Look at the dynamic linking even there!

hehe, just felt I needed to point that out.

For more complicated sites, I like to keep pages separated based on functionality. For example, I have editnews.php, thise file only deals with editing the news that goes on the site. If I want to add i do editnews.php?do=add … this way the program doesnt get hugely out of hand and its still relatily easy to find your way around the backend of your site.

Cool, I never thought of that.

Does anyone have experience with mod_rewrite?

um… I might be wrong in a few cases, but in general, google and other search engines have no problem with that type of url. (The part after the ? is called the “query” as i recall). The exception to this is when you add a session id to the end of the url. This is normally a bunch of random letters/numbers (exe: ?sid=k01f4e24as2d) and would be used to track a user across a website when cookies weren’t avialable. Becuase this session id would change on every page load, this would generate an infinate series of loops for a search engine, and therefore they won’t spider the page.

About mod_rewrite… my favorite part of the apache manual is this:


Sorry, mod_rewrite is crazy for me too… takes me forever to accomplish anything whenever i try to use it.

Good luck!


if (empty($_GET'id'])) header("Location: index.php?id=Main");
<!-- Content -->
$fn = "Includes/" . $_GET'id'] . ".php"; if (file_exists($fn)) {include($fn);} else {include("Includes/404.php");}
<!-- Put the above where u want pages to load --!>

Then it’s like:

Warning! Above code is insecure!

Lets attacker execute arbitrary code available on the server. (For example, what happens when someone uses id=…/…/…/other_user/comproming_script ? I don’t know either. That should make you nervous. Another interesting idea would be calling id=…/something.php. Watch as PHP enters a recursive loop including the same file over, and over and over until something dies.)

Always, always, always, and oh yah, always, check data. BTW, you don’t need use the Location:index.php?id=Main bit either. Just…

if (empty($_GET'id']))
    $_GET'id'] = 'Main';

or… if you feel really frisky…

$_GET'id'] = empty($_GET'id']) ? 'Main' : $_GET'id'];

Another nitpick is that this isn’t dynamic linking. Its basically just including. At which point you’re better off just using .htaccess (or appropriate platform-specific replacement) and setting auto_prepend_file/auto_append_file php settings. You’re organizing the files along the way the fileystem is internally, so you may as well just stick the header/footer on around the actual file.

(IE: why put all your content in include/news.php and then load it when you receive requests for http://foo.com/news.php when you can just go to http://foo.com/news.php?)

I have been doing this for awhile, and really like the system. Soo… some “sample code”. I store all my content as xml (because I can I guess.) A sample content file looks like:

    <title>Page Title</title>
    <content>This is the page content</content>

My htaccess looks like:

php_value auto_prepend_file header.php
php_value auto_append_file footer.php

<FilesMatch "^^\.]+$">
	SetHandler application/x-httpd-php

(For those with weak regex foo, it basically just has all files without extensions execute as PHP. I just think its cool to hide extensions. I’m weird like that)

The header file basically just includes library classes and starts output buffering. PHP then dumps the file to the output buffer (trivia: ob_start(), echoing stuff, then ob_get_contents() and ob_end_clean() is the fastest way to concat string in PHP. Faster than an array and implode(), faster than ‘something’.‘something’. Its magic. :D), and I pickup the output in footer.php, and start processing. (IE: replace templating code with the actual HTML I want. Executing behaviors like posting comments, etc).

Now that I’ve rambled this long, I’ll go away.