...

View Full Version : Dynamic Pages



marc-e
12-11-2005, 05:56 PM
Hello

Can somebody point me in the direction of a good/simple Dynamic Page tutorial? I'm fed-up of having to edit each page individually every time I edit the index page design.

PS. I have tried Google "going on 3 hours now, but to no avail"

Thanks very much.

Element
12-11-2005, 07:25 PM
A good way to do it is put your content into individual php pages, and then dynamically include them. And also its good to add mod_rewrite but I don't have time for that, gotta get some foot. :)



<?php

// PHP Include
$page = $_GET['p'];
$i404 = "home/user/public_html/404.shtml";
$home = "home/user/public_html/index_content.php"; // File to your main page, like the news script or something.
if(file_exists($page.".php")) {
include $page.".php";
} elseif (empty($page)) {
include $home;
} else {
include $i404;
}

?>

And then you would go to lets say index.php?p=media to go to the multimedia section while still being in the same template, index.php.

Or use mod_rewrite to turn index.php?p=media into something like /site/media.php if you don't want people seeing the real URL.

Also I usually make an array of files I don't want people to go to, just encase.



// PHP Include
$page = $_GET['p'];
$i404 = "home/user/public_html/404.shtml";
$home = "home/user/public_html/index_content.php"; // File to your main page, like the news script or something.
$bad_files = array ('temp/private.db', 'Thumbs.db', 'functions.php'); // Etc.
if(file_exists($page.".php") && !(in_array($page, $bad_files))) {


EDIT: Thanks Marek_mar, assuming thats for me. Havn't had coffee, well actually I don't have any coffee.

marek_mar
12-11-2005, 07:38 PM
http://www.php.net/variables. The "basics" part.

Element
12-11-2005, 07:52 PM
Also, I forgot, because these are dynamic pages, you might also want to pass the page title, to have a dynamic page title. There are a few ways to do this, I would say.

First, if you filenames only are one word, you can echo the filename and capitalize it with ucwords(), or use a $t query for the title. Example




<title>Example.com <?php $t=$_GET['t']; $t==""?$t="- Home":$t="- ".ucwords($t); echo $t; ?></title>



Example URL example.com/index.php?p=media&t=multimedia+section

So because $t isn't empty it will echo "Example.com - Multimedia Section" in the <title></title> tag. If its empty it would assume your on http://example.com/index.php and output "Exampe.com - Home"

Velox Letum
12-11-2005, 08:02 PM
Also, run str_replace on the value to ensure it isn't exploited to view other files.


<?php

// PHP Include
$page = $_GET['p'];
$page = str_replace(array('..','/','\\','.'), '', $page); // Replace any operators that would allow other-directory transversal.
$i404 = "home/user/public_html/404.shtml";
$home = "home/user/public_html/index_content.php"; // File to your main page, like the news script or something.
if(file_exists($page.".php")) {
include $page.".php";
} elseif (empty($page)) {
include $home;
} else {
include $i404;
}

?>

Element
12-11-2005, 08:24 PM
Also, run str_replace on the value to ensure it isn't exploited to view other files.


<?php

// PHP Include
$page = $_GET['p'];
$page = str_replace(array('..','/','\\','.'), '', $page); // Replace any operators that would allow other-directory transversal.
$i404 = "home/user/public_html/404.shtml";
$home = "home/user/public_html/index_content.php"; // File to your main page, like the news script or something.
if(file_exists($page.".php")) {
include $page.".php";
} elseif (empty($page)) {
include $home;
} else {
include $i404;
}

?>

Good thinking. I'm still trying to find my include script. It has all that and more, about 46 lines. It was on my old site but I can't find its back up unfortunately. Still looking though, thats why I'm mainly silent.

Well since I can't find the file I'll show you the mod_rewrite rule to hite the ugly php queries.



RewriteEngine On

# Lets start with root files #
RewriteRule ^index.php?p=(.*)&t=(.*) /site/$2/$1.php
# Lets start with 1 sub dir #
RewriteRule ^index.php?p=(.*)/(.*)&t=(.*) /site/$1/$3/$2.php
# Continue if need be with mod_rewrite #


So th first rule changes something like:

example.com/index.php?p=media&t=multimedia+section
To:
example.com/site/multimedia+section/media.php (Url you would link to)

And for the second one it would change something like:

example.com/index.php?p=reviews/coding&t=reviews+section:+coding
To:
exampe.com/site/reviews/reviews+section:+coding/coding.php

But that will only work if you allow sub directories in the code Velox added

marek_mar
12-11-2005, 08:47 PM
Yes Elemnt, it was for you :p . BTW there are ways of having variables with a number at the beginning (useless but possible). You just can't call them directly.

Element
12-11-2005, 09:00 PM
Yes Elemnt, it was for you :p . BTW there are ways of having variables with a number at the beginning (useless but possible). You just can't call them directly.

Interesting. Using eval() or something? Hum. PM me a example. ;)

Also that mod_rewrite code should be placed in the .htaccess file of your root where index.php is.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum