tags:

views:

285

answers:

4

For a school project I have to produce a website using PHP that allows user to generate there own article or comment on news or others articles. I was wondering how it is best to use the GET function to show the content in an include file and also use the get fuction for other include files such as the login page and other content the developer (me) has added to website not as articles but as links etc. Anyone got any suggestions or good tutorials they could post up.

Cheers

CHL

+1  A: 

Wheel has been invented, tested, rolled for a million miles: WordPress

Rex M
We were all thinking it...
cole
So I'm the only one with the balls to point out that it's ridiculous to burn time on problems that have already been solved
Rex M
+1 .. but sometimes it can be 'fun' or at least a good learning experience. Though if it were for a client I was getting paid to do, I'd leave it to wordpress.
alex
@alex agreed. Asker starts question with "I have to produce". Secret sauce of success: use someone else's work for the foundation and spend your time on the unique puzzles. then knock off early and get a beer.
Rex M
@rex agree on the beer!
alex
Its all well and good saying that isn't but if you have to produce one for a project (academic work) you need to produce the hard version not use a pre-built application. I wouldn't be asking otherwise!
Cool Hand Luke UK
@Cool i updated your question appropriately.
Rex M
-1. He asks for help for a school project where he should use php. Pointing at wordpress doesn't really tell him how to use GET and php.
Jonas
+1  A: 

You'll definitely want to sanitize anything that comes in through the GET parameter before including a file. You normally do this by checking for valid characters, and since it is directly calling a file from the file system I usually manually enter the valid navigation actions. This isn't the most elegant solution but often the easiest and safest for a small application.

I usually use case switches for this, but I've seen people use fancy regular expressions as well.

Something along the lines of:

if (isset($_GET['nav'])) {
    switch ($_GET['nav']) {
        case 'login':
        case 'logout':
        case 'article':
             include($_GET['nav'] . '.php');
             break;
        default:
             die('Invalid nav parameter');
             break;
    }
}
Andy Baird
+1  A: 

This is what I use...

   function safeIncludeFile($path) 
   {

        $regex = '/[a-z0-9\-_]+/i'; // match only a to z, 0 to 9 and the minus and unscore character. case insensitive. adjust to accommodate your file naming schema.

        return preg_match($regex, $path); 

   }

And then do something like this

if (isset($_GET['page']) && safeIncludeFile($_GET['page'])) {
   require PATH_TO_INCLUDES . $_GET['page'] . '.inc.php';

}
alex
A: 

You could also have an array that acts as a whitelist

$allowedIncludes = array('home', 'about', 'news');

and then check if the requested page is in there

if(in_array($_GET['page'], $allowedIncludes)){
  include $_GET['page'].'.php';
}else{
  die('Forbidden!');
}

thus, you don't have to add a "case" to your switch everytime you add something.

middus