前台頁面 提取手機號調用
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;
- }