Currently developing a website and am wondering how to include pages into the main index (to only change a certain areas content and not the whole page) using the $_Get function.
Files would be something like index.php (where content pages are included), news.php (default page display on index), members.php, about.php, guides.php, downloads.php, sponsors.php etc.
I believe (if i remember correctly) linking with the $_Get method usually looks something like this index.php?pid=about
I would also like to find out how I can change the link (via htaccess?) to link as follows: www.mywebsite.com/about/ instead of mywebsite.com/index.php?pid=about
It has been some time since I have done any PHP and do not remember how the above things are done exactly.
Preview of what I am working on can be found at www.survivaloperations.net/dev/
You would put all of your files in a pages folder, just reugular .php files.
your index.php would look like this:
include('pages/'. $_GET['pid']. '.php');
And so your files would look like:
pages/home.php
pages/contact.php
and you would go to:
index.php?pid=home
index.php?pid=contact
respectively
You would also want to have control over which $_GET values are allowed. I reccomend using a php switch:
switch($_GET['pid']) {
case "home": include("home.php"); break;
case "contact": include("contact.php"); break;
case "thing": include("thing.php"); break;
default: include("404.php"); break;
}
The switch requires less code than a bunch of else if statements, and allows for a default "404"-type page to be displayed when an invalid pid is entered. It also secures users from accessing anything they shouldn't... like "../db.php" or something (not sure if parent directory access is possible, but still).
before use $_GET you must clear tags for safety.
$pid = strip_tags($_GET['pid']);
include("$path/" . $pid . ".php");
but... i prefer other code for safety.
$pid = strip_tags($_GET['pid']);
switch($pid) {
'sub01' : include("$path/01.php"); break;
'sub02' : include("$path/02.php"); break;
default : include("$path/main.php");
}