给手机发验证码 综合使用 (忘记密码处理 php发验证码 重置用户密码)
前台页面 提取手机号调用 jQuery的ajax,到发送验证码
[php] view plain copy
- <title>找回密码 - 2015年xxx报名系统</title>
- <meta name="description" content="2015年xxx报名系统">
- <meta name="keywords" content="2015年xxx报名系统">
- <link href="<?=base_url() ?>bootstrap/./pp/pp.css" rel="stylesheet" type="text/css">
- <!--[if IE 7]>
- <link rel="stylesheet" href="<?=base_url() ?>bootstrap/http://res.ppzuche.com/static/opt/css/font-awesome-ie7.min.css?v=4476c184">
- <![endif]-->
- <!-- loading patch drivers! @adam -->
- <link rel="apple-touch-icon-precomposed" href="<?=base_url() ?>bootstrap/#static/img/apple-touch-icon-car.png">
- <script type="text/javascript">
- var InterValObj; //timer变量,控制时间
- var count = 5; //间隔函数,1秒执行
- var curCount;//当前剩余秒数
- function validatemobile(mobile)
- {
- if(mobile.length==0)
- {
- alert('请输入手机号码!');
- document.form1.mobile.focus();
- return false;
- }
- if(mobile.length!=11)
- {
- alert('请输入有效的手机号码!');
- document.form1.mobile.focus();
- return false;
- }
- var myreg = /^(((13[0-9]{1})|159|153)+\d{8})$/;
- if(!myreg.test(mobile))
- {
- alert('请输入有效的手机号码!');
- document.form1.mobile.focus();
- return false;
- }
- }
- function sendMessage() {
- curCount = count;
- //设置button效果,开始计时
- $("#btnSendCode").attr("disabled", "true");
- $("#btnSendCode").val("请在" + curCount + "秒内输入验证码");
- InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器,1秒执行一次
- //向后台发送处理数据
- var mobile = document.getElementById("mobile").value;
- validatemobile(mobile);//验证手机号码正确性
- $.ajax({
- type: "POST", //用POST方式传输
- url: '<?=base_url() ?>../member/send_code', //目标地址.
- dataType: "json", //数据格式:JSON
- //data: "dealType=" + dealType +"&uid=" + uid + "&code=" + code,
- data: "&a="+mobile,
- success: function(json){
- if(json.msgid==1){//成功的处理
- alert(json.html);
- }
- else if(json.msgid==2){//失败的处理
- alert(json.html);
- }
- else{
- alert(json.html);
- }
- }
- });
- }
- //timer处理函数
- function SetRemainTime() {
- if (curCount == 0) {
- window.clearInterval(InterValObj);//停止计时器
- $("#btnSendCode").removeAttr("disabled");//启用按钮
- $("#btnSendCode").val("重新发送验证码");
- }
- else {
- curCount--;
- $("#btnSendCode").val("请在" + curCount + "秒内输入验证码");
- }
- }
- </script>
- </head>
- <body>
- <!-- New Menu Starts -->
- <div id="page">
- <div class="container mainPage" style="margin:20px 0px 0px -10px;height:590px;">
- <div class="row text-center se_login_plogin"><h1 style="margin-left:0px">东西南北中羽毛球大赛北京站报名</h1></div>
- <!--加框
- <div class="row-fluid sortable ui-sortable" style=" width:100%; margin-left:auto; margin-right:auto;">
- <div style="margin-left:300px" class="box span5">
- <div class="box-header well">
- <h2 style="margin-top:-10px;"><i class="icon-th"></i> 注册</h2>
- </div>
- <div class="box-content" style="margin-left:60px;">
- <td><a class="btn btn-primary" href="<?=base_url() ?>register" style="width:200px"> 注册 </a> </td>
- </div>
- </div>
- </div>
- -->
- <div class="row-fluid sortable ui-sortable" style=" width:100%; margin-left:auto; margin-right:auto;">
- <div style="margin-left:240px;margin-top:60px;" class="box span6">
- <div class="box-header well">
- <h2 style="margin-top:-10px;"><i class="icon-th"></i> 找回密码</h2>
- </div>
- <div class="box-content">
- <table border="0" cellspacing="4" cellpadding="4" align="left">
- <form id="signupForm" class="regleader" action="<?=base_url() ?>member/validatinfo" method="post" >
- <tr>
- <td width="180" height="40" align="right">手机号码</td>
- <td > <input type="text" style="width:180px" name="mobile" class="inputxt" value="" id="mobile" required/></td>
- <td>
- <input id="btnSendCode" type="button" value="发送验证码" onclick="sendMessage()" />
- </td>
- </tr>
- <tr>
- <td height="40" align="right">验证码</td>
- <td><input type="text" value="" name="code" class="inputxt" required="" style="width:100px"/></td>
- </tr>
- <tr>
- <td ></td>
- <td ><button type="submit" class="btn btn-primary" id="login" style="width:200px" > 下一步 </button>
- </td>
- </tr>
- <tr>
- <td height="10" colspan="2"></td>
- </tr>
- <tr><td height="10" colspan="3"></td></tr>
- <tr><td colspan="3" align="center"><a href="<?=base_url() ?>bootstrap/intro.asp"> >>> 点击这里,反馈问题及提出建议和想法 >>></a></td></tr>
- </table>
- </form>
- <div>
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
2、后台发送验证码, 发送的同时写入message表,为以后判断验证码是否正确使用,除此之外也可以存入session中
[php] view plain copy
- public function send_code(){
- $mobile=$_POST['a'];
- $message = substr(uniqid(),8,5);
- $send = 0;
- //--2014年12月15日---
- //email=hrb&Password=00007&MsgContent=您在XXX网站注册的手机验证码为:
- //yvp4vq,此验证码同时也是您的初始登录密码,请在登录后及时修改。[XXX]
- //&mobileNumber=18000076670&SendTime=&SubNumber=
- $sql = "insert into message (contect,sendtime,mobile) values
- ('".$message."','".date('Y-m-d H:i:s')."','".$mobile."')";
- //var_dump($sql);
- $query = $this->db->simple_query($sql);
- if($query){
- $insID = $this->db->insert_id();
- date_default_timezone_set('Asia/Chongqing');
- $data=array('email'=>'XXX',//短信接口用户名
- 'Password'=>'XXX',//短信接口密码
- 'MsgContent'=>$message,//'hrb'.date('Y-m-d H:i:s'),
- 'mobileNumber'=>$mobile,
- 'SendTime'=>date('Y-m-d H:i:s'),
- 'SubNumber'=>'',);
- //echo date('y-m-d h:i:s',time());
- $data=http_build_query($data);
- $opts=array(
- 'http'=>array(
- 'method'=>'POST',
- 'header'=>"Content-type:application/x-www-form-urlencoded\r\n".
- "Content-Length:".strlen($data)."\r\n",
- 'content'=>$data
- ),
- );
- $context=stream_context_create($opts);
- $html = file_get_contents('http://www.smssend.com.cn/api/send.asp',false,$context);
- //echo $html;
- $result = strpos($html,'<string xmlns="http://tempuri.org/">0');
- //var_dump($result);
- if($result){
- $sql = "update message set status = 1 where id = ".$insID;
- $query = $this->db->simple_query($sql);
- //echo "短信发送成功!";
- //return TRUE;
- $send = 1;
- }else{
- //return FALSE;
- //echo "短信发送失败!";
- $send = 0;
- }
- }
- //$get=$_POST['a'];
- //给指定的手机号发短信功能
- //echo '<script>alert("报名成功!");</script>';
- //一般返回json格式的。可以返回数组处理,上面的text也是可以
- //$ismobile = 验证手机函数();
- // $ismobile = 1;
- // if($ismobile){
- //验证过是手机号码,发送短信 下面进行逻辑处理,写个发送短信的函数,返回发送状态
- //这边默认成功
- // $send = 1;
- if($send){
- $msgid = 1;//表示发送成功
- $html = '验证码已发送!';//'返回提示,也可以不用这个直接返回一个msgid即可';//做提示
- }
- else{
- $msgid = 2;//表示发送失败
- $html = '提示失败原因';
- }
- // }
- // else{
- // $msgid = 0;//表示失败
- // $html = '手机号码错误';
- // }
- echo json_encode(array('msgid'=>$msgid,'html'=>$html));
- // echo '<script>alert("报名成功!");location.href="www.baidu.com";</script>';
- // echo $code;
- /*
- $this->load->view('head');
- $this->load->view('forget_passwd');
- $this->load->view('foot');
- */
- }
3、验证找回密码提交的数据是否正确
[php] view plain copy
- public function validatinfo(){
- //查询提出过来的手机号是否存在
- $mobile = $this->input->post('mobile');
- $exist = $this->user_model->selectmobile($mobile);
- $code = $this->input->post('code');
- $checkcode = $this->user_model->selectcontect($mobile,$code);
- if(!$exist){
- echo '<script>alert("该手机号码未注册过,请检查是否正确!");location.href="'.base_url().'member/forget_passwd";</script>';
- }elseif($code != $checkcode['0']['contect']){
- echo '<script>alert("验证码不正确,请检查!");location.href="'.base_url().'member/forget_passwd";</script>';
- }
- else{
- // $this->load->view('head');
- // $this->load->view('forget_passwd');
- // $this->load->view('foot');
- $_SESSION['mobile'] = $mobile;
- $this->load->view('head');
- $this->load->view('repassword');
- $this->load->view('foot');
- }
- }
4、重置用户密码 (前台验证两个新密码是否一致),后台重置
[php] view plain copy
- public function resetpassword(){
- $password =md5($this->input->post('password'));
- $mobile = $_SESSION['mobile'];
- $resetpass = $this->user_model->resetpass($mobile,$password);
- if($resetpass){
- $_SESSION['mobile'] = '';
- echo '<script>alert("重置密码成功!");location.href="'.base_url().'";</script>';
- }else{
- $this->load->view('head');
- $this->load->view('forget_passwd');
- $this->load->view('foot');
- }
- }
验证码过期 10分钟 (当前时间 和 发送验证码时的时间进行对比)
[php] view plain copy
- //检查验证码是否过时(10分钟)
- $sms_statistics = M('sms_statistics');
- $sms_time = $sms_statistics->order('time DESC')->where("mobile= "."'$phone'")->getField('time');
- $sms_time=strtotime($sms_time);
- $current_time=time();
- //var_dump((($current_time-$sms_time)/3600*60));
- if((($current_time-$sms_time)/3600*60)>10){
- $result=array('err_no' =>1011,'err_msg'=>"identify_code is overtime"); //验证码超时
- echo json_encode($result); exit;
- }
验证码一天发5次 【to_days(time) = to_days(now())】
[php] view plain copy
- $sms_statistics = M('sms_statistics');
- $count = $sms_statistics->where("mobile= "."'$mobile'"." and to_days(time) = to_days(now())")->count();//to_days(time)中 time为数据库中字段的时间
- if($count>=5){
- $result=array('err_no' =>1011,'err_msg'=>"sms has sended more than 5 times"); //不是手机号
- echo json_encode($result); exit;
- }