phpphp-include

PHP Dynamic Include Help on Index.php?


I need someone to let me know a solution to this issue. I am trying to create an include on my index.php file so when a user clicks a link on my navbar the content on the index.php changes. The code below works great, except when I go to index.php, because it is not part of the array, it calls main.php twice, instead of once. I know this is because of the last portion that says:

    else {
    include('main.php');
    }

However, I need a solution to this because I am not good with php. Here is my full code for the include.

    <?php
    // Place the value from ?page=value in the URL to the variable $page.
    $page = $_GET['id'];
    // Create an array of the only pages allowed.
    $pageArray = array('index','css-pub1','page2','page3','page4','page5','page6');
    // If there is no page set, include the default main page.
    if (!$page) {
    include('main.php');
    }
    // Is $page in the array?
    $inArray = in_array($page, $pageArray);
    // If so, include it, if not, emit error.
    if ($inArray == true) {
    include(''. $page .'.php');
    } 
    else {
    include('main.php');
    }
    ?>

Solution

  • Try to use include_once instead of include:

    include_once($page . '.php');
    //...
    include_once('main.php');