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;
}

 

 先到这里,我们今天下班了,劳动节快乐,回来如果我还能记得,写的详细一点,这样只能发送验证码,里面很多东西没加,比如不能多次发送,防止注册机注册等,所以呢,回来再更新。
 

 

posted @ 2018-04-28 16:02  西贝小小凤  阅读(1843)  评论(0编辑  收藏  举报