提问人:A.Mac 提问时间:4/1/2021 最后编辑:A.Mac 更新时间:4/1/2021 访问量:217
不使用 $_SERVER[“PHP_SELF”] 的 Php 服务器端验证
Php server side validation without using $_SERVER["PHP_SELF"]
问:
我对服务器端验证相当陌生,我目前将我的表单值发布到下面的页面。
<form id="sign_in" action="<?php echo htmlspecialchars('inc/validate.php') ?>" method="POST">
我在页面上执行其他操作,因此我希望所有代码都集中在一个地方。我想在此页面上处理我的所有验证,并将错误消息发送回表单页面。但是,我看到的很多示例都在使用:
action="<?php echo $_SERVER['PHP_SELF']; ?>"
validate.php具有以下代码
<?php
include('prc_input_validation.php');
// check if username and password isset and are not empty
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Retrieve values from login form
$username = checkInput($_POST['username']);
$password = checkInput($_POST['password']);
$userNameempty = emptyCheck($username);
if($userNameempty == "true")
{
$empty_error = "This field is required";
header("location: ../sign-in.php");
}
}
?>
prc_input_validation.php具有以下代码
<?php
function checkInput($inputField) {
$inputField = trim($inputField); // Strip whitespace (or other characters) from the beginning and end of a string
$inputField = stripslashes($inputField); //Un-quotes a quoted string
$inputField = htmlspecialchars($inputField); //Convert special characters to HTML entities
return $inputField;
}
function emptyCheck($inputField)
{
if(empty($inputField))
{
return "true";
}
else
{
return "false";
}
}
?>
表单输入
<input type="email" class="form-control" name="username" placeholder="Username" autofocus required>
<span class="text-danger"><?php echo $empty_error; ?></span>
如何正确地将错误消息发送回表单以使其显示?在表格上
答: 暂无答案
评论
include
action="?"
$_SERVER['PHP_SELF']