PHP Verification Code Program Verification Code code PHP PHP picture Verification Code
<?php
/*
@date 20080514
@author Hluan
Session_Start ();
Class Icode
{
function __construct () {
Header (' content-type:image/png ');
}
function __destruct () {
Imagedestroy ($png);
}
function GetCode ($len, $type) {
Switch ($type) {
Case 1;
$str = "1234567890";
Break
Case 2;
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Break
Case 0;
$str = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Break
}
$result = "";
$length = strlen ($str)-1;
$num = 0;
for ($i =0; $i < $len; $i + +) {
$num = rand (0, $length);
$a = $str [$num];
$result = $result. $a;
}
/*before using it,you should use Session_Start () * *
$_session[' icode ' = $result;//store in session.
echo "session:". $_session[' Icode '];
Die ();
$png = Imagecreate (60,30);
$white = Imagecolorallocate ($png, 255,255,255);//background
$red = Imagecolorallocate ($png, 255,0,0);
$blue = Imagecolorallocate ($png, 0,0,255);
$brown = Imagecolorallocate ($png, 100,0,0);
$black = Imagecolorallocate ($png, 0,0,0);//identifying Code
Imagefill ($png, 0,0, $white);
$k = rand (0,3);
if (0 = $k) {
For ($n =0 $n <60; $n + +) {
$y = 15*sin ($n/30*pi ());
Imagesetpixel ($png, $n, 15+ $y, $red);
}
}else if (1 = $k) {
For ($n =0 $n <60; $n + +) {
$y = 15*cos ($n/60*pi ());
Imagesetpixel ($png, $n, 15+ $y, $red);
}
}else if (2 = $k) {
For ($n =0 $n <60; $n + +) {
$y = 15*sin ($n/45*pi ());
Imagesetpixel ($png, $n, 15+ $y, $brown);
}
}else if (3 = $k) {
For ($n =0 $n <60; $n + +) {
$y = 15*cos ($n/30*pi ());
Imagesetpixel ($png, $n, 15+ $y, $brown);
}
}
For ($k =0 $k <10; $k + +) {
$i = rand (3,60);//width
$j = rand (3,15);//height
Imageline ($png, $i -3, $j -3, $i, $j, $black);
}
Imagestring ($png, 5,3,5, $result, $blue);
$icode = Imagepng ($png);
return $icode;
}
}
/* Test Code
$icode = new Icode ();
$ICP = $icode->getcode (6,0);
Echo $ICP;
*/