写了一个PHP随机问答验证码,安全性个人认为仍是不错的

WBOY
Release: 2016-06-13 13:04:27
Original
845 people have browsed it

写了一个PHP随机问答验证码,安全性个人认为还是不错的。

wangking717 写道
安全特点一:

1.生成的内容不是最终结果,而是一个问答系统,需要用户回答该问题,安全系数较传统验证码更为严谨。

2.生成的每个数字的颜色,字体,位置都是随机的,这样别人很难捕获到程序的规则。(干扰线就没去弄了)

?

如果有什么好的提议,可以留言,一起改善代码:

<?php  
//$i为要写在画板的位置,$num为要写的文字  
function rand4string($i,$num){  
    global $image_width,$image_height,$img;  
    $font=mt_rand(3,5);  
    $x=mt_rand(1,8)+$image_width*$i/6;  
    $y=mt_rand(1,$image_height/4);   
    $color=imagecolorallocate($img,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));  
    imagestring($img,$font,$x,$y,$num,$color);  
}  
session_start();  
header("content-type:image/png");  
$image_width=80;  
$image_height=18;  
$img = imagecreate($image_width,$image_height);  
imagecolorallocate($img,255,255,255);  
$random4symbol = array("+","-","x");    //三个符号进行循环  
$result = 0;  
$symbol = $random4symbol[mt_rand(0,count($random4symbol)-1)];  
$num1 = mt_rand(1,9);  
rand4string(0,$num1);  
$num2 = mt_rand(1,9);  
switch ($symbol){  
    case "+":  
        $result = $num1 + $num2;  
        rand4string(1,"+");  
        break;  
    case "-":  
        $result = $num1 - $num2;  
        rand4string(1,"-");  
        break;  
    case "x":  
        $result = $num1 * $num2;  
        rand4string(1,"x");  
        break;  
}  
rand4string(2,$num2);  
rand4string(3,"=");  
rand4string(4,"?");  
$_SESSION['code'] = $result;  
imagepng($img);  
imagedestroy($img);  
?>  
Copy after login

?

打完收工。。呵呵。

1 楼 521069108 2011-09-21  
唔,有点意思,能只能增加中文回答之类的不呢?

2 楼 521069108 2011-09-21  
估计要存数据库才能实现我说的那样 - 0
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!