导航:首页 > 编程语言 > php生成6位验证码

php生成6位验证码

发布时间:2022-07-05 07:45:59

‘壹’ thinkphp手机短信验证码怎么处理

1、解压附件到 ThinkPHPLibraryOrg 文件中
新建方法

publicfunctionsend(){
//初始化必填
$options['accountsid']='******';//填写自己的
$options['token']='*****';//填写自己的
//初始化$options必填
$ucpass=newOrgComUcpaas($options);

//随机生成6位验证码
srand((double)microtime()*1000000);//createarandomnumberfeed.
$ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
$list=explode(",",$ychar);
for($i=0;$i<6;$i++){
$randnum=rand(0,35);//10+26;
$authnum.=$list[$randnum];
}


//短信验证码(模板短信),默认以65个汉字(同65个英文)为一条(可容纳字数受您应用名称占用字符影响),超过长度短信平台将会自动分割为多条发送。分割后的多条短信将按照具体占用条数计费。

$appId="****";//填写自己的
$to=$_POST['to'];
$templateId="1";
$param=$authnum;
$arr=$ucpass->templateSMS($appId,$to,$templateId,$param);
if(substr($arr,21,6)==000000){
//如果成功就,这里只是测试样式,可根据自己的需求进行调节
echo"短信验证码已发送成功,请注意查收短信";

}else{
//如果不成功
echo"短信验证码发送失败,请联系客服";

}
}



前台页面

<formid="form">
<inputtype="text"name="to"id="to"/>
<buttonid="submit">获取验证码</button>
</form>
<scripttype="text/javascript">
$(function(){
$("#submit").click(function(){
vartourl=$("#form").attr("action");
$.post("__URL__/send",{to:$("#to").val()},function(data,textStatus){
alert(data);
});
})
})
</script>


测试时只能给注册手机号和添加白名单手机号码发送

‘贰’ 怎么用原生php做验证码

<?php
class validateCode{
private $charset=''; //随机因子
private $_len;
private $code; //验证码
private $codelen=4; //验证码长度
private $width=130; //画布宽度
private $height=50; //画布长度
private $img; //图形资源句柄
private $font; //制定字体
private $fontsize=26;//字体大小
private $fontcolor; //字体颜色

//初始化构造函数
public function __construct(){
$this->font=dirname(__file__).'/validateFont.ttf';
//字体文件及路径,__file__表示当前PHP文件绝对路径,dirname表示这个PHP文件的绝对路径的上一层
}

//生成随机码
private function createCode(){
$this->_len=strlen($this->charset)-1; //strlen表示计算字符串长度
for($i=1;$i<=$this->codelen;$i++){
$this->code.=$this->charset[mt_rand(0,$this->_len)];
}
}
//生成背景
private function createBG(){
$this->img=imagecreatetruecolor($this->width,$this->height);//创建图像
$color=imagecolorallocate($this->img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255)); //分配颜色函数(红,绿,蓝)
imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color); //矩形区域着色函数(要填充的图像,x1,y1,x2,y2,要填充的颜色),x1,y1,x2,y2表示矩形的对角线
}

//生成文字
private function createFont(){
$_x=$this->width/$this->codelen;
for($i=0;$i<$this->codelen;$i++){
$this->fontcolor=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height/1.4,$this->fontcolor,$this->font,$this->code[$i]); //参数 size 为字形的尺寸;angle 为字型的角度,顺时针计算,0 度为水平,也就是三点钟的方向 (由左到右),90 度则为由下到上的文字;x,y 二参数为文字的坐标值 (原点为左上角);参数 col 为字的颜色;fontfile 为字型文件名称,亦可是远端的文件;text 当然就是字符串内容了。
}
}

//生成线条雪花
private function createLine(){
//线条
for($i=0;$i<6;$i++){
$color=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}

//雪花
for($i=0;$i<40;$i++){
$color=imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
}
}

//输出
private function outPut(){
header("content-type:image/png");
imagepng($this->img);
imagedestroy($this->img);
}

//对外生成
public function doimg(){
$this->createCode();
$this->createBg();
$this->createFont();
$this->createLine();
$this->outPut();
}

//获取验证码
public function getCode(){
return strtolower($this->code);
}
}
?>
字体自己去找,修改下路径就行了

‘叁’ php怎么生成6位随机数,php随机数例子

通常情况下,当我们要生成一个随机字符串时,总是先创建一个字符池,然后用一个循环和mt_rand()或rand()生成php随机数,从字符池中随机选取字符,最后拼凑出需要的长度。代码如下:
function randomkeys($length)
{
$pattern = '
ABCDEFGHIJKLOMNOPQRSTUVWXYZ,./&l
t;>?;#:@~[]{}-_=+)(*&^%___FCKpd___0pound;"!'; //字符池
for($i=0;$i<$length;$i++)
{
$key .= $pattern{mt_rand(0,35)}; //生成php随机数
}
return $key;
}
echo randomkeys(8);

这个php随机函数能生成XC*=z~7L这样的字符串,随机!

‘肆’ 如何用PHP生成验证码

PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的图片,并将验证码保存在Session中。PHP生成验证码的大致流程有:

1、产生一张png的图片;

2、为图片设置背景色;

3、设置字体颜色和样式;

4、产生4位数的随机的验证码;

5、把产生的每个字符调整旋转角度和位置画到png图片上;

6、加入噪点和干扰线防止注册机器分析原图片来恶意破解验证码;

7、输出图片;

8、释放图片所占内存。

session_start();
getCode(4,60,20);

functiongetCode($num,$w,$h){
$code="";
for($i=0;$i<$num;$i++){
$code.=rand(0,9);
}
//4位验证码也可以用rand(1000,9999)直接生成
//将生成的验证码写入session,备验证时用
$_SESSION["helloweba_num"]=$code;
//创建图片,定义颜色值
header("Content-type:image/PNG");
$im=imagecreate($w,$h);
$black=imagecolorallocate($im,0,0,0);
$gray=imagecolorallocate($im,200,200,200);
$bgcolor=imagecolorallocate($im,255,255,255);
//填充背景
imagefill($im,0,0,$gray);

//画边框
imagerectangle($im,0,0,$w-1,$h-1,$black);

//随机绘制两条虚线,起干扰作用
$style=array($black,$black,$black,$black,$black,
$gray,$gray,$gray,$gray,$gray
);
imagesetstyle($im,$style);
$y1=rand(0,$h);
$y2=rand(0,$h);
$y3=rand(0,$h);
$y4=rand(0,$h);
imageline($im,0,$y1,$w,$y3,IMG_COLOR_STYLED);
imageline($im,0,$y2,$w,$y4,IMG_COLOR_STYLED);

//在画布上随机生成大量黑点,起干扰作用;
for($i=0;$i<80;$i++){
imagesetpixel($im,rand(0,$w),rand(0,$h),$black);
}
//将数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成
$strx=rand(3,8);
for($i=0;$i<$num;$i++){
$strpos=rand(1,6);
imagestring($im,5,$strx,$strpos,substr($code,$i,1),$black);
$strx+=rand(8,12);
}
imagepng($im);//输出图片
imagedestroy($im);//释放图片所占内存
}

‘伍’ php中如何使用随机函数rand()生成一个数字验证码

如果要生成四位数字的验证码,则可以用函数:

$srand = rand(1000,9999);

会生成在1000到9999之间的随机数字,如果要生成更多位数的数字,可以更改最小、最大值。

‘陆’ 怎样制作PHP验证码

<?php
//验证码:文本类型为图像
header("content-type:image/png");
define('TYPE',3);//1.字母 2.字母数字 3.数字 4.逻辑 5.汉字
session_start();
//创建画布
$img = imagecreatetruecolor(90,33);
//创建颜色
//$bgcolor = imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
$bgcolor = imagecolorallocate($img,255,255,255);
$textcolor = imagecolorallocate($img,rand(0,100),rand(0,100),rand(0,100));
//填充颜色到画布
imagefill($img,0,0,$bgcolor);
//创建但像素的点为干扰项
//for($i=0;$i<100;$i++){
// $pixelcolor = imagecolorallocate($img,rand(150,200),rand(150,200),rand(150,200));
// imagesetpixel($img,rand(0,70),rand(0,30),$pixelcolor);
//}
//
////划线
//$linecolor = imagecolorallocate($img,255,0,0);
//imageline($img,0,0,70,30,$linecolor);
//
////多边形
// $col_poly = imagecolorallocate ( $img , 0 , 255 , 0 );
// imagepolygon ( $img ,
// array (
// 5 , 5,
// 5, 15 ,
// 20,15,
// 20,5
// ),
// 4 ,
// $col_poly );
////弧线
//$arcColor = imagecolorallocate ( $img , 0 , 0 , 255 );
//imagearc($img,35,15,30,30,0,360,$arcColor);

//创建验证码的内容
//#字母
$letter = range('A','Z');
$letterStr = $letter[rand(0,25)].$letter[rand(0,25)].$letter[rand(0,25)].$letter[rand(0,25)];
//数字字母
$num = range(0,9);
$numberAndLetter = array_merge($letter,$num);
$nal = $numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)];
//#数字
$number = rand(1000,9999);
//#逻辑
$x = rand(1,9);
$y = rand(1,9);
$expression = $x."+".$y."=?";
$sum = $x+$y;
//#汉字
$CH = array('恭喜发财','财源滚滚','财源广进','才高八斗','学富五车','抬头见喜');
$chstr = $CH[rand(0,count($CH)-1)];
switch(TYPE){
case 1 : imagettftext($img,14,0,7,23,$textcolor,'MSYH.TTF',$letterStr);$_SESSION['code']=$letterStr;break;
case 2 : imagettftext($img,14,0,7,23,$textcolor,'MSYH.TTF',$nal);$_SESSION['code']=$nal;break;
case 3 : imagettftext($img,14,0,13,23,$textcolor,'MSYH.TTF',$number);$_SESSION['code']=$number;break;
case 4 : imagettftext($img,14,0,7,23,$textcolor,'MSYH.TTF',$expression);$_SESSION['code']=$sum;break;
case 5 : imagettftext($img,11,0,6,21,$textcolor,'MSYHBD.TTF',$chstr);$_SESSION['code']=$chstr;break;
}
//输入图像到浏览器
imagepng($img);
?>

‘柒’ 用PHP写一个接口,随机生成6位数字,然后以短信的形式发送到对应的手机号上。从来没写过接口不知道如何写

短信接口需要你有对应的短信服务商才行,通常是第三方短信服务商,这种服务往往是收费的,收取的费用越多,每天可以发送的短信数量越多。
你需要向他们申请短信接口,申请成功后会有接口文档,按照文档中的规定发送相应的数据即可,通常发送的数据包含:需要发送短信的手机号、短信内容。
接口写法通常会在文档中有写,甚至有的文档会附带范例以供开发者参考。

‘捌’ Php中生成6位随机数并显示

Php中生成6位随机数并显示实现如下:

1、使用shuffle函数生成随机数。

<?php

$arr=range(100000,999999);

shuffle($arr);

foreach($arras$values)

{

echo$values." ";//显示随机数

}

?>

2、使用array_unique函数生成随机数。

<?php

$arr=array();

while(count($arr)<10)

{

$arr[]=rand(1,10);

$arr=array_unique($arr);

}

echoimplode(" ",$arr);

?>

(8)php生成6位验证码扩展阅读:

PHP生成随机数的两种方法:

1、rand()函数:

//无参数:rand()函数用户获取随机数。

echo rand() . " ";//得到一个不定位数的随机数

//带参数:rand($min,$max),$min表示从XX开始取值,$max表示最大只能为XX。

echo rand(5, 15);//在5~15之间取一个数

注意:mt_rand() 用法跟rand()类似,但是mt_rand()的执行效率更高。

2、使用array_flip函数生成随机数,可以去掉重复值。

阅读全文

与php生成6位验证码相关的资料

热点内容
编译程序输入一个字符串 浏览:404
圆命令画法 浏览:305
如果给电脑e盘文件加密 浏览:801
javaswing项目 浏览:774
androidsdksetup 浏览:1003
pdf怎么设置中文 浏览:126
安卓手机用什么软件看伦敦金 浏览:964
魅族文件夹无名称 浏览:789
苏黎世无人机算法 浏览:872
核桃编程和小码王的融资 浏览:684
微积分教材pdf 浏览:725
写python给微信好友发消息 浏览:336
蚊帐自营米加密 浏览:420
学校推荐核桃编程 浏览:804
湖南农信app怎么导明细 浏览:473
福特abs编程 浏览:509
如何自学安卓手机 浏览:439
以太坊源码共识机制 浏览:912
单片机探测器 浏览:872
demo编程大赛作品怎么运行 浏览:52