PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
// index.php.
if (isset($_GET['id'])) {
switch($_GET['id']) {
case 0:
echo 'Welcome to my homepage!';
break;
case 1:
echo 'My name is Foo';
break;
default:
echo 'Nope, no such page here...';
}
}
?>
<br />
<a href="index.php?id=0">Homepage</a> | <a href="index.php?id=1">I am...</a>
Instead of having 'Welcome to my homepage!' for the echo 0, is their a way to just include it like having
Though i wouldn't use a switch() like that as it can become cumbersome with a large amount of possible pages that can be included... case 44: etc..etc.
Would you be able to do a different format then? I assume you know what concept I would wan't to do. So yea.. Is their any other way? I remember there is, much simpler. No need for the other codes.
<?php
//array of allowed pages we can request
$allowed = array(
'home', 'login', 'logout', 'info'
);
if(empty($_GET['page'])){
$_GET['page'] = 'home'; //set the default page to view if non passed in the url
}
if(file_exists($_GET['page'].'.php') && in_array($_GET['page'], $allowed)){ //check the file exists and it is allowed
require_once $_GET['page'].'.php';
} else {
echo 'Invalid page request.'; //otherwise a bad page was requested
}
?>
This way you only have to add new pages to the $allowed array and the code should take care of itself.
Last edited by markl999 on Fri Jan 30, 2004 12:52 pm, edited 2 times in total.
<?php
//array of allowed pages we can request
$allowed = array(
'home', 'login', 'logout', 'info'
);
if(empty($_GET['page'])){
$_GET['page'] = 'home'; //set the default page to view if non passed in the url
}
if(file_exists($_GET['page'].'.php') && in_array($_GET['page'], $allowed)){ //check the file exists and it is allowed
require_once $_GET['page'].'.php';
} else {
echo 'Invalid page request.'; //otherwise something a bad page was requested
}
?>
What he said is you only have to change 1 line to add/delete pages which is