PHP Retrieve password via email

Source: Internet
Author: User
Tags ereg explode

I haven't written a blog for a long time, and now I'm going to start recording some of the problems I've encountered in my work, and record the most accumulated experience.

--------------------back to the Chase-----------------------

Many websites, as long as there is a user registration, you will inevitably encounter the user forgot the password when the situation, commonly used to recover the password way there are two, one is by sending SMS verification code, the other is to send a connection to the mailbox, open the connection through the authentication to the Reset password interface.

SMS authentication method, previously written in a JSP version. Retrieve the password by SMS. The idea is simple, by randomly generating a 6-bit character verification code, and then writing it to the database, by invoking a third-party SMS interface to send the verification code to the user. You can reset your password after the user has successfully verified it.

e-mail back password is also very simple:

Mailbox Authentication format: HTTP://IP/FIND_PASS.PHP?T=MD5 (username) +md5 (password) + 6-bit random characters

The Outgoing mail interface smtp.class.php is as follows:

<?phpclass 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 = "", $s  Mtp_port = $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 _he Aders = "") {$mail _from = $this->get_address ($this->strip_comment ($from)); $body = Ereg_replace ("(^| ( \ \ \)] (\) "," \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-&gt ; 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); $this->smtp_sockopen ($rcpt _ To) {$this->log_write ("Error:cannot send e-mail 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 beenSent 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", $hel O) {return $this->smtp_error ("Sending HELO command");} Authif ($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-&GT;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); $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 ("\ 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-&GT;SMTP_OK ();} function Smtp_ok () {$response = Str_replace ("\ r \ n", "", Fgets ($this->sock)), $this->smtp_debug ($response. " \ n "), if (!ereg (" ^[23] ", $response)) {fputs ($this->sock," quit\r\n "), Fgets ($this->sock, +); $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-&GT;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. ";";}}}? >
Send the following address to the user via the mailbox

HTTP://IP/FIND_PASS.PHP?T=MD5 (username) +md5 (password) + 6-bit random characters

The user determines whether the password can be reset by querying the database for TOKEN=MD5 (username) +md5 (password) + 6-bit random characters if it successfully jumps to the Reset Password page.
Give the source code later.

PHP Retrieve password via email

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.