php - 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);
解决方案
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);
推荐阅读
- javascript - 如何在从帮助脚本导入的变量更改上重新渲染组件?
- python - 如何摆脱错误:jack 服务器未运行或无法启动
- yocto - 如何将内核补丁绑定到不同的配方
- python - 向列表列表中的每个第 n 个元素插入值
- reactjs - 运行“npm run build”时如何忽略文件
- c# - Spotify API 客户端获取播放列表曲目偏移量
- r - 我应该如何处理shinyapp中的twitter auth token?
- linux - Shell 帮助替换双引号
- python - 如何从 python 脚本中使用 7z
- r - 在 facet_grid ggplot 栏中按降序绘制排序值