短信验证php_php如何实现短信验证

php实现短信验证的方法:首先接入短信服务;然后在网站信息提交页面请求发送信息;接着服务器向短信服务提供商通信,提交发送请求;最后短信服务提供商通过运营商将信息发送到用户的手机中。

884b8699d479a5385d0c3e9162380eed.png

php实现短信验证的方法:

第一、实现php手机短信验证功能的基本思路

1、要找到短信服务提供商,接入短信服务

2、在网站信息提交页面请求发送信息

3、服务器向短信服务提供商通信,提交发送请求

4、短信服务提供商通过运营商将信息发送到用户的手机中

二、手机号码短信验证前台页面效果实现

d3a7664c269f11a3d91651727c7b5e22.png

/*-------------------------------------------*/

var InterValObj; //timer变量,控制时间

var count = 60; //间隔函数,1秒执行

var curCount;//当前剩余秒数

var code = ""; //验证码

var codeLength = 6;//验证码长度

function sendMessage() {

curCount = count;

var dealType; //验证方式

tel = $(’#tel’).val();

if(tel!=’’){

//验证手机有效性

var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;

if(!myreg.test($(’#tel’).val()))

{

alert(’请输入有效的手机号码!’);

return false;

}

tel = $(’#tel’).val();

//产生验证码

for (var i = 0; i < codeLength; i++) {

code += parseInt(Math.random() * 9).toString();

}

//设置button效果,开始计时

$("#btnSendCode").attr("disabled", "true");

$("#btnSendCode").val("请在" + curCount + "秒内输入验证码");

InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器,1秒执行一次

//向后台发送处理数据

$.ajax({

type: "POST", //用POST方式传输

dataType: "text", //数据格式:JSON

url: ’yanzhengma.php’, //目标地址(根据实际地址)

data: "&tel=" + tel + "&code=" + code,

error: function (XMLHttpRequest, textStatus, errorThrown) { },

success: function (msg){ }

});

}else{

alert(’请填写手机号码’);

}

}

//timer处理函数

function SetRemainTime() {

if (curCount == 0) {

window.clearInterval(InterValObj);//停止计时器

$("#btnSendCode").removeAttr("disabled");//启用按钮

$("#btnSendCode").val("重新发送验证码");

code = ""; //清除验证码。如果不清除,过时间后,输入收到的验证码依然有效

}

else {

curCount--;

$("#btnSendCode").val("请在" + curCount + "秒内输入验证码");

}

}

第三、调用短信服务器短信接口

笔者整理的页面是yanzhengma.php(具体根据服务商提供信息)

$post_data = array();

$post_data[’userid’] = 短信服务商提供ID;

$post_data[’account’] = ’短信服务商提供用户名’;

$post_data[’password’] = ’短信服务商提供密码’;

// Session保存路径

$sessSavePath = dirname(__FILE__)."/../data/sessions/";

if(is_writeable($sessSavePath) && is_readable($sessSavePath)){

session_save_path($sessSavePath);

}

session_register(’mobliecode’);

$_SESSION[’mobilecode’] = $_POST["code"];

$content=’短信验证码:’.$_POST["code"].’【短信验证】’;

$post_data[’content’] = mb_convert_encoding($content,’utf-8’, ’gb2312’); //短信内容需要用urlencode编码下

$post_data[’mobile’] = $_POST["tel"];

$post_data[’sendtime’] = ’’; //不定时发送,值为0,定时发送,输入格式YYYYMMDDHHmmss的日期值

$url=’http://IP:8888/sms.aspx?action=send’;

$o=’’;

foreach ($post_data as $k=>$v)

{

$o.="$k=".$v.’&’;

}

$post_data=substr($o,0,-1);

$ch = curl_init();

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_URL,$url);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果需要将结果直接返回到变量里,那加上这句。

$result = curl_exec($ch);

?>

第四:提交表单信息时对短信验证码验证

//手机验证码开始

session_start();

$svalitel = $_SESSION[’mobilecode’];

$vdcodetel = empty($vdcodetel) ? ’’ : strtolower(trim($vdcodetel));

if(strtolower($vdcodetel)!=$svalitel || $svalitel==’’)

{

ResetVdValue();

//echo "Pageviews=".$vdcodetel;

ShowMsg("手机验证码错误!", ’-1’);

exit();

}想了解更多编程学习,敬请关注php培训栏目!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现QQ邮箱验证,可以使用PHP编程语言进行操作。下面是一个使用PHP验证QQ邮箱的示例代码: ```php <?php // 获取用户输入的邮箱地址 $email = $_POST['email']; // 正则表达式验证邮箱格式 if (!preg_match("/^[a-zA-Z0-9_\-.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-.]+$/", $email)) { // 邮箱格式不正确 echo "邮箱格式不正确"; } else { // 使用SMTP协议连接QQ邮箱服务器 $smtpServer = "smtp.qq.com"; $username = "[email protected]"; // QQ邮箱账号 $password = "your_password"; // QQ邮箱密码 try { $smtpConnection = fsockopen($smtpServer, 25, $errno, $errstr, 5); if (!$smtpConnection) { throw new Exception($errstr, $errno); } // 响应码验证 $response = fgets($smtpConnection, 515); if (substr($response, 0, 3) != "220") { throw new Exception("连接服务器失败"); } // 发送验证命令 fputs($smtpConnection, "HELO $smtpServer\r\n"); $response = fgets($smtpConnection, 515); if (substr($response, 0, 3) != "250") { throw new Exception("HELO命令发送失败"); } fputs($smtpConnection, "AUTH LOGIN\r\n"); $response = fgets($smtpConnection, 515); if (substr($response, 0, 3) != "334") { throw new Exception("AUTH LOGIN命令发送失败"); } // 发送邮箱账号 fputs($smtpConnection, base64_encode($username) . "\r\n"); $response = fgets($smtpConnection, 515); if (substr($response, 0, 3) != "334") { throw new Exception("用户名发送失败"); } // 发送邮箱密码 fputs($smtpConnection, base64_encode($password) . "\r\n"); $response = fgets($smtpConnection, 515); if (substr($response, 0, 3) != "235") { throw new Exception("密码发送失败"); } // 发送目标邮箱地址 fputs($smtpConnection, "MAIL FROM: <$email>\r\n"); $response = fgets($smtpConnection, 515); if (substr($response, 0, 3) != "250") { throw new Exception("MAIL FROM命令发送失败"); } // 处理验证结果 $result = $response == "250 OK\r\n" ? "邮箱验证通过" : "邮箱验证失败"; echo $result; // 关闭连接 fputs($smtpConnection, "QUIT\r\n"); fclose($smtpConnection); } catch (Exception $e) { echo $e->getMessage(); } } ?> ``` 首先,通过正则表达式验证用户输入的邮箱格式是否正确。然后,使用SMTP协议连接QQ邮箱服务器,并发送验证命令,包括验证邮箱账号、密码和目标邮箱地址。根据服务器的响应,可以判断邮箱验证是否通过。最后,关闭与服务器的连接。 请注意,代码中的“[email protected]”和“your_password”需要替换为你自己的QQ邮箱账号和密码。另外,由于使用SMTP协议需要与邮件服务器进行交互,因此需要确保服务器上已开启相关的网络端口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值