简体   繁体   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");
}

Could somebody tell me what I'm doing wrong? 有人能告诉我我做错了什么吗? When I request the normal page, it's supposed to display the form, however it gives me these errors: 当我请求普通页面时,它应该显示表单,但是它给了我这些错误:

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

Try checking if $_REQUEST['c'] exists as well: 尝试检查$_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");
}

Do this instead: 改为:

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");

Using switch allows you to easily handle unrecognised values of $_REQUEST['c'] with a default: and also makes it easier to add handlers for new values of $_REQUEST['c'] in the future. 使用switch可以使用default:值轻松处理$_REQUEST['c']无法识别的值default:并且还可以在将来更轻松地为$_REQUEST['c']新值添加处理程序。

What the error is basically saying is that c is not defined so it can't check if it is 1 or 2, you need to make sure that the variable is set first. 错误基本上是说c没有定义,因此无法检查它是1还是2,你需要确保首先设置变量。

You can do this using the convenient function called 'isset' to work out if the request has been sent. 您可以使用名为“isset”的便捷功能来完成此操作,以确定是否已发送请求。 You can read about it here . 你可以在这里阅读它。

<?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");
}
?>

I'm not the best PHP guy but that might work :) 我不是最好的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