我正在尝试编写一个简单的脚本,通过验证他/她的电子邮件是什么来告诉用户他的密码。但是,在用户输入电子邮件地址后,我不知道如何向用户显示忘记的密码。
以下是我要尝试的脚本:
<html>
<body>
<form action="forgottenpassword.php" method="post">
Enter Your email: <input type="text" name="email"/>
<input type="submit" name="submit" value="Retrieve password"/>
</form>
</body>
</html>
<?php
include('config.php');
if (isset ($_POST['submit'])) {
$emailcheck = $_POST['email'];
$checkit = mysql_query("SELECT email FROM user WHERE email ='$emailcheck'");
if (!$checkit) exit();
$result = mysql_fetch_array($checkit);
$question = $result['email'];
echo "Your password is $question";
}
?>
答案 0 :(得分:2)
这是密码重置的简单算法:
通过电子邮件向用户发送电子邮件,其中包含该列的哈希字符串(使用GET
方法),如:
http://www.mysite.com/?uid=232&hash=4mnb2m3n4bkj23jh2h4j23kj4kj
然后,当用户关注该网址时,您检查该用户的哈希字符串,如果存在,那么您可以让他有机会重置密码(不会看到它),因为您哈希密码,不可逆。
答案 1 :(得分:1)
一旦进入数据库,您永远不应让用户看到密码,因为密码应该加密。这里有一些代码可以帮助你做你想做的事。
HTML
<div id="displayPass"></div>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label>Enter Your email: </label><input type="text" name="email"/>
<input type="submit" name="submit" value="Retrieve password"/>
</form>
PHP(应放在头部)
<?php
include('config.php');
if (isset($_POST['submit'])) {
$emailcheck = $_POST['email'];
$checkit = mysql_query("SELECT password FROM user WHERE email ='$emailcheck'");
if (empty($checkit))
{
echo "<script type='text/javascript'>
var div = document.getElementById('displayPass');
div.innerHTML = 'You have entered the incorrect email.';
</script>";
}
else
{
$result = mysql_fetch_array($checkit);
$question = $result['email'];
echo "<script type='text/javascript'>
var div = document.getElementById('displayPass');
div.innerHTML = 'Your password is '".$question."';
</script>";
}
}
?>
但你应该是这样的......
HTML(带有一些php)
<?php
session_start();
<form action="checkEmail.php" method="post">
<label>Enter Your email: </label><input type="text" name="email"/>
<input type="submit" name="submit" value="Retrieve password"/>
<label><?php echo $_SESSION["errorMessage"]; ?></label>
</form>
?>
PHP(checkEmail.php)
<?php
session_start();
//query database to see if its a correct email address
if(//email is not corrent){
$_SESSION["errorMessage"] = "Invalid Email.";
header("Location: emailForm.php");
}
else {
header("Location: newPasswordForm.php");
}
?>
然后,您创建一个新页面,用户可以在其中填写表单以更改其密码。
希望这有帮助。