嘿伙计们,如果使用$ _GET,我有问题。我试图在页面上包含基于GET的页面。 导航代码
<a href="index.php?page=setting">Setting</a>
<a href="index.php?page=post">Post Article</a>
Php Code
<?
if($_GET['page']=='post' && $_SESSION['status'] > 0 || $_GET['page']=='setting' && $_SESSION['status'] > 0)
{
include "page/post.php";
}else if{
include "page/setting.php";
}else{
include "page/home.php";
?>
我该如何解决这个问题?
答案 0 :(得分:3)
如果
,请更正else的语法 else if
需要围绕它。
例如else if ($test == TRUE)
更正后的代码:
if($_GET['page']=='post' && $_SESSION['status'] > 0) {
include "page/post.php";
}
else if ($_GET['page']=='setting' && $_SESSION['status'] > 0) {
include "page/setting.php";
}
else {
include "page/home.php";
}
?>
更优化的代码:
if ($_SESSION['status'] > 0) {
$pages = array();
$pages['post'] = 'post';
$pages['setting'] = 'setting';
// You can add more pages, by adding entries in this array.
//e.g. $pages['about'] = 'about';
if (isset($_GET['page']) && isset($pages[$_GET['page']])) {
$page = $pages[$_GET['page']];
include "page/" . $page . ".php";
}
}
else {
include "page/home.php";
}
答案 1 :(得分:1)
如果出现以下情况,您必须为else设置条件:
<?
if(isset($_GET['page']) && $_GET['page']=='post' && $_SESSION['status'] > 0){
include "page/post.php";
}else if(isset($_GET['page']) && $_GET['page']=='setting' && $_SESSION['status'] > 0){
include "page/setting.php";
}else{
include "page/home.php";
}
?>
我还添加了一个安全检查,只是为了确保$ _GET ['page']变量存在(如果设置了变量,则isset返回true)。
如果您的网址中没有此变量,则可能会在没有此检查的情况下出现问题。
更新: 如果你有更多的页面,我会建议使用路由器;-)(Laravel框架有一个)。
但为了保持简单,就像这样:
<?
$pageArray = [
'post' => 'page/post.php',
'home' => 'page/home.php',
'test' => 'page/test.php'
];
if(isset($_GET['page'])){
$page = $_GET['page'];
}else{
$page = null;
}
if($page && isset($pageArray[$page]) && $_SESSION['status'] > 0){
include $pageArray[$page];
}else{
include "page/home.php";
}
?>
始终确保您永远不会include $_GET['page'];
,或者某人可以执行http://youwebsite.com?page=http://hackerwebsite.com之类的操作
并且您将他的页面包含在您的服务器中,可能会造成损害。
所以请务必检查您是否知道所请求的页面(这是我用isset($pageArray[$page])
检查的内容)
答案 2 :(得分:1)
Pupil's answer的略有改进版本:
if ($_SESSION['status'] > 0) {
$pages = array(
'post',
'setting',
'whatever' // add pages
);
if (isset($_GET['page']) && in_array($_GET['page'], $pages)) {
$page = $_GET['page'];
include "page/" . $page . ".php";
}
}
else {
include "page/home.php";
}
答案 3 :(得分:0)
试试这个。并确保会话状态已设置且&gt; 0
if($_GET['page']=='post' && $_SESSION['status'] > 0)
{
include "page/post.php";
} else if($_GET['page']=='setting' && $_SESSION['status'] > 0) {
include "page/setting.php";
} else {
include "page/home.php";
}