2016-05-14 22 views
0

我遇到了以下问题。这是一个简单的登录和注册表面。 注册:登录界面没有看到新的JSON数据

<form method="post"> 
       Username : 
       <input type="text" name="username" placeholder="Username"> 
       <br> 
       E-mail : 
       <input type="text" name="email" placeholder="E-mail "> 
       <br> 
       Password : 
       <input type="password" name="password" placeholder="Password"> 
       <br> 


       <?php 
       if (isset($_POST['email']) && isset($_POST['password']) && isset($_POST['username'])) { 
        $allDatas = json_decode(file_get_contents('data.json'), true); 
        $username = $_POST['username']; 
        $password = $_POST['password']; 
        $email = $_POST['email']; 
        $foundUser = false; 
        $valid = false; 

        //check the values 
        if (empty($_POST["email"])) { 
         ?> <font size="1px"><?php echo "Email is required !"; ?> </font><?php 
        } else { 
         if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
          ?> <font size="1px"><?php echo "Invalid email format!"; ?> </font><?php 
         } 
        } 
        if (empty($_POST["password"])) { 
         ?> <br><font size="1px"><?php echo "Password is required !"; ?> </font><?php 
        } 
        if (empty($_POST["username"])) { 
         ?> <br><font size="1px"><?php echo "Username is required !"; ?> </font><?php 
        } 



        //is it exists 
        foreach ($allDatas as $value) { 
         if ($value[0] == $username) { 
          ?> <br><font size="1px"><?php echo "Username exists!"; ?> </font><?php 
          $foundUser = true; 
          break; 
         } elseif ($value[2] == $email) { 
          ?> <br><font size="1px"><?php echo "E-Mail registered!";?> </font><br><?php 
          $foundUser = true; 
          break; 
         } 
        } 
        //add to database 
        if(!empty($_POST["password"]) && !empty($_POST["username"])&& !empty($_POST["email"]) && filter_var($email, FILTER_VALIDATE_EMAIL)){$valid = true;} 
        if (!$foundUser && $valid) { 
         $allDatas[] = array($username, $email, $password); 
         file_put_contents('data.json', json_encode($allDatas)); 
         echo "Done"; 
        } 
        unset($allDatas); 
       } 
       ?> 
       <br> 
       <input type="submit" value="Registration"> 
      </form> 
      <br> 
      <form action="index.php"> 
       <input type="submit" name="back" value="Back"> 
      </form> 

和登录:

<form method="post"> 
        Email: 
        <input type="text" name="email" placeholder="Email"> 
        <br> 
        Password: 
        <input type="password" name="password" placeholder="Password"> 
        <br> 

        <?php 
        $allDatas = json_decode(file_get_contents('data.json'), true); 
        $foundUser = false; 
        $action = "login.php"; 

        if (isset($_POST['email']) && isset($_POST['password'])) { 
         $password = $_POST['password']; 
         $email = $_POST["email"]; 
         if (empty($_POST["email"])) { 
          ?> <font size="1px"><?php echo "Email is required !"; ?> </font><?php 
         } else { 
          if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
           ?> <font size="1px"><?php echo "Invalid email format!"; ?> </font><?php 
          } 
         } 
         if (empty($_POST["password"])) { 
          ?> <br><font size="1px"><?php echo "Password is required !"; ?> </font><?php 
         } 
         foreach ($allDatas as $value) { 
          if ($value[2] == $email && $value[1] == $password) { 
           $foundUser = true; 
           $username = $value[0]; 
           ?> <font size="1px"><?php echo "Success! Welcome ", $username, " !"; ?> </font><?php 
           $action = "reddragon.html"; 
          } 
         } 
        } 
        if(!$foundUser) 
        { 
         ?><br> <font size="1px"><?php echo "Please type your datas!"; ?> </font><?php 
        } 
        ?> 

        <br> 
         <input type="submit" value="Log in"> 
        <br> 
       </form> 
       <font color="red" size="1px">Before you play, LOG IN!</font> 
       <form action="<?php echo "$action";?>"> 
        <input type="submit" name="play" value="Play"> 
       </form> 
       <form action="index.php"> 
        <input type="submit" name="back" value="Back"> 
       </form> 

我的问题是,当我添加一个新成员(用户名,PSW,邮件),它会被添加到JSON数据库,但登录表面看不到它!旧版本没问题,但是我通过注册创建的新版本并未被登录接受。

什么是解决方案?

回答

0

在线路#倒装$email$passwordregister.php 54如下:

$allDatas[] = array($username, $password, $email);