求php 驗證碼碼,該怎麼解決

來源:互聯網
上載者:User
求php 驗證碼碼

這個驗證在空間中不能用誰有驗證碼謝謝

//小豬會氣功
session_start();
session_register('code');
$width = "70";//圖片寬 70 63
$height = "25";//圖片高 25 22.5
$len = "4";//產生幾位驗證碼
$bgcolor = "#ffffff";//背景色
$noise = true;//產生雜點
$noisenum = 200;//雜點數量
$border = false;//邊框
$bordercolor = "#000000";
$image = imageCreate($width, $height);
$back = getcolor($bgcolor);
imageFilledRectangle($image, 0, 0, $width, $height, $back);
$size = $width/$len;
if($size>$height) $size=$height;
$left = ($width-$len*($size+$size/10))/$size;
for ($i=0; $i<$len; $i++)
{
$randtext = rand(0, 9);
$code .= $randtext;
$textColor = imageColorAllocate($image, rand(0, 100), rand(0, 100), rand(0, 100));
$font = rand(1,4).".ttf";
$randsize = rand($size-$size/10, $size+$size/10);
$location = $left+($i*$size+$size/10);
imagettftext($image, $randsize, rand(-18,18), $location, rand($size-$size/10, $size+$size/10), $textColor, $font, $randtext);
}
if($noise == true) setnoise();
$_SESSION['code'] = $code;
$bordercolor = getcolor($bordercolor);
if($border==true) imageRectangle($image, 0, 0, $width-1, $height-1, $bordercolor);
header("Content-type: image/png");
imagePng($image);
imagedestroy($image);
function getcolor($color)
{
global $image;
$color = eregi_replace ("^#","",$color);
$r = $color[0].$color[1];
$r = hexdec ($r);
$b = $color[2].$color[3];
$b = hexdec ($b);
$g = $color[4].$color[5];
$g = hexdec ($g);
$color = imagecolorallocate ($image, $r, $b, $g);
return $color;
}
function setnoise()
{
global $image, $width, $height, $back, $noisenum;
for ($i=0; $i<$noisenum; $i++){
$randColor = imageColorAllocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
imageSetPixel($image, rand(0, $width), rand(0, $height), $randColor);
} //出錯
}
?>

------解決方案--------------------

//產生驗證碼圖片
Header("Content-type: image/PNG");
srand((double)microtime()*1000000);
$im = imagecreate(62,20);
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
$gray = ImageColorAllocate($im, 200,200,200);
imagefill($im,68,30,$gray);
while(($authnum=rand()%100000)<10000);
//將四位整數驗證碼繪入圖片
imagestring($im, 5, 10, 3, $authnum, $black);
for($i=0;$i<200;$i++) //加入幹擾象素
{
$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
}
ImagePNG($im);
ImageDestroy($im);
?>


首先得啟動GD,就可以看到效果了.你試試吧!

------解決方案--------------------
我一直用的就是這個。。。。
//產生N位的隨機碼
function getcode()
{
return md5(time().rand(0,10000)).md5(time().rand(0,10000));
}
$code=getcode();
//$code=$_GET["code"];;
$img=imagecreate(185,35);
$bg_color=imagecolorallocate($img,200,200,200);
//背景幹擾點
for($i=0;$i<=200;$i++)
{
$point_color=imagecolorallocate($img,rand(1,255),rand(1,255),rand(1,255));
imagesetpixel($img,rand(1,185),rand(1,35),$point_color);
}
//產生五位驗證碼
for($i=0;$i<=4;$i++)
{
$x=($i+1)*30;
$y=rand(5,15);
$code_n=substr($code,$i,1);//$i需要改的複雜一點,不然取出的只是前五位。
$text_color=imagecolorallocate($img,rand(20,180),rand(20,180),rand(20,180)); //字型顏色
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.