给手机发验证码 综合使用 (忘记密码处理 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. }
posted @ 2017-07-16 20:08  永杰本杰  阅读(2183)  评论(0编辑  收藏  举报