A complete and secure PHP user logon System

Source: Internet
Author: User
Welcome to the Linux community forum, interact with 2 million technical staff, and read another interesting code. How can users securely change their email addresses? They can change the email address at any time, but they need to confirm. <? Phpfunctionuser_change_email ($ password1, $ new_email, $ user_name ){

Welcome to the Linux community forum and interact with 2 million technical staff> here, let's take a look at another interesting piece of code. How can users securely change their email addresses? They can change the email address at any time, but they need to confirm. <? Php function user_change_email ($ password1, $ new_email, $ user_name ){

Welcome to the Linux community forum and interact with 2 million technicians>

Let's look at another interesting piece of code. How can users safely change their email addresses? They can change the email address at any time, but they need to confirm.

<? Php

Function user_change_email ($ password1, $ new_email, $ user_name ){

Global $ feedback, $ hidden_hash_var;

If (validate_email ($ new_email )){

$ Hash = md5 ($ new_email. $ hidden_hash_var );

File: // change the unordered code value confirmed in the database, but do not change the email

File: // send a confirmation email with a new authentication code

$ User_name = strtolower ($ user_name );

$ Password1 = strtolower ($ password1 );

$ SQL = "UPDATE user SET confirm_hash = '$ hash' WHERE user_name =' $ user_name 'AND password ='". md5 ($ password1 )."'";

$ Result = db_query ($ SQL );

If (! $ Result | db_affected_rows ($ result) <1 ){

$ Feedback. = 'error-Incorrect User Name Or password ';

Return false;

} Else {

$ Feedback. = 'firmation sent ';

User_send_confirm_email ($ new_email, $ hash );

Return true;

}

} Else {

$ Feedback. = 'new Email Address Appears invalid ';

Return false;

}

}

Function user_confirm ($ hash, $ email ){

/*

When you click the connection related to the authentication email, a confirmation page is displayed, which calls this function,

*/

Global $ feedback, $ hidden_hash_var;

File: // verify that they didn't tamper with the email address

$ New_hash = md5 ($ email. $ hidden_hash_var );

If ($ new_hash & ($ new_hash = $ hash )){
   
File: // find this record in the database

$ SQL = "SELECT * FROM user WHERE confirm_hash = '$ hash '";

$ Result = db_query ($ SQL );

If (! $ Result | db_numrows ($ result) <1 ){

$ Feedback. = 'error-Hash Not Found ';

Return false;

} Else {

File: // confirm the email and set the account to activated

$ Feedback. = 'user Account Updated-You Are Now Logged in ';

User_set_tokens (db_result ($ result, 0, 'user _ name '));

$ SQL = "UPDATE user SET email = '$ email', is_confirmed = '1' WHERE confirm_hash = '$ hash '";

$ Result = db_query ($ SQL );

Return true;

}

} Else {

$ Feedback. = 'hash INVALID-UPDATE failed ';

Return false;

}

}

Function user_send_confirm_email ($ email, $ hash ){

/*

This function is used when registering for the first time or changing the email address.

*/

$ Message = "Thank You For Registering at Company.com ".

"NSimply follow this link to confirm your registration :".
      
"Nnhttp: // www.company.com/account/confirm.php? Hash = $ hash & email = ". urlencode ($ email)." nnOnce you confirm, you can use the services on PHPBuilder .";

Mail ($ email, 'registration Confirmation ', $ message, 'From: noreply@company.com ');

}

?>

[1] [2]

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.