php 發送手機驗證碼


嗯哼,做為一個好的程序猿,一定要給顧客爸爸剁手的時候,充分的告訴他,你剁完手了,所以不只有郵件通知還要有手機短信的通知,今天就來寫一下php發送驗證碼

1、首先我就寫了幾個個方法,因為配置在后台,直接取就好了,有的需要表單提交過來,只需要接收一下,把對應的值寫一下就好了,這個函數或許麻煩一點,但是仔細看,不麻煩的

get_captcha()函數,直接訪問就能發送驗證碼

//發送驗證碼
    function get_captcha1(){
       $phone="15210743272";
        if(!preg_match("/^1[3456789]{1}\d{9}$/",$phone)){
            exit("手機號格式不正確");
        }
        if(time()-$_SESSION['last_send_time']<60){
            exit("短信發送太頻繁了!");
        }
        if($phone ){
            $r = rand(100000,999999);
            $content = '本次驗證碼為:'.$r.",十分鍾內有效.【新華好房】";
                $rs  = sendSms($phone,$content);//調用函數sendSms()
                if($rs==true){
                   $_SESSION['last_send_time'] = time();//將時間存進session 
                    echo 'ok';exit("");
                }
        }
    }

 

sendSms()函數
function sendSms($mobile,$msg){
    if(!$mobile && !$msg){
        return false;
    }
    $post="action=send&userid=376&account=zyjtz&password=kk123321yy&mobile=$mobile&content=$msg&sendTime=&extno=";
  //組成鏈接,把對應的userid和account寫對,其實不同的短信接口有不同的方式,有的話根據供應商給的接口來寫 $xml
=http_post('http://115.29.242.32:8888/sms.aspx', $post);//調用http_post()函數 $res = @simplexml_load_string($xml,NULL,LIBXML_NOCDATA); $res = json_decode(json_encode($res),true); if($res['returnstatus']=='Success'){ return true; } return false; //print_test($res); }
http_post()函數
function http_post($url, $post = '', $timeout = 10, $times = 3) {
    $stream = stream_context_create(array('http' => array('header' => 'Content-type: application/x-www-form-urlencoded', 'method' => 'POST', 'content' => $post, 'timeout' => $timeout)));
    while($times-- > 0) {
        $s = file_get_contents($url, NULL, $stream, 0, 4096000);
        if($s !== FALSE) return $s;
    }
    return FALSE;
}

 

 先到這里,我們今天下班了,勞動節快樂,回來如果我還能記得,寫的詳細一點,這樣只能發送驗證碼,里面很多東西沒加,比如不能多次發送,防止注冊機注冊等,所以呢,回來再更新。
 

 


免責聲明!

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



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