2014-03-12 80 views
0

遇到一些麻煩一個PHP登錄,我在網上找到 -PHP登錄麻煩

<?php 

$host="localhost"; // Host name 
$username="SOME_USERNAME"; // Mysql username 
$password="SOME_PASSWORD"; // Mysql password 
$db_name="b00556019"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse. 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// To protect MySQL injection (more detail about MySQL injection) 
$myusername = stripslashes($myusername); 
$mypassword = stripslashes($mypassword); 
$myusername = mysql_real_escape_string($myusername); 
$mypassword = mysql_real_escape_string($mypassword); 
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; 
$result=mysql_query($sql); 

// Mysql_num_row is counting table row 
$count=mysql_num_rows($result); 

// If result matched $myusername and $mypassword, table row must be 1 row 
    if($count==1){ 

// Register $myusername, $mypassword and redirect to file "login_success.php" 
session_register("myusername"); 
session_register("mypassword"); 
header("location:login_success.php"); 
} 
else { 
echo "Wrong Username or Password"; 
} 
?> 

的錯誤是對線16-17,並顯示如下 -

Notice: Undefined index: myusername in E:\home\students\2137\B00556019\public_html\workspace\login\checklogin.php on line 16 

Notice: Undefined index: mypassword in E:\home\students\2137\B00556019\public_html\workspace\login\checklogin.php on line 17 

錯誤的用戶名或密碼

這是2行不工作:

$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

下面是HTML

<!DOCTYPE HTML> 
<html> 
<head> 
<body> 
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> 
<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> 
</body> 
</html># 

任何人只要有任何意見或更好的教程示例將不勝感激:)

+0

您應該使用表單中的密碼字段'type =「password」' – Alesfatalis

+0

使用isset來檢查索引是否存在。一旦你這樣做,刪除你的代碼,不要回頭 –

+2

你有一個''標籤,但你忘了'

' –

回答

0

$_POST[]包含從HTML表單提交數據。如果用戶沒有發送表單,則$myusername字段將爲空,因爲在該數組中找不到索引(實際上是鍵)'myusername'。這就是爲什麼你會收到這些錯誤。

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

,以確保POST數據(從提交表單)包含鍵/索引,請使用isset()命令(或array_key_exists(),這兩本著作。)

​​

但是,如果你想使用$_POST,你必須在你的HTML中寫一個表單。另外我建議不要使用表格來對齊HTML表單元素。這不是一個優雅的解決方案。 how-to的stackoverflow有很多例子。這裏下面

是你的形式在HTML5

<form action="#" method="post"> 
    <label for="myusername">Your username : </label> 
    <input name="myusername" type="text" id="myusername" /> 
    <label for="mypassword">Your password : </label> 
    <input name="mypassword" type="password" id="mypassword" /> 
    <input type="submit" name="Submit" value="Login"> 
</form> 

<label for="input id">一個例子鉤子標籤的文本輸入框。如果用戶單擊文本,則for屬性可確保瀏覽器將焦點置於for指向的輸入元素處。

然後<input>有很多屬性。 name字段用於服務器端程序來處理表單提交。瀏覽器本身的id。它主要由CSS和客戶端腳本使用。

此外,您正在使用非常過時的教程來實現登錄網頁。 mysql()函數已正式被棄用。請改用PDO

此外,this tutorial應引導您在PDO世界上沒有問題

0

請使用

if(isset($_POST['myusername']) && $_POST['myusername']!=''){ 
    $myusername=$_POST['myusername']; 
    $mypassword=$_POST['mypassword']; 

    // To protect MySQL injection (more detail about MySQL injection) 
    $myusername = stripslashes($myusername); 
    $mypassword = stripslashes($mypassword); 
    $myusername = mysql_real_escape_string($myusername); 
    $mypassword = mysql_real_escape_string($mypassword); 
    $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and  password='$mypassword'"; 
    $result=mysql_query($sql); 

    // Mysql_num_row is counting table row 
    $count=mysql_num_rows($result); 

// If result matched $myusername and $mypassword, table row must be 1 row 
if($count==1){ 

// Register $myusername, $mypassword and redirect to file "login_success.php" 
    session_register("myusername"); 
    session_register("mypassword"); 
    header("location:login_success.php"); 
} 
else { 
echo "Wrong Username or Password"; 
} 


} 

這將解決你的問題

0

添加形式開始標籤,它會開始工作

<!DOCTYPE HTML> 
<html> 
<head> 
<body> 
<form action="" method="post"> 
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1"    bgcolor="#CCCCCC"> 
<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> 

</form> 
</body> 

根據您的要求指定操作