1

次のようなログインフォームがあります。

<?php include "base.php"; ?> 

<div id="loginpart">  
<?php  
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))  
{  
 ?>   
 You are logged in as <b><?=$_SESSION['Username'];?> </b>. | <a     href="logout.php">LogOut</a>
 <?php  
}  
elseif(!empty($_POST['username']) && !empty($_POST['password']))  
{  
$username = mysql_real_escape_string($_POST['username']);  
$password = md5(mysql_real_escape_string($_POST['password']));  
$checklogin = mysql_query("SELECT * FROM user WHERE username = '".$username."' AND user_password = '".$password."'");  
if($checklogin)  
{  
    $row = mysql_fetch_row($checklogin);
    $_SESSION['Username'] = $username; 
    $_SESSION['LoggedIn'] = 1;  
    echo "<p>Success: We are now redirecting you to the member area.</p>";  
    echo "<meta http-equiv='refresh' content='2;index.php' />";
    //header('Location:index.php');  
}  
else  
{  
    echo "<p>Error: Sorry, your account could not be found. Please <a     href=\"index.php\">click here to try again</a>.</p>";  
}  
}  
else  
{  
?>    
<form method="post" action="index.php" name="loginform" id="loginform">   
    Username:<input type="text" name="username" id="username" />
    Password:<input type="password" name="password" id="password" />
    <input type="submit" name="login" id="login" value="Login" />  
| <a href="register.php">Register</a>  
</form>  
<?php  
}  
?>  
</div>   

ユーザーがログインしているかどうかを最初に確認します。いいえの場合は、ログインしてください。含まれている base.php 呼び出しデータベース:

<?php  
session_start();  
$dbhost = "localhost"; // this will ususally be 'localhost', but can sometimes differ  
$dbname = "login"; // the name of the database that you are going to use for this  project  
$dbuser = "root"; // the username that you created, or were given, to access your     database  
$dbpass = ""; // the password that you created, or were given, to access your database  
mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());  
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());  
?>  

エラーは、ユーザー名とパスワードをチェックしている 2 番目のブラケットにあります。

データベース テーブルは次のとおりです。

create table user(
user_ID smallint unsigned auto_increment,
username varchar(30),
user_password varchar(16),
user_fname VARCHAR(30) NOT NULL,
user_lname VARCHAR(30) NOT NULL,
user_contact varchar(14),
user_email varchar(30),
user_street varchar(20),
user_city varchar(20),
constraint pk_user primary key (user_ID)
) engine innodb;

問題は、間違ったユーザー名またはパスワードを入力した場合でもです。それは私をログインさせます。誰かが私が間違っているところを助けてください。

4

2 に答える 2

3

行が返されるかどうかはチェックしていません。

if($checklogin)結果セットが空の場合でも(パスワードが間違っていたか、ユーザーが見つからなかったため)、常にtrueと評価されます。

このif句を、空でない結果セットをチェックする句に置き換える必要があります。例:

if(mysql_num_rows($checklogin) > 0)  

$checklogin_result補足として:たとえば、$checklogin_rowプロジェクトがいつか本当に大きくなった場合に見失ってしまわないように、より表現力豊かな変数名を選択することもできます;)

于 2012-12-25T23:09:01.193 に答える
3

おそらく構文エラーがなく、クエリが正常に実行されたため、成功が返されます。しかし、それは行が返されたという意味ではありません。空の行をチェックする必要があります

if(mysql_num_rows($checklogin) > 0)  
{  
    $row = mysql_fetch_row($checklogin);
    $_SESSION['Username'] = $username; 
    $_SESSION['LoggedIn'] = 1;  
    echo "<p>Success: We are now redirecting you to the member area.</p>";  
    echo "<meta http-equiv='refresh' content='2;index.php' />";
    //header('Location:index.php');  
}  
else  
{  
    echo "<p>Error: Sorry, your account could not be found. Please <a     href=\"index.php\">click here to try again</a>.</p>";  
}  
于 2012-12-25T23:09:38.537 に答える