注册时双重密码

时间:2013-12-15 17:08:08

标签: php database login passwords registration

我正在使用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>

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

1 个答案:

答案 0 :(得分:1)

首先,要使您的密码字段看起来像密码,您需要将其类型更改为密码而不是文本,因此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.";

  }
}

?>