PHP member password retrieval implementation example _ PHP Tutorial

Source: Internet
Author: User
Tags ereg preg
PHP member password retrieval implementation example. If your website has a membership system, you must automatically retrieve the password, that is, forget the password function, if you forget the password, you can retrieve the password directly through your email or mobile phone number. If your website has a membership system, you must retrieve the password automatically, that is, you forgot the password, if you forget your password, you can retrieve the password directly through your email or mobile phone number. next I will introduce how to retrieve the password in your mailbox.

Setting Ideas

1, the user needs to provide a E-MAIL mailbox, the purpose is to use the mailbox to retrieve the password.

2. when the user forgets the password or user name, click the login page "retrieve password" hyperlink, open the form, and enter the registered E-MAIL mailbox, submit.

3. the system finds the user information from the database through the mailbox, and updates the user's password to a temporary password (for example, 12345678 ).

4. The system uses the Jmail function to send the user's information to the user's mailbox (including the user name, temporary password, and prompt to remind the user to change the temporary password in a timely manner ).

5. You can log on with a temporary password.


HTML

On the password retrieval page, we place an email that requires the user to enter the account used for registration, and then submit the front-end js to process the interaction.

The code is as follows:

Enter your registered email address and retrieve your password:



JQuery

After you enter your email address and click submit, jQuery first verifies that the email address format is correct. If yes, it sends the email address to the backend. php sends Ajax requests, sendmail. php is responsible for verifying whether the mailbox exists and sending emails, and returning the corresponding processing results to the front-end page. please refer to the jQuery code:

The code is as follows:

$ (Function (){
$ ("# Sub_btn"). click (function (){
Var email = $ ("# email"). val ();
Var preg =/^ w + ([-+.] w +) * @ w + ([-.] w + )*. w + ([-.] w +) */; // Match Email
If (email = ''|! Preg. test (email )){
$ ("# Chkmsg" cmd.html ("Enter the correct email address! ");
} Else {
$ ("# Sub_btn"). attr ("disabled", "disabled" ).val('submit..'hangzhou.css ("cursor", "default ");
$. Post ("sendmail. php", {mail: email}, function (msg ){
If (msg = "noreg "){
$ ("# Chkmsg" cmd.html ("This email has not been registered! ");
$ ("# Sub_btn"). removeAttr ("disabled"). val ('submit your CSS ("cursor", "pointer ");
} Else {
$ (". Demo" ).html ("" + msg + "");
}
});
}
});
})

The above jQuery code is convenient and concise to complete front-end interaction operations. if you have a certain jQuery Foundation, the above code is clear and not explained.

Of course, don't forget to load the jQuery library file on the page. some people often ask me how to load the file from www. bKjia. why can't c0m download the demo? the 80% error is that jquery or other file loading paths are incorrect, leading to unnecessary File loading.
PHP

Sendmail. php needs to verify whether the Email exists in the system user table. if there is, read the user information and wake up the user ID, user name, and password with md5 encryption to generate a special string as the verification code for password retrieval, then construct the URL. In addition, in order to control the timeliness of URL links, we will record the operation time when the user submitted the password retrieval action, and finally call the Mail sending class to send the mail to the user's mailbox, and send the mail class smtp. class. php has been packaged. download it.

The code is as follows:

Include_once ("connect. php"); // connect to the database

$ Email = stripslashes (trim ($ _ POST ['mail']);

$ SQL = "select id, username, password from 't_ user' where 'email '=' $ email '";
$ Query = mysql_query ($ SQL );
$ Num = mysql_num_rows ($ query );
If ($ num = 0) {// This email address is not registered yet!
Echo 'noreg ';
Exit;
} Else {
$ Row = mysql_fetch_array ($ query );
$ Getpasstime = time ();
$ Uid = $ row ['id'];
$ Token = md5 ($ uid. $ row ['username']. $ row ['password']); // combined verification code
$ Url = "/demo/resetpass/reset. php? Email = ". $ email ."
& Token = ". $ token; // Construct a URL
$ Time = date ('Y-m-d H: I ');
$ Result = sendmail ($ time, $ email, $ url );
If ($ result = 1) {// The email is successfully sent.
$ Msg = 'The system has sent an email to your mailbox
Please log on to your mailbox and reset your password in time! ';
// Update the data sending time
Mysql_query ("update't _ user 'set' getpasstime '=' $ getpasstime 'where id =' $ uid '");
} Else {
$ Msg = $ result;
}
Echo $ msg;
}

// Send an email
Function sendmail ($ time, $ email, $ url ){
Include_once ("smtp. class. php ");
$ Smtpserver = ""; // SMTP server, such as smtp.163.com
$ Smtpserverport = 25; // SMTP server port
$ Smtpusermail = ""; // user email address of the SMTP server
$ Smtpuser = ""; // User account of the SMTP server
$ Smtppass = ""; // SMTP server user password
$ Smtp = new Smtp ($ smtpserver, $ smtpserverport, true, $ smtpuser, $ smtppass );
// Here, "true" indicates that authentication is used; otherwise, authentication is not used.
$ Emailtype = "HTML"; // mail type, text: text; webpage: HTML
$ Smtpemailto = $ email;
$ Smtpemailfrom = $ smtpusermail;
$ Emailsubject = "www. bKjia. c0m-retrieve password ";
$ Emailbody = "dear". $ email .":
You submitted a password retrieval request in ". $ time. Click the link below to reset the password.
(The button is valid within 24 hours ).
". $ Url ."";
$ Rs = $ smtp-> sendmail ($ smtpemailto, $ smtpemailfrom, $ emailsubject, $ emailbody, $ emailtype );

Return $ rs;
}

Now, you will receive a password retrieval email from helloweba in your mailbox. the email contains a URL link and click the link to www. bKjia. c0m reset. php to verify the mailbox.

The code is as follows:

Include_once ("connect. php"); // connect to the database

$ Token = stripslashes (trim ($ _ GET ['token']);
$ Email = stripslashes (trim ($ _ GET ['email ']);
$ SQL = "select * from't _ user' where email = '$ email '";

$ Query = mysql_query ($ SQL );
$ Row = mysql_fetch_array ($ query );
If ($ row ){
$ Mt = md5 ($ row ['id']. $ row ['username']. $ row ['password']);
If ($ mt = $ token ){
If (time ()-$ row ['getpasstime']> 24*60*60 ){
$ Msg = 'this link has expired! ';
} Else {
// Reset the password...
$ Msg = 'set the password again. The password reset form is displayed,
This is just a demonstration, skipped. ';
}
} Else {
$ Msg = 'invalid link ';
}
} Else {
$ Msg = 'invalid link! ';
}
Echo $ msg;

Reset. php first accepts the parameter email and token, and then queries whether the email exists in the t_user data table based on the Email. if so, it obtains the information of the user and sendmail. in php, the token combination method is the same as the token value, and then compare it with the token passed in the url. if the current time differs from the time when the email is sent by more than 24 hours, the message "This link has expired!" is displayed! ", Otherwise, the link is valid, and is transferred to the password reset page. Finally, the user sets the new password.

Conclusion: through email registration verification and password retrieval in this article, we know the application of email sending in website development and its importance. of course, text message verification is also popular now, this requires the connection of related SMS interfaces.

Finally, the t_user structure of the data table is attached:

The code is as follows:

Create table 't_ user '(
'Id' int (11) not null auto_increment,
'Username' varchar (30) not null,
'Password' varchar (32) not null,
'Email 'varchar (50) not null,
'Getpasstime' int (10) not null,
Primary key ('id ')
) ENGINE = MyISAM default charset = utf8;

Smtp. class. php class file

The code is as follows:

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 ("(^ | (rn) (.)", "1.3", $ body );

$ Header. = "MIME-Version: 1.0rn ";

If ($ mailtype = "HTML "){
$ Header. = "Content-Type: text/htmlrn ";
}

$ Header. = "To:". $ to. "rn ";

If ($ cc! = ""){
$ Header. = "Cc:". $ cc. "rn ";
}

$ Header. = "From: $ from <". $ from. "> rn ";

$ Header. = "Subject:". $ subject. "rn ";

$ Header. = $ additional_headers;

$ Header. = "Date:". date ("r"). "rn ";

$ Header. = "X-Mailer: By Redhat (PHP/". phpversion (). ") rn ";

List ($ msec, $ sec) = explode ("", microtime ());

$ Header. = "Message-ID: <". date ("YmdHis", $ sec ). ". ". ($ msec * 1000000 ). ". ". $ mail_from. "> rn ";

$ TO = explode (",", $ this-> strip_comment ($ ));

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 hostn ");
}

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 . [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 ("^. + @ ([^ @] +) $", "1", $ address );

If (! @ 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. "rn". $ body );

$ This-> smtp_debug ("> ". str_replace ("rn", "n ". ">", $ header. "n> ". $ body. "n> "));

Return true;
}

Function smtp_eom (){
Fputs ($ this-> sock, "rn. rn ");

$ This-> smtp_debug (". [EOM] n ");

Return $ this-> smtp_ OK ();
}

Function smtp_ OK (){
$ Response = str_replace ("rn", "", fgets ($ this-> sock, 512 ));

$ This-> smtp_debug ($ response. "n ");

If (! Ereg ("^ [23]", $ response )){
Fputs ($ this-> sock, "QUITrn ");

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. "rn ");

$ 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 (! @ File_exists ($ this-> log_file) |! ($ Fp = @ fopen ($ this-> log_file, ""))){
$ 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 ("([trn]) +", "", $ address );

$ Address = ereg_replace ("^. * <(. +)>. * $", "1", $ address );

Return $ address;
}

Function smtp_debug ($ message ){
If ($ this-> debug ){
Echo $ message ."
;";
}
}
}
?>

There is a database connection class at the end, so we will not introduce the database connection mysql class on this site.

...

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.