首页 > 解决方案 > Why does my PHP function prevent other code from working?

问题描述

The issue my website is running into is that whenever I call the function NoErrors(); all the validation preceding the function stops working, not only that but it does not appear to be adding data into the session. I've got various php validation for user inputs. As well as variables that are initialised at the start of the PHP document. This includes the counter variable $totalErrors as well as any $_POST data that may need to go into the session.

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["cust"]["email"])) {
        $emailErr = "Email is required";
        $totalErrors++;
      } else {
        $email = test_input($_POST["cust"]["email"]);
        // Check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
          $emailErr = "Invalid email format";
          $totalErrors++;
        }
      }
    }

This validation is used to sanitise user input. The function below is to check whether or not the $totalErrors variable has been incremented or not. Depending on whether or not the variable is incremented, it will redirect the user to the 'receipt' page or the current page so the user can correct their mistakes. Also, if it passes the check it's supposed to add the posted data into the $_SESSION. Most data here is just for testing purposes.

function NoErrors($totalErrors, $cleanName, $cleanMobile, $cleanEmail, $cleanCard, $cleanExpiry, $cleanCVV) {
 if ($_SERVER["REQUEST_METHOD"] == "POST" and $totalErrors == 0) {
  $_SESSION['cust']['name'] = $cleanName;
  $_SESSION['cust']['mobile'] = $cleanMobile;
  $_SESSION['cust']['email'] = $cleanEmail;
  $_SESSION['cust']['card'] = $cleanCard;
  $_SESSION['cust']['expiry'] = $cleanExpiry;
  $_SESSION['cust']['cvv'] = $cleanCVV;

header("Location: receipt.php");
} 
elseif ($_SERVER["REQUEST_METHOD"] == "POST" and $totalErrors > 0) {
header("Location: index.php");
}
}

NoErrors($totalErrors, $cleanName, $cleanMobile, $cleanEmail, $cleanCard, $cleanExpiry, $cleanCVV);

标签: php

解决方案


Your function NoErrors() uses lots of variables, which are undefined - $totalErrors, $cleanName, $cleanMobile, etc. If you turn on error reporting, you should see an error to that effect.

To fix this, you must pass in all the variables you're using as arguments to your function.

function NoErrors($totalErrors, $cleanName, $cleanMobile, $cleanEmail, $cleanCard, $cleanExpiry, $cleanCVV) {
    if ($_SERVER["REQUEST_METHOD"] == "POST" and $totalErrors == 0) {
        $_SESSION['cust']['name'] = $cleanName;
        $_SESSION['cust']['mobile'] = $cleanMobile;
        $_SESSION['cust']['email'] = $cleanEmail;
        $_SESSION['cust']['card'] = $cleanCard;
        $_SESSION['cust']['expiry'] = $cleanExpiry;
        $_SESSION['cust']['cvv'] = $cleanCVV;

        header("Location: index.php");
    } 
    elseif ($_SERVER["REQUEST_METHOD"] == "POST" and $totalErrors > 0) {
        header("Location: index.php");
    }
}

NoErrors($totalErrors, $cleanName, $cleanMobile, $cleanEmail, $cleanCard, $cleanExpiry, $cleanCVV);

推荐阅读