另一个注意事项:未定义的索引问题

发布于 2024-10-27 01:30:38 字数 504 浏览 2 评论 0原文

我不断得到

注意:未定义索引:操作

当我使用以下代码时。我用它来查看需要哪个页面。无论如何要解决这个问题吗?我知道你不应该只包含来自用户输入的文件(不首先检查输入),但是这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

等数载,海棠开 2024-11-03 01:30:38

以这种方式重写你的代码:

$action = isset($_GET['action']) ? $_GET['action'] : null;

switch ($action) { ... }

Rewrite your code in this way:

$action = isset($_GET['action']) ? $_GET['action'] : null;

switch ($action) { ... }
白昼 2024-11-03 01:30:38

该错误意味着您试图在 $_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

无悔心 2024-11-03 01:30:38

您正在使用数组元素而不检查它是否存在。您应该确保您的代码在未定义时不会尝试读取 $_GET['action']

您可以通过为 $_GET['action'] 提供一个定义的(但为“空”)值来完成此操作,而无需更改 switch 逻辑,如果查询字符串中未给出 action

if (!isset($_GET['action']))
    $_GET['action'] = null;

switch ($_GET['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 if action wasn't given in the query string:

if (!isset($_GET['action']))
    $_GET['action'] = null;

switch ($_GET['action']) {
   ...
}
爱殇璃 2024-11-03 01:30:38

有一个特定的语法结构 @ 用于在通知被证明是冗余时忽略通知。

switch (@$_GET['action']) {

isset 三元用作微优化解决方法,但在您的情况下没有实际好处。

There is a specific syntax construct @ for ignoring notices when they are provably redundant.

switch (@$_GET['action']) {

The isset ternary is used as microoptimization workaround, but otherwise has no practical benefit in your case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文