Validating $ _POST Data

I do spam check on the form. The code below works the same as on my local host (redirecting to google.com if true), however this does not happen when it is on the production server (it runs the rest of the script and does NOT redirect to Google.com).

    if('POST' == $_SERVER['REQUEST_METHOD']) {
    if ($_POST["bait"]!='' || $_POST["date"] == "12/31/69" || trim($_POST["date"] == "1969-12-31")) {
        header("location: http://www.google.com");
    } else {
       //Process form here

I did var_dumpon $_POSTand this1969-12-31

What am I doing wrong?

+3
source share
1 answer

You should exit;after sending the location header to prevent the rest of the script from executing.

eg.

if('POST' == $_SERVER['REQUEST_METHOD']) {
    if ($_POST["bait"]!='' || $_POST["date"] == "12/31/69" || trim($_POST["date"] == "1969-12-31")) {
        header("location: http://www.google.com");
        exit;
    } else {
        // process form here
    }
}

The redirect is sent, but you also continue to output the rest of the request, in which case the behavior may be undefined.

+4
source

All Articles