国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Backend Development PHP Tutorial PHP+Mysql+jQuery realizes password retrieval function

PHP+Mysql+jQuery realizes password retrieval function

Jun 08, 2018 pm 04:03 PM

This article mainly introduces the password retrieval function of PHP Mysql jQuery. Interested friends can refer to it. I hope it will be helpful to everyone.

The commonly-known password retrieval function cannot really retrieve forgotten passwords, because our passwords are encrypted and stored. Generally, developers will generate a new password through a program after verifying the user information or Generate a specific link and send an email to the user's mailbox, and the user will reset a new password from the email link to the reset password module of the website.

Of course, some websites now also use mobile phone text messages to retrieve passwords. The principle is to verify your identity by sending a verification code. Just like sending an email for verification, you still have to reset your password to complete the password retrieval. process.

The general steps are:

1. Enter the email address during registration in the form;
2. Verify that the user's email address is correct. If the user's email address does not exist in the user table of the website, the user will be prompted. The mailbox is not registered;
3. Send an email. If the user's mailbox does exist in the user table, combine the string used to verify the user information, and construct a URL and send it to the user's mailbox;
4. The user logs in to the mailbox to collect Email, click the URL link to the website verification program;
5. The website program queries the local user table through the string requested by the user, and compares whether the user information is correct;
6. If correct, go to the reset password page and try again Set a new password, otherwise it will prompt the user that the verification is invalid.

HTML

We place a page on the password retrieval page that requires the user to enter the email address used for registration, and then submit the front-end js to handle the interaction.

 <p><strong>輸入您注冊的電子郵箱,找回密碼:</strong></p> 
<p><input type="text" class="input" name="email" id="email"><span id="chkmsg"></span></p> 
<p><input type="button" class="btn" id="sub_btn" value="提 交"></p>

jQuery

After the user enters the email address and clicks submit, jQuery first verifies whether the email format is correct. If correct, it sends an Ajax request to the background sendmail.php. , sendmail.php is responsible for verifying whether the mailbox exists and sending emails, and will return the corresponding processing results to the front page. Please see the jQuery code:

 $(function(){ 
  $("#sub_btn").click(function(){ 
    var email = $("#email").val(); 
    var preg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; //匹配Email 
    if(email==&#39;&#39; || !preg.test(email)){ 
      $("#chkmsg").html("請?zhí)顚懻_的郵箱!"); 
    }else{ 
      $("#sub_btn").attr("disabled","disabled").val(&#39;提交中..&#39;).css("cursor","default"); 
      $.post("sendmail.php",{mail:email},function(msg){ 
        if(msg=="noreg"){ 
          $("#chkmsg").html("該郵箱尚未注冊!"); 
          $("#sub_btn").removeAttr("disabled").val(&#39;提 交&#39;).css("cursor","pointer"); 
        }else{ 
          $(".demo").html("<h3>"+msg+"</h3>"); 
        } 
      }); 
    } 
  }); 
})

The jQuery code used above is very convenient and concise to complete the front-end interactive operation. , if you have a certain jQuery foundation, the above code is clear at a glance and requires no explanation.
Of course, don’t forget to load the jQuery library file in the page. Some students often ask me why they can’t use the demo downloaded from jb51.net. 80% of the time it’s because the loading path of jquery or other files is wrong and the necessary files are not loaded. document.

PHP

sendmail.php needs to verify whether the email exists in the system user table. If so, read the user information and wake up the user id, username and password to md5 Encryption generates a special string as a verification code to retrieve the password, and then constructs the URL. At the same time, in order to control the timeliness of the URL link, we will record the operation time when the user submits the password retrieval action, and finally call the email sending class to send the email to the user's mailbox. The sending email class smtp.class.php has been packaged, please download it.

 include_once("connect.php");//連接數(shù)據(jù)庫 
 
$email = stripslashes(trim($_POST[&#39;mail&#39;])); 
   
$sql = "select id,username,password from `t_user` where `email`=&#39;$email&#39;"; 
$query = mysql_query($sql); 
$num = mysql_num_rows($query); 
if($num==0){//該郵箱尚未注冊! 
  echo &#39;noreg&#39;; 
  exit;   
}else{ 
  $row = mysql_fetch_array($query); 
  $getpasstime = time(); 
  $uid = $row[&#39;id&#39;]; 
  $token = md5($uid.$row[&#39;username&#39;].$row[&#39;password&#39;]);//組合驗(yàn)證碼 
  $url = "http://www.jb51.net/demo/resetpass/reset.php?email=".$email." 
&token=".$token;//構(gòu)造URL 
  $time = date(&#39;Y-m-d H:i&#39;); 
  $result = sendmail($time,$email,$url); 
  if($result==1){//郵件發(fā)送成功 
    $msg = &#39;系統(tǒng)已向您的郵箱發(fā)送了一封郵件<br/>請登錄到您的郵箱及時重置您的密碼!&#39;; 
    //更新數(shù)據(jù)發(fā)送時間 
    mysql_query("update `t_user` set `getpasstime`=&#39;$getpasstime&#39; where id=&#39;$uid &#39;"); 
  }else{ 
    $msg = $result; 
  } 
  echo $msg; 
} 
 
//發(fā)送郵件 
function sendmail($time,$email,$url){ 
  include_once("smtp.class.php"); 
  $smtpserver = ""; //SMTP服務(wù)器,如smtp.163.com 
  $smtpserverport = 25; //SMTP服務(wù)器端口 
  $smtpusermail = ""; //SMTP服務(wù)器的用戶郵箱 
  $smtpuser = ""; //SMTP服務(wù)器的用戶帳號 
  $smtppass = ""; //SMTP服務(wù)器的用戶密碼 
  $smtp = new Smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass); 
  //這里面的一個true是表示使用身份驗(yàn)證,否則不使用身份驗(yàn)證. 
  $emailtype = "HTML"; //信件類型,文本:text;網(wǎng)頁:HTML 
  $smtpemailto = $email; 
  $smtpemailfrom = $smtpusermail; 
  $emailsubject = "jb51.net - 找回密碼"; 
  $emailbody = "親愛的".$email.":<br/>您在".$time."提交了找回密碼請求。請點(diǎn)擊下面的鏈接重置密碼 
(按鈕24小時內(nèi)有效)。<br/><a href=&#39;".$url."&#39;target=&#39;_blank&#39;>".$url."</a>"; 
  $rs = $smtp->sendmail($smtpemailto, $smtpemailfrom, $emailsubject, $emailbody, $emailtype); 
 
  return $rs; 
}

Okay, at this time your mailbox will receive a password retrieval email from helloweba. There is a URL link in the email content. Click the link to reset.php of jb51.net for verification. Mail.

 include_once("connect.php");//連接數(shù)據(jù)庫 
 
$token = stripslashes(trim($_GET[&#39;token&#39;])); 
$email = stripslashes(trim($_GET[&#39;email&#39;])); 
$sql = "select * from `t_user` where email=&#39;$email&#39;"; 
 
$query = mysql_query($sql); 
$row = mysql_fetch_array($query); 
if($row){ 
  $mt = md5($row[&#39;id&#39;].$row[&#39;username&#39;].$row[&#39;password&#39;]); 
  if($mt==$token){ 
    if(time()-$row[&#39;getpasstime&#39;]>24*60*60){ 
      $msg = &#39;該鏈接已過期!&#39;; 
    }else{ 
      //重置密碼... 
      $msg = &#39;請重新設(shè)置密碼,顯示重置密碼表單,<br/>這里只是演示,略過。&#39;; 
    } 
  }else{ 
    $msg = &#39;無效的鏈接&#39;; 
  } 
}else{ 
  $msg = &#39;錯誤的鏈接!&#39;;   
} 
echo $msg;

reset.php first accepts the parameters email and token, and then queries whether the email exists in the data table t_user based on the email. If it exists, obtain the user's information, and the token combination method is the same as sendmail.php Construct the token value and then compare it with the token passed by the URL. If the difference between the current time and the time when the email is sent is more than 24 hours, it will prompt "The link has expired!". Otherwise, it means that the link is valid and it will be redirected to the reset page. Set password page, and finally the user sets a new password by himself.

Summary: Through registered email verification and password retrieval through this article’s email, we know the application of sending emails in website development and its importance. Of course, SMS verification applications are also popular now, which require related SMS interfaces. Just connect.
Finally, attach the data table t_user structure:

 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;

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning.

Related recommendations:

PHP jQuery MySql implementation of red and blue voting examples

The basics of Yii framework in PHP Usage

Basic knowledge and application of php design patterns

The above is the detailed content of PHP+Mysql+jQuery realizes password retrieval function. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do I implement authentication and authorization in PHP? How do I implement authentication and authorization in PHP? Jun 20, 2025 am 01:03 AM

TosecurelyhandleauthenticationandauthorizationinPHP,followthesesteps:1.Alwayshashpasswordswithpassword_hash()andverifyusingpassword_verify(),usepreparedstatementstopreventSQLinjection,andstoreuserdatain$_SESSIONafterlogin.2.Implementrole-basedaccessc

How can you handle file uploads securely in PHP? How can you handle file uploads securely in PHP? Jun 19, 2025 am 01:05 AM

To safely handle file uploads in PHP, the core is to verify file types, rename files, and restrict permissions. 1. Use finfo_file() to check the real MIME type, and only specific types such as image/jpeg are allowed; 2. Use uniqid() to generate random file names and store them in non-Web root directory; 3. Limit file size through php.ini and HTML forms, and set directory permissions to 0755; 4. Use ClamAV to scan malware to enhance security. These steps effectively prevent security vulnerabilities and ensure that the file upload process is safe and reliable.

What are the differences between == (loose comparison) and === (strict comparison) in PHP? What are the differences between == (loose comparison) and === (strict comparison) in PHP? Jun 19, 2025 am 01:07 AM

In PHP, the main difference between == and == is the strictness of type checking. ==Type conversion will be performed before comparison, for example, 5=="5" returns true, and ===Request that the value and type are the same before true will be returned, for example, 5==="5" returns false. In usage scenarios, === is more secure and should be used first, and == is only used when type conversion is required.

How do I perform arithmetic operations in PHP ( , -, *, /, %)? How do I perform arithmetic operations in PHP ( , -, *, /, %)? Jun 19, 2025 pm 05:13 PM

The methods of using basic mathematical operations in PHP are as follows: 1. Addition signs support integers and floating-point numbers, and can also be used for variables. String numbers will be automatically converted but not recommended to dependencies; 2. Subtraction signs use - signs, variables are the same, and type conversion is also applicable; 3. Multiplication signs use * signs, which are suitable for numbers and similar strings; 4. Division uses / signs, which need to avoid dividing by zero, and note that the result may be floating-point numbers; 5. Taking the modulus signs can be used to judge odd and even numbers, and when processing negative numbers, the remainder signs are consistent with the dividend. The key to using these operators correctly is to ensure that the data types are clear and the boundary situation is handled well.

How can you interact with NoSQL databases (e.g., MongoDB, Redis) from PHP? How can you interact with NoSQL databases (e.g., MongoDB, Redis) from PHP? Jun 19, 2025 am 01:07 AM

Yes, PHP can interact with NoSQL databases like MongoDB and Redis through specific extensions or libraries. First, use the MongoDBPHP driver (installed through PECL or Composer) to create client instances and operate databases and collections, supporting insertion, query, aggregation and other operations; second, use the Predis library or phpredis extension to connect to Redis, perform key-value settings and acquisitions, and recommend phpredis for high-performance scenarios, while Predis is convenient for rapid deployment; both are suitable for production environments and are well-documented.

How do I stay up-to-date with the latest PHP developments and best practices? How do I stay up-to-date with the latest PHP developments and best practices? Jun 23, 2025 am 12:56 AM

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

What is PHP, and why is it used for web development? What is PHP, and why is it used for web development? Jun 23, 2025 am 12:55 AM

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

How to set PHP time zone? How to set PHP time zone? Jun 25, 2025 am 01:00 AM

TosettherighttimezoneinPHP,usedate_default_timezone_set()functionatthestartofyourscriptwithavalididentifiersuchas'America/New_York'.1.Usedate_default_timezone_set()beforeanydate/timefunctions.2.Alternatively,configurethephp.inifilebysettingdate.timez

See all articles