小弟疯了一天了,还没解决问题,请大虾赐教
我用下载的securimage来显示验证码,按理说这代码是没有问题的,然后我查了GD库也是打开的,运行phpinfo()可以看到是支持GD库了,但是验证码就是显示不出来,都是XX
另外我的系统是vista,查注册表也是支持验证码的,我在此系统下运行java代码的验证码页面都没有问题

解决方案 »

  1.   

    example_form.php
    <?php
    session_start();
    ?>
    <html>
    <head>
      <title>Securimage Test Form</title>
    </head><body><?php
    if (empty($_POST)) { ?>
    <form method="POST">
    Username:<br />
    <input type="text" name="username" /><br />
    Password:<br />
    <input type="text" name="password" /><br /><!-- pass a session id to the query string of the script to prevent ie caching -->
    <img src="securimage_show.php?sid=<?php echo md5(uniqid(time())); ?>"><br />
    <input type="text" name="code" /><br /><input type="submit" value="Submit Form" />
    </form><?php
    } else { //form is posted
      include("securimage.php");
      $img = new Securimage();
      $valid = $img->check($_POST['code']);  if($valid == true) {
        echo "<center>Thanks, you entered the correct code.</center>";
      } else {
        echo "<center>Sorry, the code you entered was invalid.  <a href=\"javascript:history.go(-1)\">Go back</a> to try again.</center>";
      }
    }?></body>
    </html>其余代码就不放了,太大了,怕你们看得眼花,是Securimage组件来的,专门提供php下的验证码显示,代码应该不会有问题的,这么多人用它
      

  2.   

    多数是路径的问题 多试试
    <img src="**/**/**/identify.php" style="vertical-align:middle;" id="img_code" onclick="this.src=this.src+'?'">
      

  3.   

    securimage_show.php?sid=d74052e34804b9d5499e72253ec237df
    粘贴到浏览器中 回车看看 没有图的话 还是路径问题
      

  4.   

    <?php
    } else { //form is posted
      include("securimage.php");
      $img = new Securimage();
      $valid = $img->check($_POST['code']);  if($valid == true) {
      echo "<center>Thanks, you entered the correct code.</center>";
      } else {
      echo "<center>Sorry, the code you entered was invalid. <a href=\"javascript:history.go(-1)\">Go back</a> to try again.</center>";
      }
    }?>图片输出是不能有其他东西输出的,只能单独输出图片;
      

  5.   

    给你一个现成的,直接保存成php文件,然后<img src="?.php">调用即可
    <?php
    /**
    * 验证码类
    * 注:需要GD库支持
    */
    Session_start();
    Class Coder{
    Private $config;
    Private $im;
    Private $str; Function __construct(){
    $this->config['width']=50;
    $this->config['height']=20;
    $this->config['boxline']=False;
    $this->config['codname']="coder";
    $this->config['type']="int";
    $this->config['length']=4;
    $this->config['color']=array(246,246,246);
    $this->config['interfere']=3; $this->str['default']="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $this->str['string']="abcdefghijklmnopqrstuvwxyz";
    $this->str['int']="0123456789";
    } //配置
    Public Function init($config=array()){
    IF(!empty($config) && is_array($config)){
    Foreach($config as $key=>$value){
    $this->config[$key]=$value;
    }
    }
    } //输出验证码
    Public Function create(){
    IF(!Function_exists("Imagecreate")){
    Return False;
    }
    $this->create_do();
    } //创建
    Private Function create_do(){
    $this->im=Imagecreate($this->config['width'],$this->config['height']);
    //设置背景为白色
    Imagecolorallocate($this->im,$this->config['color'][0],$this->config['color'][1],$this->config['color'][2]); //为此背景加个边框
    IF($this->config['boxline']==True){
    $bordercolor=Imagecolorallocate($this->im,37,37,37);
    Imagerectangle($this->im,0,0,$this->config['width']-1,$this->config['height']-1,$bordercolor);
    } //生成验证码
    $this->create_str();
    $coder=$_SESSION[$this->config['codname']]; //输入文字
    $fontcolor=Imagecolorallocate($this->im,46,46,46);
    For($i=0;$i<$this->config['length'];$i++){
    Imagestring($this->im,5,$i*10+6,rand(2,5),$coder[$i],$fontcolor);
    } //加入干扰线
    $interfere=$this->config['interfere'];
    $interfere=$interfere>30?"30":$interfere;
    IF(!empty($interfere) && $interfere>1){
    For($i=1;$i<$interfere;$i++){
    $linecolor=Imagecolorallocate($this->im,rand(0,255),rand(0,255),rand(0,255));
    $x=rand(1,$this->config['width']);
    $y=rand(1,$this->config['height']);
    $x2=rand($x-10,$x+10);
    $y2=rand($y-10,$y+10);
    Imageline($this->im,$x,$y,$x2,$y2,$linecolor);
    }
    } Header("Pragma:no-cache\r\n");
    Header("Cache-Control:no-cache\r\n");
    Header("Expires:0\r\n");
    Header("Content-type:Image/jpeg\r\n");
    Imagejpeg($this->im);
    Imagedestroy($this->im);
    Exit;
    } //得到验证码
    Private Function create_str(){
    IF($this->config['type']=="int"){
    For($i=1;$i<=$this->config['length'];$i++){
    $coder.=Rand(0,9);
    }
    $_SESSION[$this->config['codname']]=$coder;
    Return True;
    }
    $len=strlen($this->str[$this->config['type']]);
    IF(!$len){
    $this->config['type']="default";
    $this->create_str();
    }
    For($i=1;$i<=$this->config['length'];$i++){
    $offset=rand(0,$len-1);
    $coder.=substr($this->str[$this->config['type']],$offset,1);
    }
    $_SESSION[$this->config['codname']]=$coder;
    Return True;
    }
    }/**
    * 默认验证码SESSION为:$_SESSION['coder'];
    * 注意在给变量符值时不要把变量的名子和SESSION冲突
    * 注:在验证时不分大小写
    */
    $v = New Coder;
    //$config['width'] = 50; //验证码宽
    //$config['height'] = 20; //验证码高
    //$config['boxline'] = True; //是否有边框线
    //$config['codname'] = "coder"; //检查验证码时用的SESSION
    //$config['type'] = "default"; //验证码展示的类型default:大写字母,string:小写字母,int:数字
    //$config['length'] = 4; //验证码长度
    //$config['color']=array(246,246,246); //背景色,RGB颜色值
    //$config['interfere']= 0; //干扰线强度,范围为1-30,0或空为不起用干扰线
    //$v->init($config); //将配置信息写入
    $v->create();
    ?>
      

  6.   

    9楼兄弟给的有用,不知我这个问题出在哪了
    securimage_show.php<?php
    include 'securimage.php';
    $img = new securimage();
    $img->show(); // alternate use:  $img->show('/path/to/background.jpg');
    ?>securimage.php<?php
    define('SI_IMAGE_JPEG', 1);
    define('SI_IMAGE_PNG',  2);
    define('SI_IMAGE_GIF',  3);
    class Securimage {
      var $image_width = 175;
      var $image_height = 45;
      var $image_type = SI_IMAGE_PNG;
      var $code_length = 4;
      var $charset = 'ABCDEFGHKLMNPRSTUVWYZ23456789';
      var $wordlist_file = '../words/words.txt';
      var $use_wordlist  = true;
      var $use_gd_font = false;
      var $gd_font_file = 'gdfonts/bubblebath.gdf';
      var $gd_font_size = 20;
      var $ttf_file = "./elephant.ttf";
      var $font_size = 24;
      var $text_angle_minimum = -20;
      var $text_angle_maximum = 20;
      var $text_x_start = 8;
      var $text_minimum_distance = 30;
      var $text_maximum_distance = 33;
      var $image_bg_color = "#e3daed";
      var $text_color = "#ff0000";
      var $use_multi_text = true;
      var $multi_text_color = "#0a68dd,#f65c47,#8d32fd";
      var $use_transparent_text = true;
      var $text_transparency_percentage = 15;
      var $draw_lines = true;
      var $line_color = "#80BFFF";
      var $line_distance = 5;
      var $line_thickness = 1;
      var $draw_angled_lines = false;
      var $draw_lines_over_text = false;
      var $arc_linethrough = true;
      var $arc_line_colors = "#8080ff";
      var $audio_path = './audio/';
      var $im;
      var $bgimg;
      var $code;
      var $code_entered;
      var $correct_code;
      function Securimage()
      {
        if ( session_id() == '' ) {
          session_start();
        }
      }
      function show($background_image = "")
      {
        if($background_image != "" && is_readable($background_image)) {
          $this->bgimg = $background_image;
        }
        $this->doImage();
      }
      function check($code)
      {
        $this->code_entered = $code;
        $this->validate();
        return $this->correct_code;
    }
      function doImage()
      {
        if($this->use_transparent_text == true || $this->bgimg != "") {
          $this->im = imagecreatetruecolor($this->image_width, $this->image_height);
          $bgcolor = imagecolorallocate($this->im, hexdec(substr($this->image_bg_color, 1, 2)), hexdec(substr($this->image_bg_color, 3, 2)), hexdec(substr($this->image_bg_color, 5, 2)));
          imagefilledrectangle($this->im, 0, 0, imagesx($this->im), imagesy($this->im), $bgcolor);
        } else { //no transparency
          $this->im = imagecreate($this->image_width, $this->image_height);
          $bgcolor = imagecolorallocate($this->im, hexdec(substr($this->image_bg_color, 1, 2)), hexdec(substr($this->image_bg_color, 3, 2)), hexdec(substr($this->image_bg_color, 5, 2)));
        }
        if($this->bgimg != "") { $this->setBackground(); }
        $this->createCode();
        if (!$this->draw_lines_over_text && $this->draw_lines) $this->drawLines();
        $this->drawWord();
        if ($this->arc_linethrough == true) $this->arcLines();
        if ($this->draw_lines_over_text && $this->draw_lines) $this->drawLines();
        $this->output();
      }
      function setBackground()
      {
        $dat = @getimagesize($this->bgimg);
        if($dat == false) { return; }
        switch($dat[2]) {
          case 1:  $newim = @imagecreatefromgif($this->bgimg); break;
          case 2:  $newim = @imagecreatefromjpeg($this->bgimg); break;
          case 3:  $newim = @imagecreatefrompng($this->bgimg); break;
          case 15: $newim = @imagecreatefromwbmp($this->bgimg); break;
          case 16: $newim = @imagecreatefromxbm($this->bgimg); break;
          default: return;
        }
        if(!$newim) return;
        imagecopy($this->im, $newim, 0, 0, 0, 0, $this->image_width, $this->image_height);
      }
      function arcLines()
      {
        $colors = explode(',', $this->arc_line_colors);
        imagesetthickness($this->im, 3);
        $color = $colors[rand(0, sizeof($colors) - 1)];
        $linecolor = imagecolorallocate($this->im, hexdec(substr($color, 1, 2)), hexdec(substr($color, 3, 2)), hexdec(substr($color, 5, 2)));
        $xpos   = $this->text_x_start + ($this->font_size * 2) + rand(-5, 5);
        $width  = $this->image_width / 2.66 + rand(3, 10);
        $height = $this->font_size * 2.14 - rand(3, 10);
        if ( rand(0,100) % 2 == 0 ) {
          $start = rand(0,66);
          $ypos  = $this->image_height / 2 - rand(5, 15);
          $xpos += rand(5, 15);
        } else {
          $start = rand(180, 246);
          $ypos  = $this->image_height / 2 + rand(5, 15);
        }
        $end = $start + rand(75, 110);
        imagearc($this->im, $xpos, $ypos, $width, $height, $start, $end, $linecolor);
        $color = $colors[rand(0, sizeof($colors) - 1)];
        $linecolor = imagecolorallocate($this->im, hexdec(substr($color, 1, 2)), hexdec(substr($color, 3, 2)), hexdec(substr($color, 5, 2)));    if ( rand(1,75) % 2 == 0 ) {
          $start = rand(45, 111);
          $ypos  = $this->image_height / 2 - rand(5, 15);
          $xpos += rand(5, 15);
        } else {
          $start = rand(200, 250);
          $ypos  = $this->image_height / 2 + rand(5, 15);
        }    $end = $start + rand(75, 100);
        imagearc($this->im, $this->image_width * .75, $ypos, $width, $height, $start, $end, $linecolor);
      }
      function drawLines()
      {
        $linecolor = imagecolorallocate($this->im, hexdec(substr($this->line_color, 1, 2)), hexdec(substr($this->line_color, 3, 2)), hexdec(substr($this->line_color, 5, 2)));
        imagesetthickness($this->im, $this->line_thickness);
        for($x = 1; $x < $this->image_width; $x += $this->line_distance) {
          imageline($this->im, $x, 0, $x, $this->image_height, $linecolor);
        }
        for($y = 11; $y < $this->image_height; $y += $this->line_distance) {
          imageline($this->im, 0, $y, $this->image_width, $y, $linecolor);
        }
        if ($this->draw_angled_lines == true) {
          for ($x = -($this->image_height); $x < $this->image_width; $x += $this->line_distance) {
            imageline($this->im, $x, 0, $x + $this->image_height, $this->image_height, $linecolor);
          }
          for ($x = $this->image_width + $this->image_height; $x > 0; $x -= $this->line_distance) {
            imageline($this->im, $x, 0, $x - $this->image_height, $this->image_height, $linecolor);
          }
        }
      }  
      

  7.   

    代码太长,恢复限制,这里补
    function drawWord()
      {
        if ($this->use_gd_font == true) {
          if (!is_int($this->gd_font_file)) { //is a file name
            $font = @imageloadfont($this->gd_font_file);
            if ($font == false) {
              trigger_error("Failed to load GD Font file {$this->gd_font_file} ", E_USER_WARNING);
              return;
            }
          } else { //gd font identifier
            $font = $this->gd_font_file;
          }
          $color = imagecolorallocate($this->im, hexdec(substr($this->text_color, 1, 2)), hexdec(substr($this->text_color, 3, 2)), hexdec(substr($this->text_color, 5, 2)));
          imagestring($this->im, $font, $this->text_x_start, ($this->image_height / 2) - ($this->gd_font_size / 2), $this->code, $color);    } else {  
          if($this->use_transparent_text == true) {
            $alpha = intval($this->text_transparency_percentage / 100 * 127);
            $font_color = imagecolorallocatealpha($this->im, hexdec(substr($this->text_color, 1, 2)), hexdec(substr($this->text_color, 3, 2)), hexdec(substr($this->text_color, 5, 2)), $alpha);
          } else {
            $font_color = imagecolorallocate($this->im, hexdec(substr($this->text_color, 1, 2)), hexdec(substr($this->text_color, 3, 2)), hexdec(substr($this->text_color, 5, 2)));
          }
          $x = $this->text_x_start;
          $strlen = strlen($this->code);
          $y_min = ($this->image_height / 2) + ($this->font_size / 2) - 2;
          $y_max = ($this->image_height / 2) + ($this->font_size / 2) + 2;
          $colors = explode(',', $this->multi_text_color);
          for($i = 0; $i < $strlen; ++$i) {
            $angle = rand($this->text_angle_minimum, $this->text_angle_maximum);
            $y = rand($y_min, $y_max);
            if ($this->use_multi_text == true) {
              $idx = rand(0, sizeof($colors) - 1);
              $r = substr($colors[$idx], 1, 2);
              $g = substr($colors[$idx], 3, 2);
              $b = substr($colors[$idx], 5, 2);
              if($this->use_transparent_text == true) {
                $font_color = imagecolorallocatealpha($this->im, "0x$r", "0x$g", "0x$b", $alpha);
              } else {
                $font_color = imagecolorallocate($this->im, "0x$r", "0x$g", "0x$b");
              }
            }
            @imagettftext($this->im, $this->font_size, $angle, $x, $y, $font_color, $this->ttf_file, $this->code{$i});        $x += rand($this->text_minimum_distance, $this->text_maximum_distance);
          } 
        } 
      } 
      function createCode()
      {
        $this->code = false;
        if ($this->use_wordlist && is_readable($this->wordlist_file)) {
          $this->code = $this->readCodeFromFile();
        }
        if ($this->code == false) {
          $this->code = $this->generateCode($this->code_length);
        }    $this->saveData();
      }
      function generateCode($len)
      {
        $code = '';
        for($i = 1, $cslen = strlen($this->charset); $i <= $len; ++$i) {
          $code .= strtoupper( $this->charset{rand(0, $cslen - 1)} );
        }
        return $code;
      }
      function readCodeFromFile()
      {
        $fp = @fopen($this->wordlist_file, 'rb');
        if (!$fp) return false;
        $fsize = filesize($this->wordlist_file);
        if ($fsize < 32) return false;    
         if ($fsize < 128) {
          $max = $fsize;
        } else {
          $max = 128;
        }
        fseek($fp, rand(0, $fsize - $max), SEEK_SET);
        $data = fread($fp, 128); // read a random 128 bytes from file
        fclose($fp);
        $data = preg_replace("/\r?\n/", "\n", $data);
        $start = strpos($data, "\n", rand(0, 100)) + 1; // random start position
        $end   = strpos($data, "\n", $start);           // find end of word
        return strtolower(substr($data, $start, $end - $start)); // return substring in 128 bytes
      }
      function output()
      {
        header("Expires: Sun, 1 Jan 2000 12:00:00 GMT");
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
        header("Cache-Control: no-store, no-cache, must-revalidate");
        header("Cache-Control: post-check=0, pre-check=0", false);
        header("Pragma: no-cache");
        switch($this->image_type)
        {
          case SI_IMAGE_JPEG:
            header("Content-Type: image/jpeg");
            imagejpeg($this->im, null, 90);
            break;
          case SI_IMAGE_GIF:
            header("Content-Type: image/gif");
            imagegif($this->im);
            break;      default:
            header("Content-Type: image/png");
            imagepng($this->im);
            break;
        }
        imagedestroy($this->im);
      }
      function getAudibleCode()
      {
        $letters = array();
        $code    = $this->getCode();    if ($code == '') {
          $this->createCode();
          $code = $this->getCode();
        }
        for($i = 0; $i < strlen($code); ++$i) {
          $letters[] = $code{$i};
        }
        return $this->generateWAV($letters);
      }
      function saveData()
      {
        $_SESSION['securimage_code_value'] = strtolower($this->code);
      }
      function validate()
      {
        if ( isset($_SESSION['securimage_code_value']) && !empty($_SESSION['securimage_code_value']) ) {
          if ( $_SESSION['securimage_code_value'] == strtolower(trim($this->code_entered)) ) {
            $this->correct_code = true;
            $_SESSION['securimage_code_value'] = '';
          } else {
            $this->correct_code = false;
          }
        } else {
          $this->correct_code = false;
        }
      }
      function getCode()
      {
        if (isset($_SESSION['securimage_code_value']) && !empty($_SESSION['securimage_code_value'])) {
          return $_SESSION['securimage_code_value'];
        } else {
          return '';
        }
      }
      function checkCode()
      {
        return $this->correct_code;
      }
      function generateWAV($letters)
      {
        $first = true; 
        $data_len    = 0;
        $files       = array();
        $out_data    = '';
        foreach ($letters as $letter) {
          $filename = $this->audio_path . strtoupper($letter) . '.wav';
          $fp = fopen($filename, 'rb');
          $file = array();
          $data = fread($fp, filesize($filename)); // read file in
          $header = substr($data, 0, 36);
          $body   = substr($data, 44);
          $data = unpack('NChunkID/VChunkSize/NFormat/NSubChunk1ID/VSubChunk1Size/vAudioFormat/vNumChannels/VSampleRate/VByteRate/vBlockAlign/vBitsPerSample', $header);      $file['sub_chunk1_id']   = $data['SubChunk1ID'];
          $file['bits_per_sample'] = $data['BitsPerSample'];
          $file['channels']        = $data['NumChannels'];
          $file['format']          = $data['AudioFormat'];
          $file['sample_rate']     = $data['SampleRate'];
          $file['size']            = $data['ChunkSize'] + 8;
          $file['data']            = $body;      if ( ($p = strpos($file['data'], 'LIST')) !== false) {
                  $info= substr($file['data'], $p + 4, 8);
            $data= unpack('Vlength/Vjunk', $info);
            $file['data'] = substr($file['data'], 0, $p);
            $file['size'] = $file['size'] - (strlen($file['data']) - $p);
          }
          $files[] = $file;
          $data    = null;
          $header  = null;
          $body    = null;
          $data_len += strlen($file['data']);
          fclose($fp);
        }
        $out_data = '';
        for($i = 0; $i < sizeof($files); ++$i) {
          if ($i == 0) { // output header
            $out_data .= pack('C4VC8', ord('R'), ord('I'), ord('F'), ord('F'), $data_len + 36, ord('W'), ord('A'), ord('V'), ord('E'), ord('f'), ord('m'), ord('t'), ord(' '));
            $out_data .= pack('VvvVVvv',
                              16,
                              $files[$i]['format'],
                              $files[$i]['channels'],
                              $files[$i]['sample_rate'],
                              $files[$i]['sample_rate'] * (($files[$i]['bits_per_sample'] * $files[$i]['channels']) / 8),
                              ($files[$i]['bits_per_sample'] * $files[$i]['channels'])/8,
                              $files[$i]['bits_per_sample'] );
            $out_data .= pack('C4', ord('d'), ord('a'), ord('t'), ord('a'));
            $out_data .= pack('V', $data_len);
          }
          $out_data .= $files[$i]['data'];
        }
        return $out_data;
      }
    }?>
      

  8.   

    问题解决了,我在securimage.php这个页面的session_start();前面加了@验证码就显示正常了,但问题又来了,kao,怎么输入验证码提交后都显示不正确,看了securimage.php页面里验证输入正确与否的函数看上去也没有问题啊,郁闷了