繁体   English   中英

使用$ _REQUEST访问PHP页面时出错

[英]Error when visiting PHP page, using $_REQUEST

<?php
if ($_REQUEST['c'] == "1") {
    echo "<p style=\"text-align: center;\">Your shell account has been setup. Please check your inbox.</p>";
}
elseif ($_REQUEST['c'] == "2") {
    echo "<p style=\"color: red; text-align: center;\">There was a problem setting up your account! Please contact Fike via his details <a href=\"http://fike.me\">here</a>.</p>";
}
else {
    include("elements/signup_form.php");
}

有人能告诉我我做错了什么吗? 当我请求普通页面时,它应该显示表单,但是它给了我这些错误:

Notice: Undefined index: c in C:\\xampp\\htdocs\\index.php on line 27

Notice: Undefined index: c in C:\\xampp\\htdocs\\index.php on line 30

尝试检查$_REQUEST['c']存在:

<?php
if (isset($_REQUEST['c']) && $_REQUEST['c'] == "1") {
    echo "<p style=\"text-align: center;\">Your shell account has been setup. Please check your inbox.</p>";
}
elseif (isset($_REQUEST['c']) && $_REQUEST['c'] == "2") {
    echo "<p style=\"color: red; text-align: center;\">There was a problem setting up your account! Please contact Fike via his details <a href=\"http://fike.me\">here</a>.</p>";
}
else {
    include("elements/signup_form.php");
}

改为:

if (isset($_REQUEST['c'])) switch ($_REQUEST['c']) {
  case '1':
    echo "<p style=\"text-align: center;\">Your shell account has been setup. Please check your inbox.</p>";
    break;
  case '2':
    echo "<p style=\"color: red; text-align: center;\">There was a problem setting up your account! Please contact Fike via his details <a href=\"http://fike.me\">here</a>.</p>";
    break;
  default:
    require("elements/signup_form.php");
    break;
} else require("elements/signup_form.php");

使用switch可以使用default:值轻松处理$_REQUEST['c']无法识别的值default:并且还可以在将来更轻松地为$_REQUEST['c']新值添加处理程序。

错误基本上是说c没有定义,因此无法检查它是1还是2,你需要确保首先设置变量。

您可以使用名为“isset”的便捷功能来完成此操作,以确定是否已发送请求。 你可以在这里阅读它。

<?php
if (isset($_REQUEST['c'] && $_REQUEST['c'] == "1") {
        echo "<p style=\"text-align: center;\">Your shell account has been setup. Please check your inbox.</p>";
}
elseif (isset($_REQUEST['c'] && $_REQUEST['c'] == "2") {
    echo "<p style=\"color: red; text-align: center;\">There was a problem setting up your account! Please contact Fike via his details <a href=\"http://fike.me\">here</a>.</p>";
}
else {
    include("elements/signup_form.php");
}
?>

我不是最好的PHP人,但可能会有效:)

if (isset($_REQUEST['c']) && $_REQUEST['c'] === "1")
{
    echo '<p style="text-align: center;">Your shell account has been setup. Please      check your inbox.</p>';
}
else if (isset($_REQUEST['c']) && $_REQUEST['c'] === "2")
{
    echo '<p style="color: red; text-align: center;">There was a problem setting up your account! Please contact Fike via his details <a href="http://fike.me">here</a>.</p>';
}
else 
{
    require 'elements/signup_form.php';
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM