Web Hosting Talk







View Full Version : index.php?data


MDColson
11-08-2004, 11:54 PM
My php files are currently set up like this...

include 'includes/header.inc'

page specific data

include 'includes/footer.inc'


Is it possible that I set up my 1 index file to contain all the header and footer informaion, then pass info like aboutme, server.. in the form of index.php?about or index.php?server whereby it would just substitute the "page specific data" by calling the correct page specific file... like if it called index.php?aboutme it would just pull the data from aboutme.inc and put it in the page? I'm sorry if my question is confusing...

Check out www.mdcolson.com and you will see what I mean that all of my pages contain the same header/footer info...

I'm just wondering if I can only have 1 file in my html root, and then just parse in the page specific data as needed...

Mark

edit: or is this method only applicable where it pulls the info from a mysql table?

Pheaton
11-09-2004, 12:15 AM
sure you could. You could set it up like this:


include 'includes/header.inc';

include 'includes/'.$_GET['page'].'inc';

include 'includes/footer.inc';


If you do that, make sure you dont just do include $_GET['page']; because that poses a HUGE security risk.

If you used the above code, you would have to call your pages with something like index.php?page=aboutme.

If you wanted to do it like index.php?aboutme or just ?aboutme, it gets a bit complicated.


$argv[0] = $_SERVER["QUERY_STRING"]; // Certain versions of php don't recognise $argv

if (strpos($argv[0],"&")) {
$argv[1]=substr($argv[0],strpos($argv[0],"&")+1);
$argv[0]=substr($argv[0],0,strpos($argv[0],"&"));
}


You would then have all your data in the argv array. You could use it like this: include 'includes/'.$argv[0].'.inc';


Make any sense?

TehBooster
11-09-2004, 01:38 AM
<?php
$valid_pages = array('home', 'aboutme', 'contact');

$page = isset( $_GET['page'] ) ? trim($_GET['page']) : 'home';

if( in_array( $page, $valid_pages ) )
{
include('includes/' . $page . '.php');
}
else
{
die('The page you requested is not valid');
}
?> Is what I was doing for awhile on a simple website that didn't need much. Like Pheaton said doing index.php?aboutme is trickier. It can be accomplished like he did, or through mod_rewrite.

Also you could use mod_rewrite to turn index.php?page=aboutme into myurl.com/aboutme/ or myurl.com/aboutme.html for example.

MDColson
11-09-2004, 02:06 PM
Thanks for all of your help! If you wouldn't mind giving it a test now - I'd appreciate it! I incorporated both of your suggestions .

Thanks again!

-Mark
http://www.mdcolson.com

Now I REALLY need to update some content!