首页 > 编程知识 正文

php验证码插件,php识别验证码

时间:2023-12-29 13:16:27 阅读:329547 作者:NLDM

本文目录一览:

我想在我的PHP网站里加一个验证码,滑动那种

你也真敢想。

网页验证码一般是在后台生成一个验证码在后台生成好图片返回给网页显示,用户输入的信息与后端保存的信息再进行验证。

如果后端的信息返回到前端是已文字的形式,就起不到安全的作用了。

你的这个功能可以这样设计,但作用不大。我来说说我的思路吧

首先后端返回一个数字类型的验证码,前端获取数字行的验证用js+css组织实现特效。你在上图的黑色部分设定一个挡扳的html元素(这个元素距离左边的三角形滑动块的距离就是后端返回的数字),左边滑块滑动多少距离达这个隐藏区块,获取这个数值,保存下来。

php实现验证码,能给具体的代码吗 在这谢谢过各位高手了

index.php:

?php

/* index.php start*/

if(!empty($_POST)) {

session_start();

if($_POST['seccode'] == $_SESSION['seccode']) {

echo 'scriptalert("验证成功")/script';

} else {

echo 'scriptalert("验证失败")/script';

}

session_destroy();

}

?

form action="" method="post" /

img id="seccode" src="seccode.php?rand=".?=rand()? / input type="text" name="seccode" / input type="submit" value="submit" /

input type="button" onclick="document.getElementById('seccode').src = 'seccode.php?reload=1' + Math.random()" value="change one"/

/form

?php

/* index.php end*/

?

******************************

seccode.php:

?php

/*seccode.php start*/

session_start();

if(isset($_SESSION['seccode']) empty($_GET['reload'])) {

$arr = $_SESSION['seccode'];

} else {

for($i=0; $i4; $i++) {

$arr[] = rand(0, 9);

}

$_SESSION['seccode'] = implode($arr);

}

$im = imagecreate(90, 25);

$backgroundcolor = imagecolorallocate ($im, 255, 255, 255);

for($i = 0; $i 4; $i++) {

$s = iconv('GBK', 'UTF-8', $arr[$i]);

$x = $i * 20 + mt_rand(0, 4) - 2;// 随机X

$y = mt_rand(0, 4); // 随机Y

$angle = mt_rand(0,4);// 随机角度

$text_color = imagecolorallocate($im, mt_rand(50, 255), mt_rand(50, 128), mt_rand(50, 255)); // 随机颜色

imagettftext($im,20, $angle,$x,20+$y,$text_color,"C:\Windows\Fonts\SIMSUN.TTC",$s);

}

// 线条

$linenums = mt_rand(10, 32);

for($i=0; $i = $linenums; $i++) {

$linecolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));

$linex = mt_rand(0, 62);

$liney = mt_rand(0, 25);

imageline($im, $linex, $liney, $linex + mt_rand(0, 4) - 2, $liney + mt_rand(0, 4) - 2, $linecolor);

}

// 杂点

for($i=0; $i = 64; $i++) {

$pointcolor = imagecolorallocate($im, mt_rand(50, 255), mt_rand(50, 255), mt_rand(50, 255));

imagesetpixel($im, mt_rand(0, 62), mt_rand(0, 25), $pointcolor);

}

// 边框

$bordercolor = imagecolorallocate($im , 150, 150, 150);

imagerectangle($im, 0, 0, 89, 24, $bordercolor);

header('Content-type: image/png');

imagepng($im);

imagedestroy($im);

/*seccode.php end*/

?

php中 为什么验证码 必须要开启 ob

php中验证码必须要开启gd 组件 这个东西是创建图的。

你说的ob 应该是ob_start()这个 ,打开缓冲区。

因为图片和正常的html格式不一样,在php中必须得声明输出的是图片格式,才可以输出图片也就是header.

header 代码前面不能有输出。所以必须打开缓冲区。把缓冲区的内容清理点才可以。

如何用PHP生成验证码

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

1、产生一张png的图片;

2、为图片设置背景色;

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

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

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

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

7、输出图片;

8、释放图片所占内存。

session_start(); 

getCode(4,60,20); 

 

function getCode($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 验证码怎样设置?

补充:

验证码可以同cookie或者session的方式传递数据,

不用有特别的设置,不过要安装GD库,否则不能显示图片.

随便放在哪里都可以,

显示是以图片方式显示,

例如验证码的程序文件是ck.php

那么显示就是

img src = "ck.php"

验证码有很多种编写方式,当然是众说纷纭,

凡是程序都不会有一个统一的格式,只有统一的思路.

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。