输入一个三位数的PHP程序,随机三位数相加减的php验证码程序

原先网上找了个只有加法的验证码程序修改为本博客的验证码,有一朋友他想要一个,其实好早前就给我说了,只是没找到他QQ号码,今天他上线发来消息,我发过去,由于此验证码需要修改博客程序才能正常使用,时隔太久,我都忘记需要修改哪儿了,自然他拿过去没能正常使用。晚饭后自己又写了一个验证码,实现三个数随机加减,感觉还挺好用的,现在贴出来分享。

/**

* 图片验证码生成

* copyright (c) 寒川 All Rights Reserved

* version 1.0

* URL: http://huikon.cn

*/

session_start();

function op()//随机输出运算符

{

$operations=array('+','-');

$i = mt_rand(0, count($operations) - 1);

$op = $operations[$i];

return $op;

}

function add($n1,$n2,$op)//计算

{

switch($op)

{

case '+':

$t=$n1+$n2;

break;

case '-':

$t=$n1-$n2;

break;

}

return $t;

}

$n =range(1,20);//建立数组

shuffle($n);//打乱

$op1=op();

$op2=op();

$expression = $n[1].$op1.$n[2].$op2.$n[3].'=?';//表达式

$total=add(add($n[1],$n[2],$op1),$n[3],$op2);//计算结果

$expression_len = strlen($expression);

$_SESSION['code'] =$total;

$img_width = 160;//图片宽度

$img_height = 30;//图片高度

$img = imageCreate($img_width, $img_height);

ImageColorAllocate($img, 255, 255, 255);//背景色

$white = ImageColorAllocate($img, mt_rand(30, 180), mt_rand(10, 100), mt_rand(40, 250));//文字颜色

$ix = mt_rand(0,10);//横坐标位置

$iy = mt_rand(0,10);//纵坐标位置

for ($i = 0; $i < $expression_len; $i++)

{

imageString($img, 5, $ix, $iy, $expression[$i], $white);

$ix += 14;

}

// 输出图片

header("Content-type: " . image_type_to_mime_type(IMAGETYPE_PNG));

imagepng($img);

imagedestroy($img);

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值