Parvinder Thakran
Parvinder Thakran

Reputation: 33

Dynamic navigation using PHP include

I have a question about how I can dynamically change the content to display in the webpages. I have few portions of the website fixed - header, nav, footer, and splash and a side bar.

I only want the middle portion of my website to change based on the menu link what user clicks on.

Below is my code for index.php

<?php
include "/templates/header.php";
include "templates/menu.php";
include "/templates/splash.php";
$action = "index"; 
$disallowed_paths = array('header','menu','splash','bottom_page', 'footer'); 
if (!empty($_GET['action'])) 
{ 
    $tmp_action = basename($_GET['action']); 
       if (!in_array($tmp_action, $disallowed_paths) && file_exists("/content/$tmp_action.php")) 
        $action = $tmp_action; 
} 
include "/content/$action.php";  
include "/templates/bottom_page.php";
include "/templates/footer.php";
?>

My menu.php contains links for Home, About, products, services and login links I just want to change the main_index.php to include the above based on what user clicks on.

please advise if this approach is good. or should I create similar file as index.php multiple times with includes to each file as per the link clicked on menu

Upvotes: 3

Views: 1925

Answers (2)

insanebits
insanebits

Reputation: 838

I think better approach would be whitelisting instead of blacklisting.

$existing_pages = array('home', 'contact', 'terms-of-service'); 

if(isset($_GET['action'] && in_array($_GET['action'], $existing_pages)
{
    // load action here
} else {
    //show 404 error
}

Note that your overall approach is not ideal, you could look like into modern frameworks like Laravel or Symphony which has templating systems which helps alot.

But for learning purposes this is fine:)

Upvotes: 0

Meenesh Jain
Meenesh Jain

Reputation: 2528

Your Answer is

GET method

You can use get method for that

 <ul>
        <li><a href="?page=example_1">example 1</a></li>
        <li><a href="?page=example_2">example 2</a></li>
        <li><a href="?page=example_3">example 3</a></li>
        <li><a href="?page=example_4">example 4</a></li>
</ul>

    After User Clicks on the link 

    <?php 
        if(isset($_GET['page']) && $_GET['page']!=""){
            $page = "";
            switch ($_GET['page']) {
                case 'example_1':
                    $page = "Page_1.php";
                    break;

                case 'example_2':
                    $page = "Page_2.php";
                    break;

                case 'example_3':
                    $page = "Page_3.php";
                    break;

                case 'example_4':
                    $page = "Page_4.php";
                    break;

                default:
                    $page = "any_default_page.php";
                    break;
            }

            include($page);
        }
     ?>

And there are other ways also. but this is the most easy and efficient

Upvotes: 1

Related Questions