错误在哪里?
Where is the error?
我将在 index.php
上使用此代码制作一个算法菜单
<?php
$menu=$_GET[menu];
$file=$_GET[file];
if(!isset($menu))
{
$menu="home";
}
if(($menu=="'")||($menu="-")||($menu="/"))
{
$menu="home";
}
switch($menu)
{
case'home':
$file="isi/home.html";
break;
case'profile':
$file="isi/profile.html";
break;
case'gallery':
$file="isi/lihat_bukutamu.php";
break;
case'download':
$file="isi/lihat_bukutamu.php";
break;
case'contact':
$file="isi/buku_tamu.php";
break;
}
include"header.php";
include"menu.php";
include"content.php";
include"footer.php";
?>
但是当我尝试这个脚本时它告诉我
Notice: Use of undefined constant menu - assumed 'menu' in
C:\xampp\htdocs\layout\index.php on line 2
Notice: Undefined index: menu in C:\xampp\htdocs\layout\index.php on
line 2
Notice: Use of undefined constant file - assumed 'file' in
C:\xampp\htdocs\layout\index.php on line 3
Notice: Undefined index: file in C:\xampp\htdocs\layout\index.php on
line 3
这是menu.php
<div id="menu-content">
<div id="menu">
<h3 class ="judul_1">Main Menu</h3>
<ul>
<li><a href="index.php?menu=home">Home</a></li>
<li><a href="index.php?menu=profile">Profile</a></li>
<li><a href="index.php?menu=gallery">Gallery</a></li>
<li><a href="index.php?menu=download">Download</a></li>
<li><a href="index.php?menu=contact">Contact</a></li>
</ul>
</div>
你能解决吗?
在您的变量周围添加引号,如:
$menu=$_GET['menu'];
$file=$_GET['file'];
...
您在未定义的数组中指定常量。指定为字符串
改变这个
$menu=$_GET[menu];
$file=$_GET[file];
至
$menu=$_GET['menu'];
$file=$_GET['file'];
希望对您有所帮助
如其他答案中指出的那样添加引号将消除通知,但不会改变脚本的工作方式(PHP 仍然会添加引号)。
您没有对 $file
变量做任何事情。我猜您想将其包含在页眉和页脚之间的某处。
include "header.php";
include "menu.php";
include "content.php";
include $file;
include "footer.php";
注意 我不建议通过 GET/POST 传递 file
,因为攻击者可以从您的文件系统中创建脚本 include/display 任意文件如果没有妥善保护。
首先,您在 $_GET[menu] 中缺少引号。还有一件事,您还应该在获取请求参数时始终使用 isset 或 empty。
示例:
$menu= !empty($_GET["menu"]) ? $_GET["menu"] : '';
我将在 index.php
上使用此代码制作一个算法菜单 <?php
$menu=$_GET[menu];
$file=$_GET[file];
if(!isset($menu))
{
$menu="home";
}
if(($menu=="'")||($menu="-")||($menu="/"))
{
$menu="home";
}
switch($menu)
{
case'home':
$file="isi/home.html";
break;
case'profile':
$file="isi/profile.html";
break;
case'gallery':
$file="isi/lihat_bukutamu.php";
break;
case'download':
$file="isi/lihat_bukutamu.php";
break;
case'contact':
$file="isi/buku_tamu.php";
break;
}
include"header.php";
include"menu.php";
include"content.php";
include"footer.php";
?>
但是当我尝试这个脚本时它告诉我
Notice: Use of undefined constant menu - assumed 'menu' in C:\xampp\htdocs\layout\index.php on line 2
Notice: Undefined index: menu in C:\xampp\htdocs\layout\index.php on line 2
Notice: Use of undefined constant file - assumed 'file' in C:\xampp\htdocs\layout\index.php on line 3
Notice: Undefined index: file in C:\xampp\htdocs\layout\index.php on line 3
这是menu.php
<div id="menu-content">
<div id="menu">
<h3 class ="judul_1">Main Menu</h3>
<ul>
<li><a href="index.php?menu=home">Home</a></li>
<li><a href="index.php?menu=profile">Profile</a></li>
<li><a href="index.php?menu=gallery">Gallery</a></li>
<li><a href="index.php?menu=download">Download</a></li>
<li><a href="index.php?menu=contact">Contact</a></li>
</ul>
</div>
你能解决吗?
在您的变量周围添加引号,如:
$menu=$_GET['menu'];
$file=$_GET['file'];
...
您在未定义的数组中指定常量。指定为字符串
改变这个
$menu=$_GET[menu];
$file=$_GET[file];
至
$menu=$_GET['menu'];
$file=$_GET['file'];
希望对您有所帮助
如其他答案中指出的那样添加引号将消除通知,但不会改变脚本的工作方式(PHP 仍然会添加引号)。
您没有对 $file
变量做任何事情。我猜您想将其包含在页眉和页脚之间的某处。
include "header.php";
include "menu.php";
include "content.php";
include $file;
include "footer.php";
注意 我不建议通过 GET/POST 传递 file
,因为攻击者可以从您的文件系统中创建脚本 include/display 任意文件如果没有妥善保护。
首先,您在 $_GET[menu] 中缺少引号。还有一件事,您还应该在获取请求参数时始终使用 isset 或 empty。
示例:
$menu= !empty($_GET["menu"]) ? $_GET["menu"] : '';