<?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
}
?>
<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に正しく格納されますが、私がそれらにログインすると、ページはただちに更新され、何もしません。
'$ password == $ table_password'を2回確認する必要はありません: ) 'if($ password == $ table_password)'ブロックに 'print'文を追加して実行されていることを確認してください。 – Timmy
個人的には、安全でないコードなので、時間を無駄にしていると思います。これが教育目的のためのものであれば、なぜそれが機能していないのかを知ることはあなたの仕事です。コードが失敗したかどうかをチェックするためのメソッドがあります。 –
大学のプロジェクトの在庫データベースにリンクされたウェブサイトを作成しようとしています –