【问题标题】:How to validate email id and password & confirm password in php如何在php中验证电子邮件ID和密码并确认密码
【发布时间】:2014-11-30 18:22:57
【问题描述】:

我在 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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :</label>
            <input id="email" name="email" type="email">
            </p>
            <p><label>Password&nbsp;&nbsp; :</label>
            <input id="password" name="password" type="password">
            </p>
            <p><label>confirm Password &nbsp;&nbsp; :</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>

【问题讨论】:

    标签: php email


    【解决方案1】:

    die() 函数将有效地停止脚本的执行。这意味着在该行之后将不再运行代码。

    您可能想使用一些东西作为 header() 来将用户重定向到显示不正确登录屏幕的页面。

    或者您可以包含该页面,就像您在示例中所做的那样。像这样的:

    <?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.";
              }
        }
       }
    ?>
    

    用这个替换你的第一个代码块。

    【讨论】:

    • 感谢您的帮助,先生,我明白了。同时在php中如何检查confirmpassword和password字段是一样的。
    • 如果此答案对您有帮助,您应该将其标记为正确,并关闭问题。
    • 请注意:如果您在单独的 php 页面上处理登录,则 $msg 应该是 $_SESSION['msg'] var 以转移到您要重定向到的页面。跨度>
    • @GaryHayes 你是对的,但由于示例中没有 OP,因此我也没有将其包含在此解决方案中
    • 非常感谢先生,您给了我信心。感谢您花时间陪伴我。
    【解决方案2】:

    首先,欢迎使用 PHP!

    让我告诉你一件事, 以与执行源代码相同的方式一起编写源代码并不是一个好习惯。使用 function 有很多优点。由于您是 PHP 新手,并且您只是在运行一个简单的需求,因此我在这里为您提供了最佳解决方案。

    现在请注意:

    1. 我使用了三个用户定义的函数。
    2. 将您的$msg$errormes 替换为echo(它将打印 HTML 内容顶部的消息)。此外,最好将 PHP 和 HTML 分开保存(除非你真的想要,否则不要混合在一起)。

    这是完整的代码。试试这个:

    <?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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :</label>
                    <input id="email" name="email" type="email">
                    </p>
                    <p><label>Password&nbsp;&nbsp; :</label>
                    <input id="password" name="password" type="password">
                    </p>
                    <p><label>confirm Password &nbsp;&nbsp; :</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>
    

    现在将此代码保存为 .php 文件格式并运行。你会看到你想要的样子。

    在 PHP 中尝试实验。只是谷歌更多。并且只问你是否被卡了很长时间。

    谢谢。

    【讨论】:

      猜你喜欢
      • 2011-11-22
      • 2015-05-18
      • 1970-01-01
      • 2015-04-03
      • 2023-02-23
      • 2013-09-18
      • 1970-01-01
      • 2015-11-14
      • 2014-06-01
      相关资源
      最近更新 更多