在PHP中生成随机密码并发送到电子邮件
当您在我的网站上注册时,您的密码将使用$hashedPass=md5($password);;存储 以下代码是我忘记的密码: 问题是:它重定向到拒绝页面。 朋友们,我可以知道我在下面的代码中哪里犯了错误吗 请帮助我的朋友在PHP中生成随机密码并发送到电子邮件,php,mysql,Php,Mysql,当您在我的网站上注册时,您的密码将使用$hashedPass=md5($password);;存储 以下代码是我忘记的密码: 问题是:它重定向到拒绝页面。 朋友们,我可以知道我在下面的代码中哪里犯了错误吗 请帮助我的朋友 <?php session_start(); // Start Session //Connect to the database through our include include_once "connect_to_mysql.php"; session
<?php
session_start(); // Start Session
//Connect to the database through our include
include_once "connect_to_mysql.php";
session_register("session");
// This is displayed if all the fields are not filled in
$empty_fields_message = "<p>Please go back and complete all the fields in the form.</p>Click <a class=\"two\" href=\"javascript:history.go(-1)\">here</a> to go back";
// Convert to simple variables
$email = $_POST['email'];
if (!isset($_POST['email'])) {
?>
<?php
}
elseif (empty($email)) {
echo $empty_fields_message;
}
else {
$email=mysql_real_escape_string($email);
$status = "OK";
$msg="";
//error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
$msg="Your email address is not correct<BR>";
$status= "NOTOK";}
echo "<br><br>";
if($status=="OK"){ $query="SELECT email,username FROM members WHERE email = '$email'";
$st=mysql_query($query);
$recs=mysql_num_rows($st);
$row=mysql_fetch_object($st);
$em=$row->email;// email is stored to a variable
if ($recs == 0) {
//Redirect to denied page.
print "<script language='Javascript'>document.location.replace('forgotenpass_denied.php');</script>";
}
function makeRandomPassword() {
$salt = "abchefghjkmnpqrstuvwxyz0123456789";
srand((double)microtime()*1000000);
$i = 0;
while ($i <= 7) {
$num = rand() % 33;
$tmp = substr($salt, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
$random_password = makeRandomPassword();
$password = md5($random_password);
$sql = mysql_query("UPDATE members SET password='$password' WHERE email='$email'");
$to = "$email";
// Change this to your site admin email
$from = "geetha.victor@tryteksolutions.com";
$subject = "Your Password Has been reset";
$message = "Hi, we have reset your password.
Your New Password is: $random_password
http://www.trytek.tryteksolutions.co.in/login.php
Once logged in you can change your password
Thanks!
Admin
This is an automated response, DO NOT REPLY!";
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
$to = "$to";
// Finally send the activation email to the member
mail($to, $subject, $message, $headers);
print "<script language='Javascript'>document.location.replace('forgotenpass_sucess.php');</script>";
}
else {echo "<center><font face='Verdana' size='2' color=red >$msg <br><br><input type='button' value='Retry' onClick='history.go(-1)'></center></font>";}
}
?>
也许可以尝试使用完整的服务器路径
print "<script language='Javascript'>document.location.replace('forgotenpass_denied.php');</script>";
试一试
问题显然就在这个街区的某个地方:
$query="SELECT email,username FROM members WHERE email = '$email'";
$st=mysql_query($query);
$recs=mysql_num_rows($st);
$row=mysql_fetch_object($st);
$em=$row->email;// email is stored to a variable
if ($recs == 0) {
//Redirect to denied page.
print "<script language='Javascript'>document.location.replace('forgotenpass_denied.php');</script>";
}
$query=“选择来自成员的电子邮件、用户名,其中电子邮件='$email';
$st=mysql\u查询($query);
$recs=mysql\u num\u行($st);
$row=mysql\u fetch\u对象($st);
$em=$row->email;//电子邮件存储到一个变量中
如果($recs==0){
//重定向到拒绝页面。
打印“document.location.replace('forgotenpass_denied.php');”;
}
更具体地说,问题在于查询返回的是0
记录(或FALSE
错误)。不幸的是,我的水晶球坏了,所以我不能告诉你为什么。然而,这里有一些事情需要验证:
- 您的数据库连接是否正常工作
- 查询中的表/字段名称是否与数据库架构匹配
$email
是否确实存在于表成员中
这里有太多错误,我几乎不知道从哪里开始
- 如以下文件所述:
返回值
成功或失败时结果集中的行数
如下所述,
FALSE==0
为true。因此,布尔表达式$recs==0
将计算为TRUE
,即使mysql\u num\u rows()
失败。相反,您必须使用严格的比较$recs===0
,以确保只有当其值为零时,才会计算为TRUE
您还应该检查函数调用是否失败,如果失败,则执行适当的错误处理。例如:
mysql_query($query) or die(mysql_error());
- 也就是说,正如
mysql\u query()
中所述:
警告
从PHP5.5.0开始,此扩展已被弃用,将来将被删除。相反,应该使用or扩展名。有关更多信息,请参见指南和。此功能的替代方案包括:
- 如以下文件所述:
双引号
如果字符串用双引号(“)括起来,PHP将为特殊字符解释更多转义序列:
╔════════════════════╦═══════════════════════════════════════════════════════════════════════════════════════════════════╗
║ Sequence ║ Meaning ║
╠════════════════════╬═══════════════════════════════════════════════════════════════════════════════════════════════════╣
║ \n ║ linefeed (LF or 0x0A (10) in ASCII) ║
║ \r ║ carriage return (CR or 0x0D (13) in ASCII) ║
║ \t ║ horizontal tab (HT or 0x09 (9) in ASCII) ║
║ \v ║ vertical tab (VT or 0x0B (11) in ASCII) (since PHP 5.2.5) ║
║ \e ║ escape (ESC or 0x1B (27) in ASCII) (since PHP 5.4.0) ║
║ \f ║ form feed (FF or 0x0C (12) in ASCII) (since PHP 5.2.5) ║
║ \\ ║ backslash ║
║ \$ ║ dollar sign ║
║ \" ║ double-quote ║
║ \[0-7]{1,3} ║ the sequence of characters matching the regular expression is a character in octal notation ║
║ \x[0-9A-Fa-f]{1,2} ║ the sequence of characters matching the regular expression is a character in hexadecimal notation ║
╚════════════════════╩═══════════════════════════════════════════════════════════════════════════════════════════════════╝
这就是说,由于
@
不是一个函数,因此实际上不需要对其进行转义
此外,字符类[\w\-]
包含字母、数字、下划线和连字符:这不足以匹配电子邮件地址,请查看原因;事实上,可以使用正则表达式验证电子邮件地址(应该改用解析器)
然而,在您的情况下,我不明白为什么有必要验证电子邮件地址,只需直接在您的数据库中查找即可
- 如以下文件所述:
注意:安全密码哈希
由于此哈希算法的快速性,不建议使用此函数保护密码。有关详细信息,请参阅
- 其他人已经注意到,您可以(也可能应该)使用HTTP头而不是JavaScript重定向
- 还有其他一些奇怪的事情:
$row=mysql_fetch_object($st);
$em=$row->email;// email is stored to a variable
不清楚为什么要这样做:毕竟,您在$email
中已经有了$em
的值。另外,请记住,您的查询可能返回了多条记录,而这只会获取其中的第一条记录
$to = "$email";
为什么不直接使用$to=$email
(或者直接使用$email
作为mail()
函数调用的第一个参数)?为了执行简单赋值,您需要PHP进行不必要的解析。因此,您在代码中使用双引号的字符串文字,而单引号的文字(具有较少的解析开销,并保护您不意外地包括未转义的变量)就足够了
$to = "$to";
我不知道这行代码的目的是什么
$status = "OK";
$status= "NOTOK";
if($status=="OK")
为什么不使用布尔值而不是字符串比较
- 在这个时代,人们真的应该使用CSS而不是
,
等等
为什么不能使用php头函数?为了重定向到另一个页面…在重新定向后,您是否看到forgottenpass\u sucess.php?是否应该将其拼写为forgottenpass\u sucess.php?(您的脚本文件名中有一个“t”)@AbhikChakraborty是的,可能是它的路径相关的问题?重新定向后你会看到什么url?Javascript代码不会处理完整的文件路径…除非他的文档根被设置为他的根目录。这就是我希望的:)我当然希望不是这样case@ginovva320我的代码是创建随机密码并发送到电子邮件,但我的代码是stru签入忘记密码已被拒绝,无法生成密码并发送电子邮件。表中的列成员包括:用户名、密码、emailinclude_once“connect_to_mysql.php”;以防万一,请在继续之前进行此小更改:
$st=mysql_query($query)或die(“数据库错误”);
$row=mysql_fetch_object($st);
$em=$row->email;// email is stored to a variable
$to = "$email";
$to = "$to";
$status = "OK";
$status= "NOTOK";
if($status=="OK")