PDA

View Full Version : Dynamic Linking with PHP


plutonium83
01-08-2005, 11:19 PM
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.

<?php
switch ($page) {
case "news":
include('news.html');
break;
case "about":
include('about.html');
break;
case "contact":
include('contact.html');
break;
default:
include('start.html');
}
?>

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

Enjoy!

Greg Marra
01-08-2005, 11:36 PM
For for information visit: http://www.dotdragnet.com/content.php?aid=18

Enjoy!

Look at the dynamic linking even there!

hehe, just felt I needed to point that out.

colt527
01-09-2005, 12:17 AM
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.

plutonium83
01-09-2005, 08:40 AM
Cool, I never thought of that.

Does anyone have experience with mod_rewrite?

Jack
01-09-2005, 09:21 PM
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.

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:

``The great thing about mod_rewrite is it gives you all the configurability and flexibility of Sendmail. The downside to mod_rewrite is that it gives you all the configurability and flexibility of Sendmail.''
-- Brian Behlendorf
Apache Group
`` Despite the tons of examples and docs, mod_rewrite is voodoo. Damned cool voodoo, but still voodoo. ''
-- Brian Moore
bem@news.cmc.net

http://httpd.apache.org/docs/mod/mod_rewrite.html

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

Good luck!

Jack

dragonpaulz
01-09-2005, 09:28 PM
<?php
if (empty($_GET['id'])) header("Location: index.php?id=Main");
?>
<!-- Content -->
<?php
$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:
something.php?id=filenamewithout.php

HFWang
01-12-2005, 06:41 PM
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:

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


My htaccess looks like:

php_value auto_prepend_file header.php
php_value auto_append_file footer.php

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


(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.