0
<?php
    if ($page == null || $page == "home")
        require_once ("home.php");
    else if ($page == "bill")
        require_once ("bill.php");
    else if ($page == "product")
        require_once ("product.php");
    else if ($page == "addproduct")
        require_once ("addproduct.php");
    else if ($page == "editpd")
        require_once ("editpd.php");

    /** ... MANY LINES ... **/

    else if ($page == "permission")
        require_once ("permission.php");
    else if ($page == "options")
        require_once ("option.php");
    else // NOT FOUND, REDIRECT HOME
        echo "SOMETHING WENT WRONG";
?>

The above code is loads of IF ELSE and REQUIRE_ONCE.

I have started worrying about the execution time when the system is developed, it means that i have to write IF ELSE and REQUIRE_ONCE more and more.

So i really want to know,

What is a solution or the better way for this in case not use any framework?

InOrderToLive
  • 186
  • 1
  • 1
  • 9

4 Answers4

4

It looks like all the include files have the same name as $page, so you can use string processing to remove all the duplicate code.

$pages = array('home', 'bill', 'product', ...);
$page = $page == null ? 'home' : $page;
if (in_array($page, $pages)) {
    require_once ($page . ".php");
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • The "in_array()" is very important in this solution, as it limits the allowed pages to load. Otherwise you'll get a security flaw, where a user can require_once any php on your server. – DThought Dec 03 '14 at 07:58
1
$page = preg_replace('/\W+/', '', $page); 
if (is_file($page . '.php')) {
    require_once $page . '.php';
}

or with whitelist

$whitelist = ['permission', '....'];
if (in_array($page, $whitelist)) {
    require_once $page . '.php';
}

or for more performance

$whitelist = ['permission' => true, '...' => true];
if (isset($whitelist[$page])) {
    require_once $page . '.php';
}
Michael Freund
  • 234
  • 2
  • 12
0

You could use a switch() statement, like so:

<?php
    switch($page) {
        case "home":
        case null:
            require_once ("home.php");
            break;
        case "bill":
            require_once ("bill.php");
            break;
        case "product":
            require_once ("product.php");
            break;
        case "addproduct":
            require_once ("addproduct.php");
            break;
        case "editpd":
            require_once ("editpd.php");
            break;

    /** ... MANY LINES ... **/


        case "permission":
            require_once ("permission.php");
            break;
        case "options":
            require_once ("option.php");
            break;
        default:
            echo "SOMETHING WENT WRONG";
            break:
    }
?>

If however the structure stays the same, maybe this would be a better option:

if(is_null($page)) {
    $page = 'home';
}
if(file_exists($page . '.php')) {
    require_once($page . '.php');
} else {
    echo "SOMETHING WENT WRONG";
}
RichardBernards
  • 3,146
  • 1
  • 22
  • 30
  • 1
    never ever do this second version please..... require_once understands ../ pathes and you could end $page with null-bytes to include whatever you want – Michael Freund Dec 03 '14 at 07:53
  • @MichaelFreund Please interpret the full code instead of one line of it... This can never happen since the `file_exists()` function would not find `../.php` a valid filename... – RichardBernards Dec 03 '14 at 07:55
  • `file_exists('../../../badfile' . '.php')` is absolutely valid, if it exists.... its a bad file_inclusion leak, http://hakipedia.com/index.php/File_Inclusion#wrong_defense_3:_adding_content_to_filename – Michael Freund Dec 03 '14 at 07:58
  • @MichaelFreund If you have security concerns, please follow the following procedure; 1. lookup function reference on PHP and read the big red outlined part (http://php.net/file_exists). 2. who said `$page` is something a user can alter? It is not specified a whitelist is needed, and this cannot be deduced from the question asked – RichardBernards Dec 03 '14 at 08:03
0
if($page == NULL) {
    include 'home.php';
} else {
    include "$page.php";
}
Jazi
  • 6,569
  • 13
  • 60
  • 92