将 header("Content-type: image/gif");  注释掉运行看有不有报错。

解决方案 »

  1.   

    Notice: Undefined index: verify_code in D:\crmdemo\WebRegistration\wr_image.php on line 27 
    Warning: imagegif(): Unable to open temporary file in D:\crmdemo\WebRegistration\wr_image.php on line 41
    Warning: Unknown: open(C:\Windows\Temp\sess_6csjrgjmqhskptco5rr6rf5601, O_RDWR) failed: Permission denied (13) in Unknown on line 0 
    Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (C:\Windows\Temp) in Unknown on line 0 
      

  2.   

    $verify_code=trim($_SESSION['verify_code']); 这一句,$_SESSION['verify_code'] 为空,导致不能生成图片。1.生成图片的流程也是怪,为什么验证码是从session中读去再生成。而不是应该先生成,然后再写到session使用吗?
    2.function Show(){} 这个完全就没用,__construct时就已经生成图片了。改了一下,
    1.主要程序放在show中调用。
    2.增加了genCode用与生成验证码<?php
    //+------------------------------------------------------------------+
    //| WebRegistration: Captcha                                         |
    //+------------------------------------------------------------------+
    class CImage{    private $num = 0;    public function __construct($num=4){ // 加一个参数,设定验证码长度
            ob_clean();
            $this->num = $num;
        }    public function Show(){
            header("Content-type: image/gif");
            header ("Expires: Mon, 1 Jun 1999 01:00:00 GMT");
            //----
            if(!function_exists('imagecreate')){
                readfile('i/0.gif');
                exit;
            }        @session_start();        //----
            $im=imagecreate(59,22);
            //----
            $bg   =imagecolorallocate($im,0xff,0xff,0xff);
            $black=imagecolorallocate($im,0x62,0x63,0x63);
            //----
            $verify_code = $this->genCode();         // 生成验证码
            $_SESSION['verify_code'] = $verify_code; // 写入session,使用时读取$_SESSION['verify_code']        $len =strlen($verify_code);
            //----
            $x=3; $y=3;
            $w=imagefontwidth(5);
            mt_srand($this->MakeSeed());
            //----
            for($i=0;$i<$len;++$i){
                imagestring($im,5,$x,$y+mt_rand(0,4)-2,$verify_code[$i],$black);
                //----
                $x+=$w;
            }
            //---- output the image
            imagegif($im);
            //----
            exit;
        }   private function MakeSeed(){
          list($usec,$sec)=explode(' ',microtime());
          return((float)$sec+((float)$usec*100000));
       }   // 生成指定长度的验证码
       private function genCode(){
            $hash = '';
            $chars = 'ABCDEFGHIJKLMNPQRSTUVWXYZ23456789';
            $max = strlen($chars) - 1;
            for($i = 0; $i < $this->num; $i++) {
                $hash .= $chars[mt_rand(0, $max)];
            }
            return $hash;
       }}//--- show captcha
    $image=new CImage();
    $image->Show();
    //+------------------------------------------------------------------+
    ?>