Home > php教程 > php手册 > body text

简单实例php验证码代码

WBOY
Release: 2016-05-25 16:42:35
Original
1301 people have browsed it

验证码,是一种区分用户是计算机和人的公共全自动程序,在captcha测试中,作为服务器的计算机会自动生成一个问题由用户来解答,这个问题可以由计算机生成并评判,但是必须只有人类才能解答,由于计算机无法解答captcha的问题,所以回答出问题的用户就可以被认为是人类,php实例代码如下:

<?php
session_start();
$string = null;
$im = imagecreatetruecolor(60, 25); //创建真彩图60*25
$bg = imagecolorallocate($im, 255, 255, 255); //白色背景
imagefill($im, 0, 0, $bg); //填充白色
$x = 5; //
$y = 0; //文字坐标
for ($i = 0; $i & lt; 4; $i++) {
    $char = mt_rand(0, 9);
    $string.= $char;
    $y = mt_rand(0, 10);
    $ccolor = imagecolorallocate($im, mt_rand(0, 230) , mt_rand(0, 230) , mt_rand(0, 230));
    imagechar($im, 6, $x, $y, $char, $ccolor); //填充文字
    $x+= mt_rand(10, 15);
}
for ($i = 0; $i{$x1 = mt_rand(0, 80); $x2 = mt_rand(0, 80); $y1 = mt_rand(0, 30); $y2 = mt_rand(0, 30); $x2 = $x1 + mt_rand(1, 5); $y2 = $y1 + mt_rand(1, 5); $lc = imagecolorallocate($im, mt_rand(0, 230) , mt_rand(0, 230) , mt_rand(0, 230)); imageline($im, $x1, $y1, $x2, $y2, $lc); //填充线条
}$_session[&#39;code&#39;] = md5($string); header("content-type:image/jpeg"); imagepng($im); imagedestroy($im);
Copy after login


永久地址:

转载随意~请带上教程地址吧^^

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template