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

时间:2011-08-28 11:28:05

标签: request php

<?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

4 个答案:

答案 0 :(得分:1)

尝试检查$_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");
}

答案 1 :(得分:1)

请改为:

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,您可以使用$_REQUEST['c']轻松处理default:的无法识别的值,并且还可以更轻松地为将来$_REQUEST['c']的新值添加处理程序。

答案 2 :(得分:0)

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

您可以使用名为“isset”的便捷功能来完成此操作,以确定是否已发送请求。你可以阅读它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");
}
?>

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

答案 3 :(得分:0)

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';
}