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

     

  • 相关阅读:
    监听器、过滤器
    最详细的Log4j使用教程
    Tomcat version 7.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 and 6 Web modules
    Unsupported major.minor version 52.0
    jdk安装
    数据库建表
    SpringMVC学习系列-后记 解决GET请求时中文乱码的问题
    面向对象中的常用魔术方法
    面向对象中的修饰关键词
    面向对象三大特性之二--【继承】
  • 原文地址:https://www.cnblogs.com/xbxxf/p/8968281.html
Copyright © 2020-2023  润新知