另一个注意事项:未定义的索引问题
我不断得到
注意:未定义索引:操作
当我使用以下代码时。我用它来查看需要哪个页面。无论如何要解决这个问题吗?我知道你不应该只包含来自用户输入的文件(不首先检查输入),但是这个 switch 语句仅在操作设置为 view 或 blah 时才有效,否则它只显示主页。
?动作=视图或?动作=废话
switch ($_GET['action'])
{
case 'view':
echo "We are in view";
require FORUM_ROOT . 'view2.php';
break;
case 'blah':
echo "We are in blah";
break;
default:
"This is default";
require FORUM_ROOT . 'main.php';
}
I keep getting
Notice: Undefined index: action
When I use the following code. I use it to see which page is required. Anyway to sort this out? I know you're not supposed to just include files from user input (without checking the input first), but this switch statement only works if action is set to view or blah, otherwise it just shows the main page.
?action=view or ?action=blah
switch ($_GET['action'])
{
case 'view':
echo "We are in view";
require FORUM_ROOT . 'view2.php';
break;
case 'blah':
echo "We are in blah";
break;
default:
"This is default";
require FORUM_ROOT . 'main.php';
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以这种方式重写你的代码:
Rewrite your code in this way:
该错误意味着您试图在 $_GET['action'] 不存在时使用它。例如,当您进入页面而不传递 page.html?action=xxx 时
The error means that you are attempting to use $_GET['action'] when it doesn't exist. Like for instance, when you go to the page without passing page.html?action=xxx
您正在使用数组元素而不检查它是否存在。您应该确保您的代码在未定义时不会尝试读取
$_GET['action']
。您可以通过为
$_GET['action']
提供一个定义的(但为“空”)值来完成此操作,而无需更改switch
逻辑,如果查询字符串中未给出action
:You're using an array element without checking whether it exists. You should make sure that your code does not try to read
$_GET['action']
when that might not be defined.You can do this in a way that doesn't require altering your
switch
logic, by giving$_GET['action']
a defined (but "empty") value ifaction
wasn't given in the query string:有一个特定的语法结构
@
用于在通知被证明是冗余时忽略通知。isset 三元用作微优化解决方法,但在您的情况下没有实际好处。
There is a specific syntax construct
@
for ignoring notices when they are provably redundant.The isset ternary is used as microoptimization workaround, but otherwise has no practical benefit in your case.