注册时使用双重密码


Double password during register

我正在用PHP制作一个智力竞赛游戏的注册和登录页面。我目前已经涵盖了基本知识,但我希望通过类似密码检查(双密码)的方式进行注册,并且当用户输入密码时,我希望它被涵盖,此时密码显示用户正在输入什么,有人能帮我吗?下面是我正在使用的代码:

登录

<?PHP
if (isset($_POST['submit']))
{
  $username = $_POST['username'];
  $password = $_POST['password'];
  $file = file_get_contents("data.txt");
  if(!strstr($file, "$username||$password"))
  {
    print '<script> alert ("Sorry! You have entered a Invalid Username or Password.");   window.location="index.php"; </script>';
  }
  if(empty($username))
  {
    print '<script> alert ("Sorry! You have entered a Invalid Username or Password."); window.location="index.php"; </script>';
  }
  else
  {
    header("Location: /home.php");
  }
}
?>
<!doctype html>
<html>
<head>
  <title>Index</title>
</head>
<body>
<div id="container" style="width:500px; height:500px; border: 2px solid black;  margin:auto">
  <?php include "header.php"; ?>
  <div id="content" style="background-color:#EEEEEE; width:500px; height:400px; float: left">
    <br>
    <form  align="center" method="post" action="index.php" >
      Username:
      <input type="text" name="username" />
      <br/>
      <br/>
      Password:
      <input type="text" name="password" />
      <br/>
      <br/>
      <input type="submit" value="Login" name="submit"/>
      <input type="button" value="Register" onclick="document.location='registration.php'" />
    </form>
  </div>
  <?php include "footer.php"; ?>
</div>
</body>
</html>

并注册

<div align="center">
  <?PHP
  if (isset($_POST['submit']))
  {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $file = file_get_contents("data.txt");
    $string = "$username||$password";
    if(!strstr($file, "$string"))
    {
      $myFile = "data.txt";
      $fh = fopen($myFile, 'a') or die("can't open file");
      $stringData = "$username||$password'n";
      fwrite($fh, $stringData);
      print '<script> alert ("Registration Complete"); window.location="index.php"; </script>';
      fclose($fh);
    }
    else
    {
      echo "Sorry the username: <b>$username</b> is already registered. Please use diferent  username.";
    }
  }
  ?>
</div>
<!doctype html>
<html>
<head>
  <title>Registration</title>
</head>
<body>
<div id="container" style="width:500px; height:500px; border: 2px solid black; margin:auto">
  <?php include "header.php"; ?>
  <div id="content" style="background-color:#EEEEEE; width:500px; height:400px; float: left">
    <br>
    <form align="center" method="post" action="registration.php" >
      Username:
      <input type="text" name="username" />
      <br/>
      <br/>
      Password:
      <input type="text" name="password" />
      <br/>
      <br/>
      <input type="submit" value="Register" name="submit" />
    </form>
  </div>
  <?php include "footer.php"; ?>
</div>
</body>
</html>

我正在使用平面文件数据库来存储信息。

首先,要使您的密码字段看起来像密码,您需要将其类型更改为密码而不是文本,因此type="text"变成type="password",您还需要添加另一个密码字段并更改其名称,以便进行确认。

登录

<form  align="center" method="post" action="index.php" >
  Username:
  <input type="text" name="username" />
  <br/>
  <br/>
  Password:
  <input type="password" name="password" />
  <br/>
  <br/>
  Password (Confirmation):
  <input type="password" name="password2" />
  <br/>
  <br/>
  <input type="submit" value="Login" name="submit"/>
  <input type="button" value="Register" onclick="document.location='registration.php'" />
</form>

然后在注册时,您需要获取请求中发送的值,并将它们相互比较(密码和密码2),如果它们相等,则让用户继续,如果它们没有显示错误消息

<?PHP
if (isset($_POST['submit']))
{
  $username = $_POST['username'];
  $password = $_POST['password'];
  $password2 = $_POST['password2'];
  if($password != $password2) die('Passwords do not match');
  $file = file_get_contents("data.txt");
  $string = "$username||$password";
  if(!strstr($file, "$string"))
  {
    $myFile = "data.txt";
    $fh = fopen($myFile, 'a') or die("can't open file");
    $stringData = "$username||$password'n";
    fwrite($fh, $stringData);
    print '<script> alert ("Registration Complete"); window.location="index.php";    </script>';
    fclose($fh);
  }
  else
  {
    echo "Sorry the username: <b>$username</b> is already registered. Please use diferent  username.";
  }
}
?>