0

The code below work like this:

http://www.website.com/?page=home will pull content from http://www.website.com/home.php

-- or --

http://www.website.com/?page=About will pull content from http://www.website.com/About.php

Code:

// Set the default name 
$action = 'home'; 

// Specify some disallowed paths 
$disallowed_paths = array('admin'); 

if (!empty($_GET['page'])) { 
    $tmp_action = basename($_GET['page']); 
    // If it's not a disallowed path, and if the file exists, update $action 
    if (!in_array($tmp_action, $disallowed_paths) && file_exists("{$tmp_action}.php")) 
        $action = $tmp_action; 
} 
// Include $action 
include("$action.php"); 
?>

The code above works fine for all my pages but now I have one custom link which I need to add http://www.website.com/?page=Search to pull the content from http://www.website.com/search/search.php instead of http://www.website.com/Search.php

How can I do that?

Thanks

nickhar
  • 19,981
  • 12
  • 60
  • 73
Jeremy John
  • 1,665
  • 3
  • 18
  • 31

1 Answers1

3

There are 2 options, you could always look for the file in a sub folder, or you could have a special list of pages with their own paths much like you do with disallowed_paths.

$special_paths = array(
    "search" => "search/search.php",
);

if(in_array($_GET['page'], $special_paths)) {
    $action = $special_paths[$_GET['page']];

This would mean if in future another special page exists with a different path you can simply add it to the array.

Full code would then be:

<?php
    // Set the default name
    $action = 'home.php';

    // Specify some disallowed paths
    $disallowed_paths = array('admin');

    // special paths
    $special_paths = array(
            "search" => "search/search.php",
            "special_b" => "a/different/lengthier/path/file.php",
    );

    if (!empty($_GET['page'])) {
            $tmp_action = basename($_GET['page']);

            // If it's not a disallowed path, and if the file exists, update $action
            if (!in_array($tmp_action, $disallowed_paths) && file_exists("{$tmp_action}.php")) {
                    $action = $tmp_action.".php";
            } elseif(isset($special_paths[$tmp_action])) {
                    $action = $special_paths[$tmp_action];
            }
    }

    // Include $action
    include("$action");
sam
  • 5,459
  • 6
  • 32
  • 53
  • Also it should be noted you should consider forcing all your strings to lowercases internally with your files all being lowercase. Also I wouldn't write the code this way, but I wanted to keep it as similar to the questions code as possible – sam Nov 03 '12 at 15:07
  • OK it works thanks! but just need to add .php in `$action = 'home';` to be `$action = 'home.php';` – Jeremy John Nov 03 '12 at 15:12
  • Depending on the size of your project, you would probably want to extend this solution to make it more flexible by storing this data in a database. Then you could cache this data on the web server to avoid querying the database for each page load. But yeah, hard coding the pages into an array is certainly also a way to go to keep things simple. – ba0708 Nov 03 '12 at 15:15