pass.php
<?php
header("Content-Type: text/html; charset=utf-8");
/* 找回密码 */
//if(!defined('IN_OLDCMS')) die('Access Denied');
$mysql_conf = array(
'host' => 'localhost',
'db' => 'xss',
'db_user' => 'xss',
'db_pwd' => 'pass',
);
$mysql_conn = @mysql_connect($mysql_conf['host'], $mysql_conf['db_user'], $mysql_conf['db_pwd']);
mysql_query("set names 'utf8'");//编码转化
$select_db = mysql_select_db($mysql_conf['db']);
$email = stripslashes(trim($_POST['email']));
$query = mysql_query("select id,userName,userPwd from `oc_user` where `email`='$email'");
$num = mysql_num_rows($query);
if($num==0){//该邮箱尚未注册!
echo 'noreg';
exit;
}else{
$row = mysql_fetch_array($query);
$getpasstime = time();
$uid = $row['id'];
$token = md5($uid.$row['username'].$row['password']);//组合验证码
$url = "http://xss.red/source/pass_reset.php?email=".$email."&token=".$token;//构造URL
$time = date('Y-m-d H:i');
$result = sendmail_test($time,$email,$url);
if($result== 0){//邮件发送成功
$msg = '系统已向您的邮箱发送了一封邮件<br/>请登录到您的邮箱及时重置您的密码!';
//更新数据发送时间
mysql_query("update `oc_user` set `getpasstime`='$getpasstime' where id='$uid'");
}else{
$msg = $result;
}
echo $msg;
}
/* *
* 已兼容php7
* 注:本邮件类都是经过我测试成功了的,如果大家发送邮件的时候遇到了失败的问题,请从以下几点排查:
* 1. 用户名和密码是否正确;
* 2. 检查邮箱设置是否启用了smtp服务;
* 3. 是否是php环境的问题导致;
* 4. 将26行的$smtp->debug = false改为true,可以显示错误信息,然后可以复制报错信息到网上搜一下错误的原因;
* 5. 如果还是不能解决,可以访问:http://www.daixiaorui.com/read/16.html#viewpl
* 下面的评论中,可能有你要找的答案。
*
*
* Last update time:2017/06
* UPDATE:
* 1、替换了高版本不支持的写法,如ereg、ereg_replace.
* 2、将 var 改为 public/private等.
* 3、使其兼容php7.
*
*/
function sendmail_test($time,$email,$url){
include_once("smtp.class.php");
$smtpserver = "ssl://smtp.qq.com";//SMTP服务器
$smtpserverport = 465;//SMTP服务器端口
$smtpusermail = "server@xiaoxiaowu.me";//SMTP服务器的用户邮箱
$smtpemailto = $email;//发送给谁
$smtpuser = "server@xiaoxiaowu.me";//SMTP服务器的用户帐号
$smtppass = "xxx";//SMTP服务器的用户密码
$mailsubject = "XSS找回密码 - Mr.Wu";//邮件主题
$mailbody = "亲爱的".$email."<br/><br/>您在 ".$time." 提交了找回密码请求。请点击下面的链接重置密码
(按钮24小时内有效)。<br/><br/><a href='".$url."'target='_blank'>".$url."</a>";//邮件内容
$mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
##########################################
$smtp = new smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
$smtp->debug = false;//是否显示发送的调试信息
$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
}
?>
pass_reset.php
<?php
header("Content-Type: text/html; charset=utf-8");
/* 找回密码 */
//if(!defined('IN_OLDCMS')) die('Access Denied');
$mysql_conf = array(
'host' => 'localhost',
'db' => 'xss',
'db_user' => 'xss',
'db_pwd' => 'pass',
);
$mysql_conn = @mysql_connect($mysql_conf['host'], $mysql_conf['db_user'], $mysql_conf['db_pwd']);
mysql_query("set names 'utf8'");//编码转化
$select_db = mysql_select_db($mysql_conf['db']);
$token = stripslashes(trim($_GET['token']));
$email = stripslashes(trim($_GET['email']));
$sql = "select * from `oc_user` where email='$email'";
$query = mysql_query($sql);
$row = mysql_fetch_array($query);
$uid = $row['id'];
if($row){
$mt = md5($row['id'].$row['username'].$row['password']);
if($mt==$token){
if(time()-$row['getpasstime']>246060){
$msg = '该链接已过期!';
}else{
//重置密码...
mysql_query("update `oc_user` set `userPwd`='12345' where id='$uid'");
mysql_query("update `oc_user` set `getpasstime`='0' where id='$uid'");
$msg = '您的密码已重置为: <b style="color:red;">123456789</b></br>请尽快登陆 <b style="color:red;"> 及时设置强密码 </b> 以防成果被他人窃取!!';
}
}else{
$msg = '无效的链接';
}
}else{
$msg = '错误的链接!';
}
echo $msg;
?>
smtp.class.php
smtp.class.php 发信类+最新可用+支持 PHP7 6年前 (2019-04-28) 1
代码解释
- 新建 pass.php 文件,作为密码找回请求包的接收文件,接收到邮箱后对数据库查询,如果存在,发送找回密码邮件。
- smtp.class.php 文件是一个流行的 PHP 发信小工具。
- pass_reset.php 文件,对 pass.php 文件生成的 URL 效验,如果通过,则重置密码。
过程截图
1.发送找回密码邮件
2.收到邮件
3.访问邮件中的地址,密码重置成功。
4.前端样式。