2010-03-18 62 views
-1

我创建了更改密码的代码。现在它似乎包含一个错误。 当我填写表格更改密码,然后点击保存错误消息:更改密码

你忘了输入您的用户ID!

请重试。

我真的不知道错误信息是什么意思。请大家。帮我解决它。

这里的是代码:

<?php # change password.php 

//set the page title and include the html header. 
$page_title = 'Change Your Password'; 
//include('templates/header.inc'); 

if(isset($_POST['submit'])){//handle the form 
require_once('connectioncomplaint.php');//connect to the db. 
//include "connectioncomplaint.php"; 

//create a function for escaping the data. 
function escape_data($data){ 
    global $dbc;//need the connection. 
    if(ini_get('magic_quotes_gpc')){ 
    $data=stripslashes($data); 
    } 
    return mysql_real_escape_string($data); 
}//end function 

$message=NULL;//create the empty new variable. 

//check for a username 
if(empty($_POST['userid'])){ 
    $u=FALSE; 
    $message .='<p> You forgot enter your userid!</p>'; 
}else{ 
    $u=escape_data($_POST['userid']); 
} 

//check for existing password 
if(empty($_POST['password'])){ 
    $p=FALSE; 
    $message .='<p>You forgot to enter your existing password!</p>'; 
}else{ 
    $p=escape_data($_POST['password']); 
} 

//check for a password and match againts the comfirmed password. 
if(empty($_POST['password1'])) { 
    $np=FALSE; 
    $message .='<p> you forgot to enter your new password!</p>'; 
}else{ 
    if($_POST['password1'] == $_POST['password2']){ 
    $np=escape_data($_POST['password1']); 
}else{ 
    $np=FALSE; 
    $message .='<p> your new password did not match the confirmed new password!</p>'; 
} 
} 

if($u && $p && $np){//if everything's ok. 

$query="SELECT userid FROM access WHERE (userid='$u' AND password=PASSWORD('$p'))"; 
[email protected]_query($query); 
$num=mysql_num_rows($result); 
if($num == 1){ 
    $row=mysql_fetch_array($result, MYSQL_NUM); 

    //make the query 
    $query="UPDATE access SET password=PASSWORD('$np') WHERE userid=$row[0]"; 
    [email protected]_query($query);//run the query. 
    if(mysql_affected_rows() == 1) {//if it run ok. 

    //send an email,if desired. 
    echo '<p><b>your password has been changed.</b></p>'; 
    //include('templates/footer.inc');//include the HTML footer. 
    exit();//quit the script. 

    }else{//if it did not run OK. 
    $message= '<p>Your password could not be change due to a system error.We apolpgize for any inconvenience.</p><p>' .mysql_error() .'</p>'; 
    } 
    }else{ 
    $message= '<p> Your username and password do not match our records.</p>'; 
    } 
    mysql_close();//close the database connection. 

}else{ 
    $message .='<p>Please try again.</p>'; 
} 
}//end of the submit conditional. 

//print the error message if there is one. 
if(isset($message)){ 
echo'<font color="red">' , $message, '</font>'; 
} 
?> 

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
+4

喜欢这个'$ p && $ np' - 你能告诉我们'$ p'是否等于'$ np'吗? - 我会与你分享百万美元奖金(http://www.claymath.org/millennium/)。 –

+0

你的代码似乎不完整。 – Young

+2

等等...您编写了代码,并且您不明白您在自己的代码中输入的错误消息! – Josh

回答

0

这意味着你没有一起useridPOST参数发送。据推测,您的表单不包含名称为userid的元素。这个错误来自该行:因为这个测试显示

if(empty($_POST['userid'])){ 
0

该错误:

if(empty($_POST['userid'])){ 
    $u=FALSE; 
    $message .='<p> You forgot enter your userid!</p>'; 
} 

这意味着该服务器没有从形式接收userid场。


我猜你应该确保有一个在您的形式这样的字段 - 它不得不包含要更改密码的用户的userid

考虑到你可能不希望显示的那场,虽然,你会使用hidden输入:

<input type="hidden" name="userid" 
    value="<?php echo htmlspecialchars(HERE THE USERID); ?>" /> 
0

根据你的代码就意味着userid POST变量是空的。验证您使用的字段的名称。

3

请不要将实际密码存储在数据库中。创建密码的散列并将其存储。当用户登录时,对传入的密码进行散列并检查它是否与用户的散列密码匹配。有关更多信息,请参阅http://phpsec.org/articles/2005/password-hashing.html

此外,将用户标识存储在会话中并从中检索它,而不是从表单中获取它会更安全。即使输入在页面上隐藏,也可以使用许多方式替代。它会在应用程序中留下一个小小的漏洞,如果一个用户知道另一个用户的ID和密码,他们可以以一种无法检测的方式改变它。也就是说,尽管您没有该用户的登录记录,但可以更改密码。即使从表单(或url)获取用户标识,也要检查他们操作的数据是否是他们自己的,而不是其他人的,除非他们是具有足够特权的用户。

+1

+1散列至关重要 – Andy