php通过邮箱找回密码

很久没有写过博客了,现在我要重新开始记录,工作中遇到的一些问题,并将其记录下来最为经验的积累.

--------------------回归正题-----------------------

很多网站,只要有用户注册,就必然会遇到用户登陆时忘记密码的情况,常用的找回密码的方式有两种,一种是通过发送短信验证码,另一种是通过发送连接到邮箱,打开连接通过验证后跳转到重置密码界面.

短信认证的方法,之前写过一篇jsp版本的.通过短信找回密码. 思路十分简单,通过随机生成6位字符验证码,然后将其写入数据库,通过调用第三方短信接口将验证码发送到用户.用户验证成功后即可重置密码.

邮箱找回密码方式也十分简单:

邮箱验证格式:http://ip/find_pass.php?t=md5(username)+md5(password)+6位随机字符

发送邮件接口smtp.class.php如下:

<?php
class Smtp{
    /* Public Variables */
	var $smtp_port;
	var $time_out;
	var $host_name;
	var $log_file;
	var $relay_host;
	var $debug;
	var $auth;
	var $user;
	var $pass;

	/* Private Variables */
	var $sock;
	/* Constractor */
	function smtp($relay_host = "", $smtp_port = 25, $auth = false, $user, $pass) {
		$this->debug = false;
		$this->smtp_port = $smtp_port;
		$this->relay_host = $relay_host;
		$this->time_out = 30; //is used in fsockopen()
		$this->auth = $auth; //auth
		$this->user = $user;
		$this->pass = $pass;
		$this->host_name = "localhost"; //is used in HELO command
		$this->log_file = "";
		$this->sock = false;
	}

	/* Main Function */
	function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") {
		$mail_from = $this->get_address($this->strip_comment($from));
		$body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body);
		$header .= "MIME-Version:1.0\r\n";
		if ($mailtype == "HTML") {
			$header .= "Content-Type:text/html\r\n";
		}
		$header .= "To: " . $to . "\r\n";
		if ($cc != "") {
			$header .= "Cc: " . $cc . "\r\n";
		}
		$header .= "From: $from<" . $from . ">\r\n";
		$header .= "Subject: " . $subject . "\r\n";
		$header .= $additional_headers;
		$header .= "Date: " . date("r") . "\r\n";
		$header .= "X-Mailer:By Redhat (PHP/" . phpversion() . ")\r\n";
		list ($msec, $sec) = explode(" ", microtime());
		$header .= "Message-ID: <" . date("YmdHis", $sec) . "." . ($msec * 1000000) . "." . $mail_from . ">\r\n";
		$TO = explode(",", $this->strip_comment($to));
		if ($cc != "") {
			$TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
		}

		if ($bcc != "") {
			$TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
		}

		$sent = true;
		foreach ($TO as $rcpt_to) {
			$rcpt_to = $this->get_address($rcpt_to);
			if (!$this->smtp_sockopen($rcpt_to)) {
				$this->log_write("Error: Cannot send email to " . $rcpt_to . "\n");
				$sent = false;
				continue;
			}

			if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
				$this->log_write("E-mail has been sent to <" . $rcpt_to . ">\n");
			} else {
				$this->log_write("Error: Cannot send email to <" . $rcpt_to . ">\n");
				$sent = false;
			}
			fclose($this->sock);
			$this->log_write("Disconnected from remote host\n");
		}
		return $sent;
	}

	/* Private Functions */

	function smtp_send($helo, $from, $to, $header, $body = "") {
		if (!$this->smtp_putcmd("HELO", $helo)) {
			return $this->smtp_error("sending HELO command");
		}
		// auth
		if ($this->auth) {
			if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
				return $this->smtp_error("sending HELO command");
			}

			if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
				return $this->smtp_error("sending HELO command");
			}
		}

		if (!$this->smtp_putcmd("MAIL", "FROM:<" . $from . ">")) {
			return $this->smtp_error("sending MAIL FROM command");
		}

		if (!$this->smtp_putcmd("RCPT", "TO:<" . $to . ">")) {
			return $this->smtp_error("sending RCPT TO command");
		}

		if (!$this->smtp_putcmd("DATA")) {
			return $this->smtp_error("sending DATA command");
		}

		if (!$this->smtp_message($header, $body)) {
			return $this->smtp_error("sending message");
		}

		if (!$this->smtp_eom()) {
			return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
		}

		if (!$this->smtp_putcmd("QUIT")) {
			return $this->smtp_error("sending QUIT command");
		}

		return true;
	}

	function smtp_sockopen($address) {
		if ($this->relay_host == "") {
			return $this->smtp_sockopen_mx($address);
		} else {
			return $this->smtp_sockopen_relay();
		}
	}

	function smtp_sockopen_relay() {
		$this->log_write("Trying to " . $this->relay_host . ":" . $this->smtp_port . "\n");
		$this->sock = @ fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
		if (!($this->sock && $this->smtp_ok())) {
			$this->log_write("Error: Cannot connenct to relay host " . $this->relay_host . "\n");
			$this->log_write("Error: " . $errstr . " (" . $errno . ")\n");
			return false;
		}

		$this->log_write("Connected to relay host " . $this->relay_host . "\n");
		return true;
		;
	}

	function smtp_sockopen_mx($address) {
		$domain = ereg_replace("^[email protected]([^@]+)$", "\1", $address);
		if ([email protected] getmxrr($domain, $MXHOSTS)) {
			$this->log_write("Error: Cannot resolve MX \"" . $domain . "\"\n");
			return false;
		}

		foreach ($MXHOSTS as $host) {
			$this->log_write("Trying to " . $host . ":" . $this->smtp_port . "\n");
			$this->sock = @ fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
			if (!($this->sock && $this->smtp_ok())) {
				$this->log_write("Warning: Cannot connect to mx host " . $host . "\n");
				$this->log_write("Error: " . $errstr . " (" . $errno . ")\n");
				continue;
			}

			$this->log_write("Connected to mx host " . $host . "\n");
			return true;
		}

		$this->log_write("Error: Cannot connect to any mx hosts (" . implode(", ", $MXHOSTS) . ")\n");
		return false;
	}

	function smtp_message($header, $body) {
		fputs($this->sock, $header . "\r\n" . $body);
		$this->smtp_debug("> " . str_replace("\r\n", "\n" . "> ", $header . "\n> " . $body . "\n> "));
		return true;
	}

	function smtp_eom() {
		fputs($this->sock, "\r\n.\r\n");
		$this->smtp_debug(". [EOM]\n");
		return $this->smtp_ok();
	}

	function smtp_ok() {
		$response = str_replace("\r\n", "", fgets($this->sock, 512));
		$this->smtp_debug($response . "\n");
		if (!ereg("^[23]", $response)) {
			fputs($this->sock, "QUIT\r\n");
			fgets($this->sock, 512);
			$this->log_write("Error: Remote host returned \"" . $response . "\"\n");
			return false;
		}

		return true;
	}

	function smtp_putcmd($cmd, $arg = "") {
		if ($arg != "") {
			if ($cmd == "")
				$cmd = $arg;

			else
				$cmd = $cmd . " " . $arg;
		}

		fputs($this->sock, $cmd . "\r\n");
		$this->smtp_debug("> " . $cmd . "\n");
		return $this->smtp_ok();
	}

	function smtp_error($string) {
		$this->log_write("Error: Error occurred while " . $string . ".\n");
		return false;
	}

	function log_write($message) {
		$this->smtp_debug($message);

		if ($this->log_file == "") {
			return true;
		}

		$message = date("M d H:i:s ") . get_current_user() . "[" . getmypid() . "]: " . $message;
		if ([email protected] file_exists($this->log_file) || !($fp = @ fopen($this->log_file, "a"))) {
			$this->smtp_debug("Warning: Cannot open log file \"" . $this->log_file . "\"\n");
			return false;
			;
		}

		flock($fp, LOCK_EX);
		fputs($fp, $message);
		fclose($fp);
		return true;
	}

	function strip_comment($address) {
		$comment = "\([^()]*\)";
		while (ereg($comment, $address)) {
			$address = ereg_replace($comment, "", $address);
		}
		return $address;
	}

	function get_address($address) {
		$address = ereg_replace("([ \t\r\n])+", "", $address);
		$address = ereg_replace("^.*<(.+)>.*$", "\1", $address);
		return $address;
	}

	function smtp_debug($message) {
		if ($this->debug) {
			echo $message . "
			;";
		}
	}
}
?>

将如下地址通过邮箱发送给用户

http://ip/find_pass.php?t=md5(username)+md5(password)+6位随机字符

用户通过查询数据库中是否有token=md5(username)+md5(password)+6位随机字符判断是否可以重置密码,如果成功跳转到重置密码页面.

稍后给出源码下载地址.

时间: 2024-08-29 05:33:18

php通过邮箱找回密码的相关文章

AspNetCore-MVC实战系列(二)之通过绑定邮箱找回密码

AspNetCore - MVC实战系列目录 . 爱留图网站诞生 . AspNetCore - MVC实战系列(一)之Sqlserver表映射实体模型 . AspNetCore-MVC实战系列(二)之通过绑定邮箱找回密码 开篇唠嗑 本篇内容写在5.1假期前夕,主要是让大家能在节假日休息充点的时候能有好的干货例子,到目前为止netcore方面的实战例子分享即将进入正轨,谢谢各位朋友多多支持:最近工作安排的新项目即将开始,项目前期就我一人搭建,让我犹豫的是对于公司这个内部系统并且是初建的项目用什么开

给自己项目添加注册、登陆、改密码、邮箱找回密码等功能,出现大坑!

1.使用django验证框架的登陆.注销功能,必须settings设置如下: LOGIN_REDIRECT_URL = 'shop:product_list' LOGOUT_REDIRECT_URL = 'shop:product_list' LOGIN_URL = 'shop:login' LOGOUT_URL = 'shop:logout' LOGIN_REDIRECT_URL = 'shop:product_list'-----------登录成功后,转到哪个页面的url,在此设置. LO

Java实现邮箱找回密码

[来源网络:http://www.cnblogs.com/zyw-205520/p/3738439.html] 通过邮件找回密码功能的实现 1.最近开发一个系统,有个需求就是,忘记密码后通过邮箱找回.现在的系统在注册的时候都会强制输入邮箱,其一目的就是 通过邮件绑定找回,可以进行密码找回.通过java发送邮件的功能我就不说了,重点讲找回密码. 2.参考别人的思路:发送邮件→请求邮件里的URL→验证url→{验证成功修改密码,不成功跳转到失败页面} 重点就是如何生成这个url和如何解析这个url.

Java实现邮箱找回密码 --转载

通过邮件找回密码功能的实现 1.最近开发一个系统,有个需求就是,忘记密码后通过邮箱找回.现在的系统在注册的时候都会强制输入邮箱,其一目的就是 通过邮件绑定找回,可以进行密码找回.通过java发送邮件的功能我就不说了,重点讲找回密码. 2.参考别人的思路:发送邮件→请求邮件里的URL→验证url→{验证成功修改密码,不成功跳转到失败页面} 重点就是如何生成这个url和如何解析这个url. 需要注意的是一个url只能修改一次密码,当同一帐号发送多封邮件,只有最后一封邮件的url 邮箱 3.加密能防止

简述邮箱找回密码功能

1. 在登录页面给个链接,在找回密码界面可以输入邮箱地址和验证码,验证码是为了防止恶意找回: 2. 后台,首先判断验证码是否正确,再判断该邮箱是否注册过用户; 3. 如果该邮箱存在,检索出该用户信息; 4. 如果你的密码没有MD5加密的话,可以直接查询出密码,如果MD5加密了,只能重新生成一个密码,并修改该用户: 5. 将新生成的密码发送到用户邮箱里,用JavaMail发送邮件;

[py][mx]django通过邮箱找回密码

忘记密码处理流程 直接上代码 class ActiveView(View): # 主要功能是修改user_profile里的is_active字段为1 def get(self, request, active_code): all_reocrds = EmailVerifyRecord.objects.filter(code=active_code) if all_reocrds: for record in all_reocrds: email = record.email user = U

thinkphp_邮箱找回密码

QQ邮箱STMP配置 链接... thinkphp mailer类加载  链接-- 看了上面两步你就可以做邮箱接口啦

django 开发忘记密码通过邮箱找回功能

一.流程分析: 1.点击忘记密码====>forget.html页面,输入邮箱和验证码,发送验证链接网址的邮件====>发送成功,跳到send_success.html提示 2.到邮箱里找到验证链接网址,访问重设密码网址reset.html===>重设密码提交数据,成功则返回首页,失败则返回错误信息 二. 1.users/forms.py文件中 from django import forms from captcha.fields import CaptchaField .......

PHP会员找回密码功能实现实例介绍

设置思路 1.用户注册时需要提供一个E-MAIL邮箱,目的就是用该邮箱找回密码. 2.当用户忘记密码或用户名时,点击登录页面的“找回密码”超链接,打开表单,并输入注册用的E-MAIL邮箱,提交. 3.系统通过该邮箱,从数据库中查找到该用户信息,并更新该用户的密码为一个临时密码(比如:12345678). 4.系统借助Jmail功能把该用户的信息发送到该用户的邮箱中(内容包括:用户名.临时密码.提醒用户及时修改临时密码的提示语). 5.用户用临时密码即可登录. HTML 我们在找回密码的页面上放置