2016-04-28 13 views
1

私は最初のログインシステムを作ろうとしています。何らかの理由で私のデータベースからパスワードを取得しようとすると、値が与えられません。私は何が間違っているのか分かりません。エラーは$ sqlと$ db_passwordの間です。最初のログインシステムを作る。 SQLカラムの選択は機能しませんか?

@LFlare私はDESCユーザーのことが何であるかわかりません。ここにはテーブルの写真があります。私はあなたがそれを望んでいたかどうかはわかりませんでした。 http://i.imgur.com/WkZV7IZ.png

ありがとうございます!

+0

あなたが得た '$のrow'の値は何ですか? –

+0

$ rowの値は何もありません。 – Matt

+0

このクエリで試してみてください$ sql = "SELECT * FROM users where username = '"。$ username。 "' LIMIT 1 '; –

答えて

1

あなたのPHP

<?php 
    session_start(); 

    if (isset($_POST['username']) && isset($_POST['password'])) { 
     include_once("db.php"); 
     $username = mysqli_real_escape_string($sqlcon, $_POST['username']); 
     $password = mysqli_real_escape_string($sqlcon, $_POST['password']); 
     // If you want to make sure username is alphanumeric, you can do 
     // $username = preg_replace('/[^a-zA-Z0-9]/', '', mysqli_real_escape_string($sqlcon, $_POST['username'])); 

     // Do not use these, mysqli_real_escape_string is enough to prevent injection attacks. Furthermore, you may be compromising user security by remove special characters in passwords. 
     // $username = strip_tags($_POST['username']); 
     // $password = strip_tags($_POST['password']); 
     // $username = stripslashes($username); 
     // $password = stripslashes($password); 

     // $password = md5($password); This is very susceptibile to rainbow table attacks, do something like a loop 
     for ($i = 0; $i < 1000; $i ++) { 
      $password = md5($password . $username); // Looping the md5 a thousand times and salting it with the username is good practice too.U 
     } 

     $userQuery = "SELECT * FROM users WHERE username = '" . $username . "' LIMIT 1"; 
     $user = mysqli_query($sqlcon, $userQuery); 

     if (mysqli_num_rows($user) > 0) { // If user exists, 
      $user = mysqli_fetch_assoc($user); // mysqli_fetch_arrays put values into $user[0], $user[1], etc. 
      $id = $user['id']; 
      $databasepass = $user['password']; 

      if ($password === $databasepass) { 
       $_SESSION['username'] = $username; 
       $_SESSION['id'] = $id; 
       header("Location: index.php"); 
      } else { 
       echo "Password is incorrect"; 
      } 
     } else { 
      echo "Username does not exist"; 
     } 
    } else { 
     echo "Username or Password not filled in"; 
    } 
    echo $password; 
?> 

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Login</title> 
    </head> 
    <body> 
     <h1>Login</h1> 
     <form action="login.php" method="post" enctype="multipart/form-data"> 
      <input placeholder="Username" name="username" type="text" autofocus> 
      <input placeholder="Password" name="password" type="password"> 
      <input name="login" type="submit" value="Login"> 
     </form> 
    </body> 
</html> 

あなたをdb.php

<?php 
$host = "localhost"; 
$user = "root"; 
$pass = ""; 
$database = "users"; 

$sqlcon = mysqli_connect($host, $user, $pass, $database); 

if (mysqli_connect_errno()) { 
    die ("MySQL Database Connection Error"); 
} 
?> 
+0

これは、Usernameがそのコードに存在しないことを伝えています。 – Matt

+0

@Sphiinx、申し訳ありませんが、私は偶然、あまりにも多くの人に匹敵しました。それでも問題が解決しない場合は、サンプルとしてデータベース構造とサンプル行を使用できますか? –

+0

@LFlareパスワード比較の部分は弱いタイピングのバイパスに対して脆弱です。その悪役の上に三つの等しいを投げる。 https://pen-testing.sans.org/blog/2014/12/18/php-weak-typing-woes-with-some-pontification-about-code-and-pen-testingセキュリティに関するご意見から、あなたはそれを感謝するだろう;) – Tim

0

mysqli_queryには$ dbと$ sqlがあります。

$query = mysqli_query($sql, $db);

http://php.net/manual/en/function.mysql-query.php

また、MD5を使用しないようしてみてください、とPHPのpassword_hash、http://php.net/manual/en/function.password-hash.phpを使用しています。

現在、DBが悪用されると、レインボーテーブルの攻撃に対して脆弱です。

+0

動作しませんでした。 =/ – Matt

+0

真剣に今ですか? $ sqlはクエリ、$ dbは接続です。また、あなたがリンクしたリンクは、mysql_queryは推奨されません。さらに、md5を塩で複数回実行することで、レインボーテーブルの攻撃を回避できます。 –

+0

@LFlare Ahh !!あなたはまったく正しいです、私はmysq_queryの代わりにmysql_queryのリンクを持っています。減価償却に関する警告は今や意味をなさない。 – Tim

関連する問題