我想要一个表单服务器验证,我已经验证了所有单独的字段,所有工作,我唯一无法工作的验证是验证表单在提交时不为空。在提交表单之后的下面的代码中,它在if(isset($ post语句)中收到。我想要做的是检查字段不为空,然后如果为空则创建错误消息然后使用标题('位置:)在会话变量中转到一个定义的页面,错误消息。如果验证返回填充字段,那么if语句的else部分将通过单独的验证(这一切都按要求工作)。你可以在代码中看到我尝试了不同的选项来验证表单字段是否为空,我尝试过的这些选项被注释掉了,第一个if语句中有一个,而在哈希密码之前有一个if语句。我做有客户端验证,但正如堆栈溢出中经常提到的那样,做服务器端验证是一种好习惯。 因此,如果有人愿意帮我解决这个问题,我将非常感激
php code ***************************
<?php
include_once 'db_connect.php';
include_once 'functions.php';
sec_session_start();
error_reporting(E_ALL); ini_set('display_errors', 1);
$emailErr = $passwordErr = $password_matchErr = $email_existErr = $nameErr = $name_existErr = $confirm_passwordErr = $emptyErr = '';
$email = $name = $password = $confirm_password = $results = $row = $db_email = $db_id = $db_name = $db_password = '';
if (isset($_POST['name2'], $_POST['email2'], $_POST['paswword2'],$_POST['confirm_password2'])) {
// $emptyErr = 'Please fill in all the Fields';
// $_SESSION['emptyErr'] = $emptyErr;
//// header('location: ../join.php');
//echo $emptyErr;
//echo 'emptyform';
// die();
}else{
$name = filter_input(INPUT_POST, 'name2', FILTER_SANITIZE_STRING);
// Sanitize the data passed in 'email'
$email = filter_input(INPUT_POST, 'email2', FILTER_SANITIZE_EMAIL);
// validate the data passed in 'email'
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
// check if email is valid
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Not a valid email
$emailErr = "The email address you entered is not valid";
}
//Sanitize the data passed in 'password'
$password = filter_input(INPUT_POST, 'password2', FILTER_SANITIZE_STRING);
//validate the data passed in 'password'
if (preg_match("/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/", $password)) {
} else {
$passwordErr = "Password is invalid!<br>Please ensure your password is formatted as described when filling in the form!";
}
//Sanitize the data passed in 'confirm_password'
$confirm_password = filter_input(INPUT_POST, 'confirm_password2', FILTER_SANITIZE_STRING);
//check that password and confirm password match
if ($password != $confirm_password) {
// error matching passwords
$confirm_passwordErr = "Your passwords do not match.<br>Please type carefully.";
}
// if($name == '' && $email == '' && $password == '' && $confirm_password = '') {
// $emptyErr - 'Please fill in all the Fields';
// $_SESSION['emptyErr'] = $emptyErr;
//// header('location: ../join.php');
//echo $emptyErr;
// die();
// }else{
// }
$hash = password_hash($password, PASSWORD_DEFAULT);
}
//echo 'test8';
//echo '<br>';
$query = ("SELECT * FROM signed_up WHERE (email = '$email') OR (name = '$name')");
$results = mysqli_query($mysqli, $query);
while ($row = mysqli_fetch_array($results, MYSQLI_BOTH)){
//echo 'test4';
//echo '<br>';
$db_id = $row['id'];
$db_name = $row['name'];
$db_email = $row['email'];
$db_password = $row['password'];
}
//echo $db_id;
//echo '<br>';
//echo $db_name;
//echo '<br>';
//echo $db_email;
//echo '<br>';
//echo $db_password;
//check if user exsists, if confirmed go to the login page so user can login.
if ($db_name === $name && $db_email === $email){
$regErr = 'User Already Exsists!<br>Please Login';
$_SESSION['regErr'] = $regErr;
header('location: ../login.php');
//echo $regErr;
die();
}else
//echo $name;
//echo $db_id;
if($db_name === $name && $db_email !== $email){
//echo 'test4';
$name_existErr = 'Username has to be Unique!<br>If this is your Username Please Login!<br>If this is not your Username Please Join with a different Username!';
//echo $name_existErr;
}
//echo $db_id;
//echo '<br>';
//echo $db_name;
//echo '<br>';
//echo $db_email;
//echo '<br>';
//echo $db_password;
if($db_email === $email && $db_name !== $name){
$email_existErr = 'Email has to be Unique<br>If this is your Email Please goto Login!<br>If this is not your Email check your Email and fill the Form in Again!';
//echo $email_existErr;
}
if($emailErr =='' && $passwordErr =='' && $confirm_passwordErr == '' && $email_existErr == '' && $name_existErr == '' && $emptyErr = ''){
//if all valid and no errors, register new user to the database.
$_SESSION['email'] = $email;
$_SESSION['hash'] = $hash;
$_SESSION['name'] = $name;
echo 'no errors';
header('location: register_user.inc.php');
}else{
//if validation shows errors go back to join form.
// $_SESSION['emptyErr'] = $emptyErr;
$_SESSION['email_existErr'] = $email_existErr;
$_SESSION['name_existErr'] = $name_existErr;
$_SESSION['emailErr'] = $emailErr;
$_SESSION['passwordErr'] = $passwordErr;
$_SESSION['confirm_passwordErr'] = $confirm_passwordErr;
//echo $email_existErr;
//echo '<br>';
//echo $name_existErr;
//echo '<br>';
//echo $confirm_passwordErr;
//echo '<br>';
//echo $emailErr;
//echo '<br>';
//echo $password_matchErr;
//echo '<br>';
//echo $passwordErr;
header('Location: ../join.php');
}
//echo $name;
//echo $email;
//echo $confirm_password;
//echo $password;
//echo $hash;
?>
php代码结束******************************
Html代码********************************
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Dot Mov Home</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="/styles/home.css" rel="stylesheet" type="text/css">
<!--The following script tag downloads a font from the Adobe Edge Web Fonts server for use within the web page. We recommend that you do not modify it.-->
<script>var __adobewebfontsappname__="dreamweaver"</script>
<script src="http://use.edgefonts.net/lemon:n4:default.js" type="text/javascript"></script>
<script src="/js/civem.js"></script>
<script type='text/javascript' src='http://code.jquery.com/jquery.min.js'></script>
<script src="http://code.jquery.com/jquery-1.7.min.js"></script>
<script src="/js/confirm_password__dbvalidate.js"></script>
<script src="/js/email_dbvalidate.js"></script>
<script src="/js/pswd_match.js"></script>
<script src="/js/pswrd_strength.js"></script>
<script src="/js/username_dbvalidate.js"></script>
<script type="text/javascript">
function SwapDivsWithClick(div1,div2)
{
d1 = document.getElementById(div1);
d2 = document.getElementById(div2);
if( d2.style.display == "none" )
{
d1.style.display = "none";
d2.style.display = "block";
}
else
{
d1.style.display = "block";
d2.style.display = "none";
}
}
</script>
</head>
<body>
<div id="video_container">
<video muted autoplay loop >
<source src="/video/South Dakota Badlands Scenic Byway 720p (Video Only)_1.3gp" type="video/3gp">
<source src="/video/South Dakota Badlands Scenic Byway 720p (Video Only).webm" type="video/webm">
<source src="/video/South Dakota Badlands Scenic Byway 720p.ogg" type="video/ogg">
Your browser does not support the video tag. I suggest you upgrade your browser. </video>
</div>
<header>
<div class="upload"><a href="upload.html" title="Upload" target="_self">UPLOAD</a></div>
<div class="view"><a href="view.php" title="View" target="_self">VIEW</a></div>
<div class="spacer1"></div>
<div class="search_bar">
<form action="/search_results.php" method="post" name="search_database" autocomplete="on">
<input type="text" class="search" placeholder="Search">
</form>
</div>
<div class="logo_text">.MOV </div>
<div class="tagline">Motorcycle Online Video</div>
</a></header>
<main>
<div id="login" style="display:block">
<form method="post" action="includes/login.inc.php" id="loginform">
<input name="email" type="email" id="email" form="loginform" placeholder="Please Enter Your Email">
<input name="password" type="password" id="password" form="loginform" placeholder="Please Enter Your Password" title="Please Enter Your Password">
<div class="submit_buttons">
<input type="submit" class="login_btn" form="loginform" formaction="includes/login.inc.php" title="Login" value="Login">
<a href="javascript:SwapDivsWithClick('login','join')"><div class="join_but">Or Join</div></a>
</div>
<div id="forgotten_password">Forgotten Password</div>
<div class="login_statements">
<div class="statement1">
<div class="by_joining">By Joining</div>
<div class="dot_mov">.MOV</div>
<div class="agree">You agree to our</div>
</div>
<div class="statement2">
<div class="terms_link"><a href="terms.html" title="Terms of Service" target="_self">Terms of Service</a></div>
<div class="and">and</div>
<div class="service_link"><a href="privacy.html" title="Privacy Policy" target="_self">Privacy Policy</a></div>
</div>
</div>
<div class="facebook_login">
<div class="facebook_icon"><img src="images/fb.png" class="fb_icon"></div>
<div class="fb_link">Login with FaceBook </div>
</div>
</form>
</div>
</div>
<div id="join" style="display: none;">
<form action="includes/register.inc.php" method="post" id="joinform">
<input name="name2" type="text" id="name2" form="joinform" placeholder="Please Enter Your Username" title="Please Enter Your Username">
<div id="user-name">
<h4>Username must meet the following requirements!<br>If the Usename doesnt meet the requirements you will be required to fill in the form again!</h4>
<div id="name_result"></div>
<div id="name_length" class="invalid">At least <strong>6 letters</strong></div>
</div>
<input name="email2" type="text" id="email2" form="joinform" placeholder="Please Enter Your Email" title="Please Enter a Valid Email">
<div id="user-email">
<h4>Email must be a valid Email format!<br>If the Email doesnt meet the requirements you will be required to fill in the form again!</h4>
<div id="email_result">
<div id="email_validate" class="invalid">Email Valid</div>
<div id="emaildb_validate"></div>
</div>
</div>
<input name="password2" type="password" id="password2" form="joinform" placeholder="Please Enter Your Password" title="Please Enter Your Password">
<div id="pswd_info">
<h4>Password must meet the following requirements!<br>If the Password doesnt meet the requirements you will be required to fill in the form again!</h4>
<ul id="pswd_list">
<li id="letter" class="invalid">At least <strong>one letter</strong></li>
<li id="capital" class="invalid">At least <strong>one capital letter</strong></li>
<li id="number" class="invalid">At least <strong>one number</strong></li>
<li id="length" class="invalid">Be at least <strong>8 characters</strong></li>
</ul>
</div>
<input name="confirm_password2" type="password" id="confirm_password2" form="joinform" placeholder="Please Confirm Your Password" title="Please Confirm Your Password">
<div id="user-confirm_password">
<h4>Please Confirm Password<br>If the Passwords do not match, you wil be required to fill in the form again!</h4>
<div id="error" class="error"></div>
<div id="confirm_match" class="invalid">Passwords Match</div>
</div>
<div class="submit_buttons2">
<input name="join_btn2" type="submit" id="join_btn2" form="joinform" formaction="includes/register.inc.php" " formmethod="POST" title="Join" value="Join">
<a href="javascript:SwapDivsWithClick('login','join')"><div class="join_btn2">Or Login</div></a>
</div>
<div class="login_statements2">
<div class="statement1">
<div class="by_joining">By Joining</div>
<div class="dot_mov">.MOV</div>
<div class="agree">You agree to our</div>
</div>
<div class="statement2">
<div class="terms_link"><a href="terms.html" title="Terms of Service" target="_self">Terms of Service</a></div>
<div class="and">and</div>
<div class="service_link"><a href="privacy.html" title="Privacy Policy" target="_self">Privacy Policy</a></div>
</div>
</div>
<div class="facebook_login2">
<div class="facebook_icon"><img src="images/fb.png" class="fb_icon"></div>
<div class="fb_link">Login with FaceBook </div>
</div>
</form>
</div>
</div>
</div>
<div class="scroll_container">
<a data-scroll href="#body2"><div class="scroll_link">
<div class="arrow"><img src="/images/arrow.png" alt="" class="arrow_icon"/></div>
<div class="arrow3"><img src="/images/arrow.png" alt="" class="arrow_icon"/></div>
Scroll Down</div></a>
</div>
</main>
<div class="body2" id="body2">
<div class="vid_grid">
<div class="top_section">
<div class="top_left_quarter"></div>
<div class="top_right_quarter">
<div class="top_right_left_quarter"></div>
<div class="top_right_right_quarter"></div>
<div class="top_right_bottom_left"></div>
<div class="top_right_bottom_right"></div>
</div>
</div>
<div class="bottom_section">
<div class="bottpm_left_top"></div>
<div class="bottpm_left_bottom"></div>
<div class="bottom_middle"></div>
<div class="bottom_left_quarter"></div>
<div class="bottom_right_quarter"></div>
<div class="bottom_right_top"></div>
<div class="bottom_right_bottom"></div>
</div>
<a href="/staff_picks.php"><div class="staff_picks">Staff Picks </div></a>
</div>
</div>
<footer class="footer">
<div id="breadcrumbs"><a href="/terms.html" title="Terms" target="_self">Terms</a> | <a href="/privacy.html" title="Privacy" target="_self">Privacy</a> | <a href="/about.html" title="About Us" target="_self">About Us</a> | <a href="/copyright.html" title="Copyright" target="_self">Copyright</a> | <a href="/cookies.html" title="Cookies" target="_self">Cookies</a> | ® © 2015</div><img src="/images/.mov.png" alt="" width="42" height="14" class="logo"/>
<div class="social_media"><img src="/images/fb.png" alt="" width="30" height="30" class="fbicon"/><img src="/images/twitter.png" alt="" width="32" height="32" class="twittericon"/><img src="/images/googleplus.png" alt="" width="32" height="32" class="googleplusicon"/></div>
</footer>
<script src="/js/smooth-scroll.js"></script>
<script src="/js/smooth-scroll.min.js"></script>
<script type="text/javascript">
smoothScroll.init({
speed: 1000,
easing: 'easeInOutCubic',
offset: 0,
updateURL: true,
callbackBefore: function ( toggle, anchor ) {},
callbackAfter: function ( toggle, anchor ) {}
});
</script>
</body>
</html>
html代码结束***************************
you can see the validation as is at this url 单击此表单的加入按钮
答案 0 :(得分:0)
严重的是,不要在此处转储代码页面和页面,希望其他人调试它。
你可以这样简单地检查:
$valid = true;
$requiredFields = array('name', 'age', 'email');
foreach ($requiredFields as $requiredField) {
if( !isset($_POST[$requiredField] ) {
$valid = false;
break;
}
if (!$valid) {
header('Location: /missingfields.php');
}
但是,不使用重定向会更好,因为用户输入的所有数据都将消失。您可以在不处理输入的情况下再次输出相同的页面,然后在HTML中预填充值:
<input name="email" value="<?=htmlspecialchars($_POST['email'])?>">
答案 1 :(得分:-1)
您可以通过简单的条件(如
)验证每个字段是否为空if($_POST['fieldname'] == "")
return $error
以下是第一个字段的已编辑代码,您可以对其他字段执行相同操作
sec_session_start();
error_reporting(E_ALL); ini_set('display_errors', 1);
$emailErr = $passwordErr = $password_matchErr = $email_existErr = $nameErr = $name_existErr = $confirm_passwordErr = $emptyErr = '';
$email = $name = $password = $confirm_password = $results = $row = $db_email = $db_id = $db_name = $db_password = '';
if (isset($_POST['name2'], $_POST['email2'], $_POST['paswword2'],$_POST['confirm_password2'])) {
if($_POST['name2'] == "")
{
$emptyErr = 'Please fill in all the Fields';
$_SESSION['emptyErr'] = $emptyErr;
header('location: ../join.php');
echo $emptyErr;
echo 'emptyform';
die();
}
}else{
$name = filter_input(INPUT_POST, 'name2', FILTER_SANITIZE_STRING);
// Sanitize the data passed in 'email'
$email = filter_input(INPUT_POST, 'email2', FILTER_SANITIZE_EMAIL);
// validate the data passed in 'email'
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
// check if email is valid
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Not a valid email
$emailErr = "The email address you entered is not valid";
}
//Sanitize the data passed in 'password'
$password = filter_input(INPUT_POST, 'password2', FILTER_SANITIZE_STRING);
//validate the data passed in 'password'
if (preg_match("/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/", $password)) {
} else {
$passwordErr = "Password is invalid!<br>Please ensure your password is formatted as described when filling in the form!";
}
//Sanitize the data passed in 'confirm_password'
$confirm_password = filter_input(INPUT_POST, 'confirm_password2', FILTER_SANITIZE_STRING);
//check that password and confirm password match
if ($password != $confirm_password) {
// error matching passwords
$confirm_passwordErr = "Your passwords do not match.<br>Please type carefully.";
}
// if($name == '' && $email == '' && $password == '' && $confirm_password = '') {
// $emptyErr - 'Please fill in all the Fields';
// $_SESSION['emptyErr'] = $emptyErr;
//// header('location: ../join.php');
//echo $emptyErr;
// die();
// }else{
// }
$hash = password_hash($password, PASSWORD_DEFAULT);
}
//echo 'test8';
//echo '<br>';
$query = ("SELECT * FROM signed_up WHERE (email = '$email') OR (name = '$name')");
$results = mysqli_query($mysqli, $query);
while ($row = mysqli_fetch_array($results, MYSQLI_BOTH)){
//echo 'test4';
//echo '<br>';
$db_id = $row['id'];
$db_name = $row['name'];
$db_email = $row['email'];
$db_password = $row['password'];
}
//echo $db_id;
//echo '<br>';
//echo $db_name;
//echo '<br>';
//echo $db_email;
//echo '<br>';
//echo $db_password;
//check if user exsists, if confirmed go to the login page so user can login.
if ($db_name === $name && $db_email === $email){
$regErr = 'User Already Exsists!<br>Please Login';
$_SESSION['regErr'] = $regErr;
//header('location: ../login.php');
echo $regErr;
die();
}else
//echo $name;
//echo $db_id;
if($db_name === $name && $db_email !== $email){
echo 'test4';
$name_existErr = 'Username has to be Unique!<br>If this is your Username Please Login!<br>If this is not your Username Please Join with a different Username!';
//echo $name_existErr;
}
//echo $db_id;
//echo '<br>';
//echo $db_name;
//echo '<br>';
//echo $db_email;
//echo '<br>';
//echo $db_password;
if($db_email === $email && $db_name !== $name){
$email_existErr = 'Email has to be Unique<br>If this is your Email Please goto Login!<br>If this is not your Email check your Email and fill the Form in Again!';
//echo $email_existErr;
}
if($emailErr =='' && $passwordErr =='' && $confirm_passwordErr == '' && $email_existErr == '' && $name_existErr == '' && $emptyErr = ''){
//if all valid and no errors, register new user to the database.
$_SESSION['email'] = $email;
$_SESSION['hash'] = $hash;
$_SESSION['name'] = $name;
echo 'no errors';
// header('location: register_user.inc.php');
}else{
//if validation shows errors go back to join form.
// $_SESSION['emptyErr'] = $emptyErr;
$_SESSION['email_existErr'] = $email_existErr;
$_SESSION['name_existErr'] = $name_existErr;
$_SESSION['emailErr'] = $emailErr;
$_SESSION['passwordErr'] = $passwordErr;
$_SESSION['confirm_passwordErr'] = $confirm_passwordErr;
echo $email_existErr;
echo '<br>';
echo $name_existErr;
echo '<br>';
echo $confirm_passwordErr;
echo '<br>';
echo $emailErr;
echo '<br>';
echo $password_matchErr;
echo '<br>';
echo $passwordErr;
//header('Location: ../join.php');
}
//echo $name;
//echo $email;
//echo $confirm_password;
//echo $password;
//echo $hash;
?>