使用PHP的动态导航包括
我有一个关于如何动态更改网页中显示的内容的问题。 我有几个部分的网站固定-标题,导航,页脚,和飞溅和一个侧栏 我只想我的网站的中间部分改变的基础上,用户点击的菜单链接 下面是我的index.php代码使用PHP的动态导航包括,php,dynamic,include,nav,Php,Dynamic,Include,Nav,我有一个关于如何动态更改网页中显示的内容的问题。 我有几个部分的网站固定-标题,导航,页脚,和飞溅和一个侧栏 我只想我的网站的中间部分改变的基础上,用户点击的菜单链接 下面是我的index.php代码 <?php include "/templates/header.php"; include "templates/menu.php"; include "/templates/splash.php"; $action = "index"; $disallowed_paths = arra
<?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";
?>
Mymenu.php包含主页、关于、产品、服务和登录链接
我只想根据用户点击的内容将main_index.php更改为include
请告知此方法是否有效。
或者我应该多次创建类似于index.php的文件,并根据菜单上单击的链接包含每个文件您的答案是
方法
您可以使用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);
}
?>
用户点击链接后
还有其他的方法。但这是最简单、最有效的我认为更好的方法应该是白名单而不是黑名单
$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
}
请注意,您的总体方法并不理想,您可以像现代框架中的Laravel
或Symphony
那样使用模板系统,这有很多帮助
但出于学习目的,这很好:)如果您想编程或使用它,请查看smarty或simular模板系统,以便更好地理解