include php file

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!

Moderator: General Moderators

Post Reply
dotphp
Forum Commoner
Posts: 25
Joined: Sat Nov 10, 2007 8:03 am

include php file

Post by dotphp »

I want to include php files depends on the $_GET variable.
Let's suposed that I have an index.php with a menu with:
Add -> link (index.php?page=add)
Edit -> link (index.php?page=edit)
etc....

On server I have add.php and edit.php.
The code in index.php is:

Code: Select all

<?php
if($_POST['ok']){
     require($_GET['page'] . '.php');
} else {
//code from the index.php page
}
?>
It's ok to include like that ? From security point of view.
If you have other ideas please share.
Thanks
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: include php file

Post by Jade »

Yes, as long as you use a hash function to validate they've used a file that you allow. For instance:

Code: Select all

<?php
if($_POST['ok']){
     require(includeHash($_GET['page']));
} else {
//code from the index.php page
}

function includeHash($file)
{
   switch($file)
   {
      case "index": return "index.php";
      case "help": return "help.php";
      case "search": return "search.php";
      default: return "index.php";
   }
}
?>
 
Post Reply