简体   繁体   中英

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:

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

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.

You can do this using the convenient function called 'isset' to work out if the request has been sent. 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 :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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