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 varHeader("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”