在PHP中验证密码和确认密码

5uzkadbs  于 2023-01-29  发布在  PHP
关注(0)|答案(2)|浏览(170)

我正在尝试一个注册表,其中密码和确认密码应该相互匹配。如果它不匹配,它应该显示错误消息。以及如何验证密码超过6个字符?我该怎么做?如何验证密码和确认密码匹配,并显示错误消息密码是错误的?
下面是代码

<?php
$username = "root";
$password = "";
$hostname = "localhost"; 
$db = "reg";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
mysql_select_db($db,$dbhandle) or die('cannot select db');

if(isset($_POST['submit'])) {

     $name= $_POST['name'];

     $username= $_POST['username'];
    $password= $_POST['password'];
     $cpassword= $_POST['cpassword'];

     if($name==''){
     echo "<script>alert('Please enter Name')
     </script>";
    exit();
    }
    if($username==''){
     echo "<script>alert('Please enter Username')
     </script>";
    exit();
    }
    if($password=='' && $password<6){
     echo "<script>alert('Please enter  Password')</script>";

    exit();
    }

    if($cpassword==''){
     echo "<script>alert('Please enter Confirm Password')
     </script>";

    }


    $check_name="select * from registration where username='$username'";
    $run=mysql_query($check_name);

     if(mysql_num_rows($run)>0){
    echo "<script>alert('Username $username already exits in our database. Please try with Another!')</script>";

    }

    elseif($password != $cpassword){
       echo "<script>alert('passwords doesn't match')</script>";
    }
    else{
          $query = "INSERT INTO `registration` (name,username, password,cpassword) VALUES ('$name','$username', '$password', '$cpassword')";
          $run1=mysql_query($query);

    if($run1){
    echo "<script>window.open('register.html','_self')</script>";
    }
    }
    }
    ?>

HTML代码

<form name="register"  action="register.php" method="POST" id="register" style="font-family:ff-meta-web,Arial,sans-serif;text-align:justify;line-height:25px; font-size:12px;">
    <label style="margin-right:30px;"> <b> Name : </b></label>

    <input type="text" name="name" id="name" style="width:200px;" /> 
    <br /> <br />

    <label> <b> User Name : </b></label>
    <input type="text" name="username" id="username" style="width:200px;" /> <br/><br/>
    <label> <b> Password : </b></label> 
    <input type="password" name="password" id="password" style="width:200px;" />
    <br /> <br /> 
    <label> <b>Confirm Password : </b></label> 
    <input type="password" name="cpassword" id="cpassword" style="width:200px;" />
    <br /> <br /> 
    <input type="submit" name="submit" value="Submit" style="background-color:#005797; color:#fff; border:0px; padding:5px 10px;" /> 

    </form>
tvokkenx

tvokkenx1#

变化

if($password=='' && $password<6){

if($password=='' && strlen($password)<6){

您应该验证您的所有输入在客户端也。

ldioqlga

ldioqlga2#

如果($password==''&& strlen($password)〈6){ echo“您的错误消息”;}elseif($密码!=$c密码){echo“错误消息”;}您可以显示如下错误消息

相关问题