Loading...

    AD: 猛买 | 快递查询 | Jobsdigg | 很棒的男装店

Random Confirm Code

可任意转载,但必须在醒目位置以超链接形式标明文章原始出处和作者信息
原文地址:http://www.blogkid.net/archives/556.html

A recent project required confirm code to prevent spam. But I knew nothing about it when I got the design. After about a hour, I am able to write a short php script to finish this work now.

Firstly you have to install the GDlib of php–in linux, type “apt-get install php5gd” and it will be OK in few seconds. Then use “apache2 -k restart” to restart the web sevrice. With all above done, you can begin coding at once.

Here’s my code:

< ?
session_start();
session_register("str");

srand((double)microtime()*1000000);
$cur_code = rand(1000,9999);//get a random number

$im=imagecreate(56,18);

//get colors
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
$gray = ImageColorAllocate($im, 200,200,200);
imagefill($im,68,30,$gray);

imagestring($im, 5, 8, 3, $cur_code, $black);//write string to the picture
for($i=0;$i<50;$i++) //fill in some small pixes
{
imagesetpixel($im, rand()%70 , rand()%30 , $black);
}
$str=md5($cur_code); //store the hashed code in session var

Header("Content-type: image/PNG"); //send header type
ImagePNG($im); //output img
ImageDestroy($im);

?>

It is really easier when succeeded once. Thus, I can write some words beside the picture, like Livid :”Confirm code designed only for human beings”.

I made some progresses again. I’m happy for I’m learning more and more.

0 Responses to “Random Confirm Code”


  1. No Comments

Leave a Reply