我有7个不同的页面.考虑到布局,所有页面都是相同的,但考虑到内容,它们是不同的.内容由数据库提供,具体取决于用户单击的内容.
我想你必须使用URL传递变量,然后定义要加载的内容,对吧?
所以,这是我的菜单项:
<a id="menupag" class="item1" href="index.php?page=groups?group1">
这是我的索引:
<div class="content">
<?php
include_once ('content/'.$_GET['page'].'.php');
?>
</div>
Run Code Online (Sandbox Code Playgroud)
但出于某种原因,当我点击菜单项时,会显示以下消息:
Warning: include_once(content/groups?group1.php): failed to open stream: No such file or directory in httpd.www/new/index.php on line 32
Run Code Online (Sandbox Code Playgroud)
我该怎么办才能让php忽略URL的最后一部分(这部分只用于定义数据库必须返回的数据)并且只是听取index.php?page=groups?
您以错误的方式传递URL中的参数:
你应该把它改成:
<a id="menupag" class="item1" href="index.php?page=groups&group=group1">
Run Code Online (Sandbox Code Playgroud)
所以你将拥有$_GET超全局两个值:
$_GET['page']; // value: groups
$_GET['group']; // value: group1
Run Code Online (Sandbox Code Playgroud)
但是使用:
include_once ('content/'.$_GET['page'].'.php');
Run Code Online (Sandbox Code Playgroud)
是完全不安全的.更好的解决方案是:
$allow = array('groups', 'about', 'users');
$page = in_array($_GET['page'], $allow) ? $_GET['page'] : 'default';
include_once ('content/'.$page.'.php');
Run Code Online (Sandbox Code Playgroud)