給手機發驗證碼 綜合使用 (忘記密碼處理 php發驗證碼 重置用戶密碼)


前台頁面 提取手機號調用 jQuery的ajax,到發送驗證碼
  1. <title>找回密碼 - 2015年xxx報名系統</title>
  2. <meta name="description" content="2015年xxx報名系統">
  3. <meta name="keywords" content="2015年xxx報名系統">
  4. <link href="<?=base_url() ?>bootstrap/./pp/pp.css" rel="stylesheet" type="text/css">
  5. <!--[if IE 7]>
  6. <link rel="stylesheet" href="<?=base_url() ?>bootstrap/http://res.ppzuche.com/static/opt/css/font-awesome-ie7.min.css?v=4476c184">
  7. <![endif]-->
  8. <!-- loading patch drivers! @adam -->
  9. <link rel="apple-touch-icon-precomposed" href="<?=base_url() ?>bootstrap/#static/img/apple-touch-icon-car.png">
  10. <script type="text/javascript">
  11. var InterValObj; //timer變量,控制時間
  12. var count = 5; //間隔函數,1秒執行
  13. var curCount;//當前剩余秒數
  14. function validatemobile(mobile)
  15. {
  16. if(mobile.length==0)
  17. {
  18. alert('請輸入手機號碼!');
  19. document.form1.mobile.focus();
  20. return false;
  21. }
  22. if(mobile.length!=11)
  23. {
  24. alert('請輸入有效的手機號碼!');
  25. document.form1.mobile.focus();
  26. return false;
  27. }
  28. var myreg = /^(((13[0-9]{1})|159|153)+\d{8})$/;
  29. if(!myreg.test(mobile))
  30. {
  31. alert('請輸入有效的手機號碼!');
  32. document.form1.mobile.focus();
  33. return false;
  34. }
  35. }
  36. function sendMessage() {
  37.  curCount = count;
  38.   //設置button效果,開始計時
  39. $("#btnSendCode").attr("disabled", "true");
  40. $("#btnSendCode").val("請在" + curCount + "秒內輸入驗證碼");
  41. InterValObj = window.setInterval(SetRemainTime, 1000); //啟動計時器,1秒執行一次
  42.    //向后台發送處理數據
  43. var mobile = document.getElementById("mobile").value;
  44. validatemobile(mobile);//驗證手機號碼正確性
  45. $.ajax({
  46.   type: "POST", //用POST方式傳輸   
  47.   url: '<?=base_url() ?>../member/send_code', //目標地址.
  48. dataType: "json", //數據格式:JSON
  49.    //data: "dealType=" + dealType +"&uid=" + uid + "&code=" + code,
  50. data: "&a="+mobile,
  51.   success: function(json){
  52. if(json.msgid==1){//成功的處理
  53. alert(json.html);
  54. }
  55. else if(json.msgid==2){//失敗的處理
  56. alert(json.html);
  57. }
  58. else{
  59. alert(json.html);
  60. }
  61. }
  62. });
  63. }
  64. //timer處理函數
  65. function SetRemainTime() {
  66. if (curCount == 0) {
  67. window.clearInterval(InterValObj);//停止計時器
  68. $("#btnSendCode").removeAttr("disabled");//啟用按鈕
  69. $("#btnSendCode").val("重新發送驗證碼");
  70. }
  71. else {
  72. curCount--;
  73. $("#btnSendCode").val("請在" + curCount + "秒內輸入驗證碼");
  74. }
  75. }
  76. </script>
  77. </head>
  78. <body>
  79. <!-- New Menu Starts -->
  80. <div id="page">
  81. <div class="container mainPage" style="margin:20px 0px 0px -10px;height:590px;">
  82. <div class="row text-center se_login_plogin"><h1 style="margin-left:0px">東西南北中羽毛球大賽北京站報名</h1></div>
  83. <!--加框
  84. <div class="row-fluid sortable ui-sortable" style=" width:100%; margin-left:auto; margin-right:auto;">
  85. <div style="margin-left:300px" class="box span5">
  86. <div class="box-header well">
  87. <h2 style="margin-top:-10px;"><i class="icon-th"></i> 注冊</h2>
  88. </div>
  89. <div class="box-content" style="margin-left:60px;">
  90. <td><a class="btn btn-primary" href="<?=base_url() ?>register" style="width:200px"> 注冊 </a> </td>
  91. </div>
  92. </div>
  93. </div>
  94. -->
  95. <div class="row-fluid sortable ui-sortable" style=" width:100%; margin-left:auto; margin-right:auto;">
  96. <div style="margin-left:240px;margin-top:60px;" class="box span6">
  97. <div class="box-header well">
  98. <h2 style="margin-top:-10px;"><i class="icon-th"></i> 找回密碼</h2>
  99. </div>
  100. <div class="box-content">
  101. <table border="0" cellspacing="4" cellpadding="4" align="left">
  102. <form id="signupForm" class="regleader" action="<?=base_url() ?>member/validatinfo" method="post" >
  103. <tr>
  104. <td width="180" height="40" align="right">手機號碼</td>
  105. <td > <input type="text" style="width:180px" name="mobile" class="inputxt" value="" id="mobile" required/></td>
  106. <td>
  107. <input id="btnSendCode" type="button" value="發送驗證碼" onclick="sendMessage()" />
  108. </td>
  109. </tr>
  110. <tr>
  111. <td height="40" align="right">驗證碼</td>
  112. <td><input type="text" value="" name="code" class="inputxt" required="" style="width:100px"/></td>
  113. </tr>
  114. <tr>
  115. <td ></td>
  116. <td ><button type="submit" class="btn btn-primary" id="login" style="width:200px" > 下一步 </button>
  117. </td>
  118. </tr>
  119. <tr>
  120. <td height="10" colspan="2"></td>
  121. </tr>
  122. <tr><td height="10" colspan="3"></td></tr>
  123. <tr><td colspan="3" align="center"><a href="<?=base_url() ?>bootstrap/intro.asp"> >>> 點擊這里,反饋問題及提出建議和想法 >>></a></td></tr>
  124. </table>
  125. </form>
  126. <div>
  127. </div>
  128. </div>
  129. </div>
  130. </div>
  131. </div>
  132. </div>
 
 
2、后台發送驗證碼, 發送的同時寫入message表,為以后判斷驗證碼是否正確使用,除此之外也可以存入session中
 
  1. public function send_code(){
  2. $mobile=$_POST['a'];
  3. $message = substr(uniqid(),8,5);
  4. $send = 0;
  5. //--2014年12月15日---
  6. //email=hrb&Password=00007&MsgContent=您在XXX網站注冊的手機驗證碼為:
  7. //yvp4vq,此驗證碼同時也是您的初始登錄密碼,請在登錄后及時修改。[XXX]
  8. //&mobileNumber=18000076670&SendTime=&SubNumber=
  9. $sql = "insert into message (contect,sendtime,mobile) values
  10. ('".$message."','".date('Y-m-d H:i:s')."','".$mobile."')";
  11. //var_dump($sql);
  12. $query = $this->db->simple_query($sql);
  13. if($query){
  14. $insID = $this->db->insert_id();
  15. date_default_timezone_set('Asia/Chongqing');
  16. $data=array('email'=>'XXX',//短信接口用戶名
  17. 'Password'=>'XXX',//短信接口密碼
  18. 'MsgContent'=>$message,//'hrb'.date('Y-m-d H:i:s'),
  19. 'mobileNumber'=>$mobile,
  20. 'SendTime'=>date('Y-m-d H:i:s'),
  21. 'SubNumber'=>'',);
  22. //echo date('y-m-d h:i:s',time());
  23. $data=http_build_query($data);
  24. $opts=array(
  25. 'http'=>array(
  26. 'method'=>'POST',
  27. 'header'=>"Content-type:application/x-www-form-urlencoded\r\n".
  28. "Content-Length:".strlen($data)."\r\n",
  29. 'content'=>$data
  30. ),
  31. );
  32. $context=stream_context_create($opts);
  33. $html = file_get_contents('http://www.smssend.com.cn/api/send.asp',false,$context);
  34. //echo $html;
  35. $result = strpos($html,'<string xmlns="http://tempuri.org/">0');
  36. //var_dump($result);
  37. if($result){
  38. $sql = "update message set status = 1 where id = ".$insID;
  39. $query = $this->db->simple_query($sql);
  40. //echo "短信發送成功!";
  41. //return TRUE;
  42. $send = 1;
  43. }else{
  44. //return FALSE;
  45. //echo "短信發送失敗!";
  46. $send = 0;
  47. }
  48. }
  49. //$get=$_POST['a'];
  50. //給指定的手機號發短信功能
  51. //echo '<script>alert("報名成功!");</script>';
  52. //一般返回json格式的。可以返回數組處理,上面的text也是可以
  53. //$ismobile = 驗證手機函數();
  54. // $ismobile = 1;
  55. // if($ismobile){
  56. //驗證過是手機號碼,發送短信 下面進行邏輯處理,寫個發送短信的函數,返回發送狀態
  57. //這邊默認成功
  58. // $send = 1;
  59. if($send){
  60. $msgid = 1;//表示發送成功
  61. $html = '驗證碼已發送!';//'返回提示,也可以不用這個直接返回一個msgid即可';//做提示
  62. }
  63. else{
  64. $msgid = 2;//表示發送失敗
  65. $html = '提示失敗原因';
  66. }
  67. // }
  68. // else{
  69. // $msgid = 0;//表示失敗
  70. // $html = '手機號碼錯誤';
  71. // }
  72. echo json_encode(array('msgid'=>$msgid,'html'=>$html));
  73. // echo '<script>alert("報名成功!");location.href="www.baidu.com";</script>';
  74. // echo $code;
  75. /*
  76. $this->load->view('head');
  77. $this->load->view('forget_passwd');
  78. $this->load->view('foot');
  79. */
  80. }
 
3、驗證找回密碼提交的數據是否正確
  1. public function validatinfo(){
  2. //查詢提出過來的手機號是否存在
  3. $mobile = $this->input->post('mobile');
  4. $exist = $this->user_model->selectmobile($mobile);
  5. $code = $this->input->post('code');
  6. $checkcode = $this->user_model->selectcontect($mobile,$code);
  7. if(!$exist){
  8. echo '<script>alert("該手機號碼未注冊過,請檢查是否正確!");location.href="'.base_url().'member/forget_passwd";</script>';
  9. }elseif($code != $checkcode['0']['contect']){
  10. echo '<script>alert("驗證碼不正確,請檢查!");location.href="'.base_url().'member/forget_passwd";</script>';
  11. }
  12. else{
  13. // $this->load->view('head');
  14. // $this->load->view('forget_passwd');
  15. // $this->load->view('foot');
  16. $_SESSION['mobile'] = $mobile;
  17. $this->load->view('head');
  18. $this->load->view('repassword');
  19. $this->load->view('foot');
  20. }
  21. }
4、重置用戶密碼 (前台驗證兩個新密碼是否一致),后台重置
  1. public function resetpassword(){
  2. $password =md5($this->input->post('password'));
  3. $mobile = $_SESSION['mobile'];
  4. $resetpass = $this->user_model->resetpass($mobile,$password);
  5. if($resetpass){
  6. $_SESSION['mobile'] = '';
  7. echo '<script>alert("重置密碼成功!");location.href="'.base_url().'";</script>';
  8. }else{
  9. $this->load->view('head');
  10. $this->load->view('forget_passwd');
  11. $this->load->view('foot');
  12. }
  13. }
 
 
驗證碼過期 10分鍾 (當前時間 和 發送驗證碼時的時間進行對比)
  1. //檢查驗證碼是否過時(10分鍾)
  2. $sms_statistics = M('sms_statistics');
  3. $sms_time = $sms_statistics->order('time DESC')->where("mobile= "."'$phone'")->getField('time');
  4. $sms_time=strtotime($sms_time);
  5. $current_time=time();
  6. //var_dump((($current_time-$sms_time)/3600*60));
  7. if((($current_time-$sms_time)/3600*60)>10){
  8. $result=array('err_no' =>1011,'err_msg'=>"identify_code is overtime"); //驗證碼超時
  9. echo json_encode($result); exit;
  10. }
 
驗證碼一天發5次 【to_days(time) = to_days(now())】
  1. $sms_statistics = M('sms_statistics');
  2. $count = $sms_statistics->where("mobile= "."'$mobile'"." and to_days(time) = to_days(now())")->count();//to_days(time)中 time為數據庫中字段的時間
  3. if($count>=5){
  4. $result=array('err_no' =>1011,'err_msg'=>"sms has sended more than 5 times"); //不是手機號
  5. echo json_encode($result); exit;
  6. }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM