2017-02-06 4 views
0
<?php 
session_start(); 
$username = mysql_real_escape_string($_POST['username']); 
$password = mysql_real_escape_string($_POST['password']); 
$bool = true; 

mysql_connect("localhost", "root", "") or die (mysql_error()); //Connect to server 
mysql_select_db("first_db") or die ("Cannot connect to database"); //Connect to database 
$query = mysql_query("Select * from users WHERE username='$username'"); // Query the users table 
$exists = mysql_num_rows($query); //Checks if username exists 
$table_users = "": 
$table_password = ""; 
if($exists > 0) //IF there are no returning rows or no existing username 
{ 
    while($row = mysql_fetch_assoc($query)) // display all rows from query 
    { 
     $table_users = $row['username']; // the first username row is passed on to $table_users, and so on until the query is finished 
     $table_password = $row['password']; // the first password row is passed on to $table_password, and so on until the query is finished 
    } 
    if(($username == $table_users) && ($password == $table_password))// checks if there are any matching fields 
    { 
     if($password == $table_password) 
     { 
     $_SESSION['user'] = $username; //set the username in a session. This serves as a global variable 
     header("location: home.php"); // redirects the user to the authenticated home page 
     } 
    } 
    else 
    { 
    Print '<script>alert("Incorrect Password!");</script>'; // Prompts the user 
    Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php 
    } 
} 
else 
{ 
    Print '<script>alert("Incorrect username!");</script>'; // Prompts the user 
    Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php 
} 
?> 

PHPのログインページが

<head> 

    <title>My first PHP website</title> 

</head> 

<body> 

    <h2>Login Page</h2> 

    <a href="index.php">Click here to go back</a><br/><br/> 

    <form action="checklogin.php" method="post"> 

     Enter Username: <input type="text" name="username" required="required"/> <br/> 

     Enter Password: <input type="password" name="password" required="required" /> <br/> 

     <input type="submit" value="Login"/> 

    </form> 

</body> 

これはログインを確認し、index.phpページにあなたを指示する必要があり、私が持っているコードですが、それは何もしません。 。私のユーザ名とパスワードはすべて私のdbに保存されています 最後のコードはログインページの作成に使用されたものですが、何が起こっているのか分かりません。すべてのユーザーはdbに正しく格納されますが、私がそれらにログインすると、ページはただちに更新され、何もしません。

+0

'$ password == $ table_password'を2回確認する必要はありません: ) 'if($ password == $ table_password)'ブロックに 'print'文を追加して実行されていることを確認してください。 – Timmy

+0

個人的には、安全でないコードなので、時間を無駄にしていると思います。これが教育目的のためのものであれば、なぜそれが機能していないのかを知ることはあなたの仕事です。コードが失敗したかどうかをチェックするためのメソッドがあります。 –

+0

大学のプロジェクトの在庫データベースにリンクされたウェブサイトを作成しようとしています –

答えて

0

printステートメントの使用に関する私のコメントは、 SQLクエリを経由して見つかりませユーザ名が一致しない場合

if(($username == $table_users) && ($password == $table_password)) 

その行が実行されることはありません飽きないだろうので:私は削除した

<?php 
    session_start(); 
    $username = mysql_real_escape_string($_POST['username']); 
    $password = mysql_real_escape_string($_POST['password']); 
    $bool = true; 

    mysql_connect("localhost", "root", "") or die (mysql_error()); //Connect to server 
    mysql_select_db("first_db") or die ("Cannot connect to database"); //Connect to database 
    $query = mysql_query("Select * from users WHERE username='$username'"); // Query the users table 
    $exists = mysql_num_rows($query); //Checks if username exists 
    $table_users = "": 
    $table_password = ""; 

    if($exists > 0) //IF there are no returning rows or no existing username 
    { 
     while($row = mysql_fetch_assoc($query)) // display all rows from query 
     { 
      $table_users = $row['username']; // the first username row is passed on to $table_users, and so on until the query is finished 
      $table_password = $row['password']; // the first password row is passed on to $table_password, and so on until the query is finished 
     } 

     if($password == $table_password) //check password match 
     { 
      Print 'password matched'; 
      $_SESSION['user'] = $username; //set the username in a session. This serves as a global variable 
      header("location: home.php"); // redirects the user to the authenticated home page 
     } 
     else 
     { 
      Print '<script>alert("Incorrect Password!");</script>'; // Prompts the user 
      Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php 
     } 
    } 
    else //user doesnt exist and/or no rows returned 
    { 
     Print '<script>alert("Incorrect username!");</script>'; // Prompts the user 
     Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php 
    } 
?> 

注:ユーザーのパスワードは二回、以下の次の変更を参照してくださいとが一致する場合、パスワードは次のif文で比較されます。

コードブロックif($password == $table_password) //check password matchPrintステートメントを追加しました。このコードセクションが実際に実行されているかどうかを確認できます。そうであれば、header()はリダイレクトされません。それ以外の場所に問題がある場合はリダイレクトされません。

最後に、コメントに記載されているように、これはSQL注入に対して脆弱な危険なコードです。 PDOやMySQLiでの準備文の使用を検討する必要があります。現在のソリューションは大学には適していますが、現実世界ではもう一つの脆弱性です。そのメモでは、ユーザ名やパスワードが間違っているかどうかを明らかにすることもセキュリティ上のリスクです。代わりに「無効なログイン」を選択するだけです:)

+0

アドバイスをいただきありがとうございます。何らかの理由で私がloginをクリックしても何もしないので、どこに問題があるのか​​わかりません。 –

+0

@ JamesMorris上記のコードを試しましたか?私は 'else'節を削除するのを忘れてしまったので、もう一度試してみました。出力はまったく得られますか?フォームは正しいページに送信されますか?あなたがphp.iniの設定ファイルでエラー報告を無効にしていて、編集前にコードを試しても何も出力されなかったと思われます。 php.iniファイルを直接編集できない場合は、スクリプトの先頭に 'error_reporting(E_ALL);'を追加してエラーを出力する必要があります。デバッグが可能になります。 – Timmy

+0

私はそれが働いて、それは右のページに送信されます。私はどこに間違っていたのか理解していますので、助けてくれてありがとう! –

関連する問題