2014-03-12 81 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> 

根据您的要求指定操作