<?php
$digit = $_GET['digit'];
if (!$digit) exit;$im = imagecreate(62,20);
$black = ImageColorAllocate($im, 0,0,0); 
$white =ImageColorAllocate($im, 255,255,255); 
$gray = ImageColorAllocate($im,200,200,200); 
imagefill($im,0,0,$gray);
//&#189;&#171;&#203;&#196;&#206;&#187;&#213;&#251;&#202;&#253;&#209;é&#214;¤&#194;&#235;&#187;&#230;&#200;&#235;&#205;&#188;&#198;&#172; 
imagestring($im, 5, 10, 3, $digit, $black); 
for($i=0;$i<200;$i++)   //&#188;&#211;&#200;&#235;&#184;&#201;&#200;&#197;&#207;ó&#203;&#216; 

    $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
    imagesetpixel($im, rand()%70 , rand()%30 , $randcolor); 

ImagePNG($im); 
ImageDestroy($im); 
?>另:此程序需要GD库的支持

解决方案 »

  1.   

    通过利用gd库画图,和session 技术,session 随机产生数字或字母,然后利用gd 画成图片,然后登陆的时候判断输入的时候与session的一样。
      

  2.   

    http://www.cnblogs.com/mikespook/archive/2005/03/02/111758.html原理+实现
      

  3.   

    了解原理,我有c#代码,---------------
    欢迊来到麦高网-私活兼职首选平台 www.mgao.net
      

  4.   

    回复人: Cain(好好学习,天天向上) ( ) 信誉:105 此君的方法不可取
    如果能够直接获得验证码的字符串内容的话,验证码还有什么存在的意义呢?一般实现方法都是将验证码内容存放在session中
      

  5.   

    贴一个没有修饰的验证码代码 网上找的 我做了大幅度的修改:
    //////////////////////////////////////////////////////////////////////////////
    <?php/***********************************************************************
    pwd.php
    ***********************************************************************/
    class png_pwd{ var $image_x;
    var $image_y; var $pwd_word = 5;
    var $pwd_len;
    var $password ='';
    var $ctctct = 1; function png_pwd()
    {
    $this ->image_x = "45";
    $this ->image_y = "25"; $this ->pwd_word = "4";//密码字号(可选为1-5)
    $this ->pwd_len = "4";//密码长度
    //$this ->pwd_word = imageloadfont ( "./font/georgia.ttf"); }
    function my_rand($min,$max)
    {
    mt_srand((double)microtime()*1000000*getmypid()*$this->ctctct);
    $this->ctctct +=523;
    return mt_rand($min,$max);
    }
    //生成随机字符串函数
    function Randstr()
    {
    $chars='0123456789';
    // characters to build the password from
    // seed the random number generater (must be done)
    //mt_srand((double)microtime()*1000000*getmypid());
    return substr($chars,($this->my_rand(0,strlen($chars)-1)),1);
    } //创建图片
    function createImage()
    {
    $pwd = '';
    for($i =0;$i<$this->pwd_len;$i++) $pwd .= $this->Randstr();
    $image = imagecreatetruecolor ($this->image_x , $this->image_y);
    //定义背景色
    $background_color = imagecolorallocate($image , 0xFF , 0xFF , 0xFF);
    //定义文字及边框颜色
    $black_color = imagecolorallocate($image , 0x00 , 0x00 , 0x00);
    //定义雪花点颜色
    //$gray_color  = imagecolorallocate($image , 0x80 , 0x80 , 0x80); //生成矩形边框
    imagefilledrectangle ($image , 0 , 0 , $this->image_x-1 , $this->image_y - 1 , $background_color);
    imagerectangle($image , 0 , 3 , $this->image_x - 1 , $this->image_y - 1 , $black_color); //循环生成雪花点
    for ($i = 0 ; $i < 400 ; $i++)
    {
    $gray_color  = imagecolorallocate($image , 128+$this->my_rand(0,128) , 128+$this->my_rand(0,128) , 128+$this->my_rand(0,128));
    imagesetpixel($image , mt_rand(1 , $this->image_x-2) , mt_rand(4 , $this->image_y-2) , $gray_color);
    //echo $gray_color."<br>";
    }
    //把随机字符串输入图片
    for ($i = 0 ; $i < strlen($pwd) ; $i++){
    $font_color = imagecolorallocate($image , $this->my_rand(0,100) , $this->my_rand(0,100) , $this->my_rand(0,100));
    //$rn_char = $this->Randstr();
    imagestring($image , $this->pwd_word , 4+$i*10 , 6 , $pwd{$i} , $font_color);
    //$this->password .=$rn_char;
    //echo $rn_char."<br>";
    }
    //exit;
    //return $image;
    session_start();
    $_SESSION["vCode"] = $pwd;
    header("Content-type:image/png");
    Imagepng($image);
    }
    }if(isset($_GET["show"]) && $_GET["show"] == "true")
    {
    //use and show it
    $im = new png_pwd; $im->createImage();
    //$im->show();
    }
    ?>//////////////////////////////////////////////////////////////////////////////在登陆表单中现实这个图片:
    <img src="pwd.php?show=true">//////////////////////////////////////////////////////////////////////////////在登陆验证代码中加入如下的代码:
    if($_POST['vCode']!=$_SESSION['vCode']) {
    //处理验证码不正确
    }
      

  6.   

    $_POST['vCode']
    关于这个 呵呵 忘了说 
    在登陆框中别忘了加一个文本框:
    <input type="text" name="vCode" >
      

  7.   

    TO:ray929(海豚) 是我疏忽了,我承认错误,接受组织的批评。