php 如何在php中验证电子邮件ID和密码并确认密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27216595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to validate email id and password & confirm password in php
提问by Vijaykarthik
i doing register form in php. i dont know how to validate my entered email and (password and confirm password) in php. For email if i enter same id "It should tell my email alread registered" and for password and confirmpassword should check whether it same or not. I have tried email in my coding. it throwing error email already registed but i want to display error in login screen. Please help for this two things. i am very new to php. sorry for my poor english. I have return my code below,
我在 php 中做注册表。我不知道如何在 php 中验证我输入的电子邮件和(密码和确认密码)。对于电子邮件,如果我输入相同的 id“它应该告诉我的电子邮件已经注册”,对于密码和确认密码应该检查它是否相同。我在我的编码中尝试过电子邮件。它抛出错误电子邮件已经注册,但我想在登录屏幕中显示错误。请帮忙解决这两件事。我对 php 很陌生。对不起我糟糕的英语。我在下面返回了我的代码,
<?php
require('config.php');
// If the values are posted, insert them into the database.
if (isset($_POST['username']) && isset($_POST['password'])){
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$cpassword = $_POST['confirmpassword'];
$slquery = "SELECT 1 FROM register WHERE email = '$email'";
$selectresult = mysql_query($slquery);
if(mysql_num_rows($selectresult)>0)
{
die('email already exists');
}
$query = "INSERT INTO `register` (username, password,confirmpassword, email) VALUES ('$username', '$password', '$cpassword', '$email')";
$result = mysql_query($query);
if($result){
$msg = "User Created Successfully.";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login screen</title>
<style type="text/css">
.register-form{
width: 500px;
margin: 0 auto;
text-align: center;
padding: 10px;
padding: 10px;
background : #c4c4c4;
border-radius: 10px;
-webkit-border-radius:10px;
-moz-border-radius:10px;
}
.register-form form input{padding: 5px;}
.register-form .btn{
background: #726E6E;
padding: 7px;
border-radius: 5px;
text-decoration: none;
width: 50px;
display: inline-block;
color: #FFF;
}
.register-form .register{
border: 0;
width: 60px;
padding: 8px;
}
</style>
</head>
<body>
<div class="register-form">
<?php
if(isset($msg) & !empty($msg)){
echo $msg;
}
?>
<?php
if(isset($errormes))
{
echo $errormes;
}
?>
<h1>Register</h1>
<form action="" method="POST">
<p><label>User Name: </label>
<input type="text" id="username" name="username" placeholder="Username">
</p>
<p><label>E-Mail :</label>
<input id="email" name="email" type="email">
</p>
<p><label>Password :</label>
<input id="password" name="password" type="password">
</p>
<p><label>confirm Password :</label>
<input id="confirmpassword" name="confirmpassword" type="password">
</p>
<input type="submit" name="submit" value="register" class="btn register">
<a class="btn" href="login.php">Login</a>
</form>
</div>
</body>
</html>
回答by Philip G
The die(), function will effectivly stop the execution of your script. This meaning no more code will run after that line.
die(), 函数将有效地停止脚本的执行。这意味着在该行之后将不再运行更多代码。
You probably would like to use something as a header() to redirect the user to a page dispalying the incorrect loginscreen.
您可能希望使用一些东西作为 header() 来将用户重定向到显示不正确登录屏幕的页面。
Or you could include the page, like you do in your example. Something like this:
或者您可以包含页面,就像您在示例中所做的那样。像这样的东西:
<?php
require('config.php');
// If the values are posted, insert them into the database.
if (isset($_POST['username']) && isset($_POST['password'])){
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$cpassword = $_POST['confirmpassword'];
$slquery = "SELECT 1 FROM register WHERE email = '$email'";
$selectresult = mysql_query($slquery);
if(mysql_num_rows($selectresult)>0)
{
$msg = 'email already exists';
}
elseif($password != $cpassword){
$msg = "passwords doesn't match";
}
else{
$query = "INSERT INTO `register` (username, password,confirmpassword, email) VALUES ('$username', '$password', '$cpassword', '$email')";
$result = mysql_query($query);
if($result){
$msg = "User Created Successfully.";
}
}
}
?>
replace your first code block with this.
用这个替换你的第一个代码块。
回答by Ataboy Josef
First, Welcome to PHP!
首先,欢迎使用 PHP!
Let me tell you one thing, it is not a good practice to write source codes all-together in the same way as they are executed. Use functions which has many advantages. As you are new in PHP and you are just running a simple requirement, here I have the best solution for you.
让我告诉你一件事,以与执行相同的方式编写源代码并不是一个好习惯。使用具有许多优点的函数s。由于您是 PHP 新手并且您只是在运行一个简单的需求,因此我为您提供了最佳解决方案。
Now please note:
现在请注意:
- I have used three user defined functions.
- Replaced your
$msg
and$errormes
withecho
(it will print the message on top of your HTML content). Also it is good to keep your PHP and HTML separately(not mixing together unless you really want it).
- 我使用了三个用户定义的函数。
- 将您的
$msg
和替换$errormes
为echo
(它将在您的 HTML 内容顶部打印消息)。将 PHP 和 HTML 分开保存也是很好的(除非你真的想要,否则不要混合在一起)。
Here is the full code. Try this:
这是完整的代码。尝试这个:
<?php
require_once('config.php');
// function for email validation
function is_valid_email($mail)
{
if (empty($mail)) {
echo "Email is required.";
return false;
} else {
$email = test_input($mail);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Invalid email format.";
return false;
}
// now check if the mail is already registered
$slquery = "SELECT 1 FROM register WHERE email = '$email'";
$selectresult = mysql_query($slquery);
if(mysql_num_rows($selectresult)>0) {
echo 'This email already exists.';
return false;
}
// now returns the true- means you can proceed with this mail
return true;
}
// function for password verification
function is_valid_passwords($pass,$confirmpass)
{
// Your validation code.
if (empty($pass)) {
echo "Password is required.";
return false;
}
else if ($pass != $confirmpass) {
// error matching passwords
echo 'Your passwords do not match. Please type carefully.';
return false;
}
// passwords match
return true;
}
// function for creating user
function create_user($username, $password, $cpassword, $email)
{
$query = "INSERT INTO `register` (username, password, confirmpassword, email) VALUES ('$username', '$password', '$cpassword', '$email')";
$result = mysql_query($query);
if($result){
return true; // Success
}else{
return false; // Error somewhere
}
}
// Code execution starts here when submit
if (isset($_POST['username']) && isset($_POST['password'])){
// Reading form values
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$cpassword = $_POST['confirmpassword'];
if (is_valid_email($email) && is_valid_passwords($password,$cpassword))
{
if (create_user($username, $password, $cpassword, $email)) {
echo 'New User Registered Successfully.';
}else{
echo 'Error Registering User!';
}
}
// You don't need to write another 'else' since this is the end of PHP code
?>
<!-- Here you go with the HTML -->
<!DOCTYPE html>
<html>
<head>
<title>Login screen</title>
<style type="text/css">
.register-form{
width: 500px;
margin: 0 auto;
text-align: center;
padding: 10px;
padding: 10px;
background : #c4c4c4;
border-radius: 10px;
-webkit-border-radius:10px;
-moz-border-radius:10px;
}
.register-form form input{padding: 5px;}
.register-form .btn{
background: #726E6E;
padding: 7px;
border-radius: 5px;
text-decoration: none;
width: 50px;
display: inline-block;
color: #FFF;
}
.register-form .register{
border: 0;
width: 60px;
padding: 8px;
}
</style>
</head>
<body>
<div class="register-form">
<h1>Register</h1>
<form action="" method="POST">
<p><label>User Name: </label>
<input type="text" id="username" name="username" placeholder="Username">
</p>
<p><label>E-Mail :</label>
<input id="email" name="email" type="email">
</p>
<p><label>Password :</label>
<input id="password" name="password" type="password">
</p>
<p><label>confirm Password :</label>
<input id="confirmpassword" name="confirmpassword" type="password">
</p>
<input type="submit" name="submit" value="register" class="btn register">
<a class="btn" href="login.php">Login</a>
</form>
</div>
</body>
</html>
Now save this code it in .php file format and run. You will see the way you want it.
现在将此代码保存为 .php 文件格式并运行。你会看到你想要的方式。
Try experiments in PHP. Just google for more. And only ask-if you are stuck for a long time.
尝试在 PHP 中进行实验。只是谷歌更多。并且只询问您是否被卡住了很长时间。
Thank you.
谢谢你。