2016-05-09 9 views
-1

私はphpを使用してログインページを作成しようとしていますが、ログインボタンを押した後にユーザー名とパスワードを確認するようにします。しかし、ログインボタンが押されていなくてもコードは実行されます。私は数十のコードをオンラインで検索し、ほとんどすべてがこの方法で実装されているので、ここで何が間違っているのか理解できません。ここに私のコードです:php issetが送信ボタンと連動していません

<HTML> 
    <HEAD> 
     <TITLE>Welcome to FoodOrder - Log In or Sign Up</TITLE> 

    </HEAD> 
    <BODY background = "foodOrder.png"> 

     <?php 
     session_start(); 
     $con = mysqli_connect("xxxx", "xxxx", "xxxx", "xxxx"); 

     if(mysqli_connect_errno()){ 
      echo "Failed to connect, please check your username and password: ".mysqli_connect_error(); 
     } 

     if(isset($_SESSION['user'])!= ""){ 
      header("Location: home.php"); 
     } 

     if(isset($_POST["login_button"])){ 
      $username = mysqli_real_escape_string($_POST['username']); 
      $password = mysqli_real_escape_string($_POST['password']); 
      $sql = "SELECT password from user where username = '$username'"; 
      $res = mysqli_query($con,$sql); 
      $row = mysqli_fetch_array($res); 
      if($row['password'] == md5($password)){ 
       $_SESSION['user'] = $username; 
       header("Location : home.php"); 
      } else { 
       ?> <script>alert('wrong username or password');</script> <?php 
      } 
     } 


     ?> 

     <center> 
      <div id ="login form"> 
       <form method = "POST"> 
        <table align = "center" width = "25%" border = "0"> 
         <tr> 
          <td><input type = "text" name = "username" placeholder = "your_username_here" required></td> 
         </tr> 
         <tr> 
          <td><input type = "text" name = "password" placeholder = "your_password_here"></td> 
         </tr> 
         <tr> 
          <td><input type = "submit" name = "login_button" value = "Log In" /></td> 
         </tr> 
         <tr> 
          <td><a href = "register.php">Sign Up</a></td> 
         </tr> 
        </table> 
       </form> 
      </div> 
     </center> 

    </BODY> 



</HTML> 
+3

でこれ

if(isset($_SESSION['user'])!= ""){ header("Location: home.php"); } 

を交換してみてください?あなたはまた、dbに行かない入力をエスケープしないでください。引用符付きのパスワードは、このように正しくハッシュされます。また、パスワードとして 'md5'を使うべきではありません。 – chris85

+2

が編集されていても、資格情報であればアカウントをリセットします。資格情報はすでに改ざんされていて、改訂版にはまだ存在している可能性があります。 – chris85

+0

@ chris85 dbに正しく行きますが、間違ってセッションに保存されます。 – Rudie

答えて

0

md5を一度削除してコードをチェックしてください。 $_SESSION VARIABLEが正しく動作していません。

も押されていない `in`ログインした場合、実行するコード..あなたが本当の資格情報ではありません、私はそれらを願っています。この

if(!$_SESSION['user']){ 
      header("Location: home.php"); 
     } 
+0

なぜあなたのイムネット内に感嘆符がありますか? 'isset(!$ _ SESSION ['user'])' –

+0

$ _SESSION変数に何かがあるかどうかをチェックします。 –

+0

間違いは間違いです。 ** E_COMPILE_ERROR:type64 - 式の結果にisset()を使用することはできません(代わりに "null!== expression"を使用できます)** –

関連する問題