2013-01-11 42 views
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>&nbsp;</td> 
<td>&nbsp;</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> 
+1

確保您取消設置'$ _SESSION [「名爲myUsername」]'試圖登錄的用戶,否則您的測試可能會受到影響之前由舊數據。 – Scutterman

+1

請不要使用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

+0

'$ myusername = $ _ POST ['username'];'但是在表單中沒有'name''用戶名的元素。 –

回答

0

你不應該將密碼存儲在其數據庫中的原始表單。如果你是編程新手,那裏有很多很棒的用戶認證腳本。請在以這種方式創建生產網站之前對其進行研究。

這是一個良好的開始地點。

Hashing a Password

而且不要使用MySQL。使用庫MySQLi或PDO

下面是一個谷歌的鏈接,讓你去:User authentication