how to get pages like index.php?page=apples explaind

Started by Leipe Po, March 15, 2006, 07:47:24 PM

Previous topic - Next topic

Leipe Po

well i hope perham doesn't mind this, usually he's the one making this stuff,
but after dealing and seeing several posts about how to get the index.php?page=blaat thingy,
i thought that a tut about it would be nice,
note that this is really simple code, so everybody should be able to follow it,

lets first start off with a chunk of code:

index.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>index</title>
</head>

<body>

<table width="100%" border="1">
<tr>
<td align="center">my logo here?</td>
</tr>
<tr>
<td>
<a href="index.php">Home</a>&nbsp;|&nsbp;
<a href="index.php?page=about">About us</a>&nbsp;|&nsbp;
<a href="index.php?page=contact">Contact</a>&nbsp;|&nsbp;
</td>
</tr>
</table>

<br>

<table width="100%" border="1">
<tr>
<td align="center" valign="top">
<?php
// lets check if the $_GET['page'] isnt set:
if(!isset($_GET['page'])){
// if not, lets load our index page(you can change home.php to whatever you want:
include("home.php");
// else $_GET['pahe'] was set so lets do stuff:
} else {
// lets first check if the file exists:
if(file_exists($_GET['page'].'.php')){
// and lets include that then:
include($_GET['page'].'.php');
// sorry mate, could not find it:
} else {
echo 'Sorry, could not find <strong>' $_GET['page'] .'.php</strong>';
}

}
?>

</td>
</tr>
</table>

</body>
</html>


this is really basic stuff, functions used are file_exists() to check if a file exists and then use the include() function to get the page, else we echo an error,

now as you see the links in the "nav bar" you can see that home is just plain index.php, thats because if $_GET['page'] is not set, we include home.php

home.php

Welcome to our site!<br>
as you can see the url to this page is just plain index.php,<br>
but i really am home.php!!!<br>


now as you see the other links:
      <a href="index.php?page=about">About us</a>
      <a href="index.php?page=contact">Contact</a>

they have the ?page=thingy thingy
this is one weakness of the script, that our filenames should correspond to the url without(!) extention,
so if you have index.php?page=allaboutmeandmylovelygirlwholikescatsanddogsandjumpingintherain
then you also must have a file with:
allaboutmeandmylovelygirlwholikescatsanddogsandjumpingintherain.php <- notice the extention

so as you know by now, you will have to make a about.php and a contact.php, but i leave that up to you
this should work like a charm, allthough i didn't tested it  :P
There is only one thing more importend to me then coding:
My Girlfriend

Microsoft - "You've got questions.  We've got dancing paperclips."

Advertisement: