Hi,
My website has a bunch of pages where it looks VERY similar save for some text.
What I want to do is build my site around index.php
So a file named "lol" would show up as
index.php?p=lol
How would I do this?
Thanks.
Hi,
My website has a bunch of pages where it looks VERY similar save for some text.
What I want to do is build my site around index.php
So a file named "lol" would show up as
index.php?p=lol
How would I do this?
Thanks.
$p = $_GET['p'];
echo($p);
Would display "lol" in this case.
If you want more the one value use index.php?p=lol&q=lawl
Um, I kinda meant that a file named "lol.php" would be included into the page.
$page = null;
if (isset ($_GET['p']) ){
$page = $_GET['p'];
//make sure he is not trying to hack your page
if (!preg_match('@[a-z0-9][a-z0-9\.]*@i',$page) ) $page = null;
}
if (isset ($page) ) include $page.'.php';
else {
//code for when the "p" paramater is not found or invalid
}
Another thing you could do to have one page is this.
<?php
$page = $_GET['page'];
$myPages = array("page1", "page2", "page3"); // list of pages.
if(!$page) {
echo "You're on the home page!!!";
}else {
if(in_array($page, $myPages)) {
include $page . ".php";
}else {
echo "The page your looking for cannot be found!";
}
}
echo "<br>";
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "\">Home</a> ";
foreach($myPages AS $allPages) {
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?page=" . $allPages . "\">" . $allPages . "</a> ";
}
?>
for more information on in_array click here
ScoobyDooobyD00's example is that it lacks correct checking for the existence of $_GET['page'] before its use. This is well hanlded in Jack McSlay's example, but that example does not account for an invalid/missing include file (i.e., the filename passes the validity check, but the file does not exist).
Jack McSlay's example can be fixed with [man]file_exists/man, possibly including a check that the file to be included is not index.php, and ScoobyDooobyD00's example can be fixed by properly using [man]isset[/man]. I suspect ScoobyDooobyD00's example is better since it can be more directly transformed to use a database later on. On the other hand, Jack McSlay's example could be a more long term solution, but at the risk of the user being able to arbitrarily include any file in the current directory.