简体   繁体   English

使用PHP的动态导航包括

[英]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 下面是我的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. 我的menu.php包含HomeAbout产品服务登录链接,我只是想根据用户单击的内容更改main_index.phpinclude上述内容。

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 还是我应该多次创建与index.php类似的文件,并按菜单上的链接将每个文件都包含在内

Your Answer is 你的答案是

GET method GET方法

You can use get method for that 您可以使用get方法

 <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 但这是最简单,最有效的

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. 请注意,您的整体方法并不理想,您可能会喜欢使用LaravelSymphony类的现代框架,这些框架具有很多模板系统。

But for learning purposes this is fine:) 但是出于学习目的,这很好:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM