0
不幸的是,在我的登錄表單中,沒有輸入任何數據,仍然出現登錄成功。我是否犯過任何特定錯誤,還是應該添加更多代碼?非常感謝您的幫助零數據仍然登錄成功
PHP
<table style= width:300px; border="0"; align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
CSS - checklogin.php
<?php
$host="localhost";
$username="root";
$password="";
$db_name="Hockey Club";
$tbl_name="members";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$myusername=$_POST['username'];
$mypassword=$_POST['password'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM members WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1){
$_SESSION['username'] = $myusername];
$_SESSION['password'] = $mypassword];
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
和 - login_success.php
<?php
session_start();
if (!isset($_SESSION['myusername'])) {
header("location:Index.php");
}
?>
<html>
<body>
Login Successful
</body>
</html>
確保您取消設置'$ _SESSION [「名爲myUsername」]'試圖登錄的用戶,否則您的測試可能會受到影響之前由舊數據。 – Scutterman
請不要使用mysql_ *函數,它已被棄用(請參閱[*紅盒子*](http://php.net/manual/en/function.mysql-query.php)),並且容易受到sql注入的影響。使用[* PDO *](http://php.net/manual/en/book.pdo.php)或[* MySQLi *](http://php.net/manual/en/book.mysqli.php) – alfasin
'$ myusername = $ _ POST ['username'];'但是在表單中沒有'name''用戶名的元素。 –