PHP 出力セッションの検証コードが画像と同期していません。画像は常に 1 つ先を行っています。解決してください。解決

WBOY
リリース: 2016-06-13 13:35:44
オリジナル
867 人が閲覧しました

PHP 出力セッションの検証コードが画像と同期していません。画像は常に 1 つ先を行っています。解決してください。
PHP 出力セッション検証コードが画像と同期していません。画像は常に 1 段階速くなります。解決してください。

PHP コード
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<?
session_start();
function random($len)
{
$srcstr="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
mt_srand();
$strs="";
for($i=0;$i<$len;$i++){
$strs.=$srcstr[mt_rand(0,35)];
}
return strtoupper($strs);
}
$str=random(4); 
$width = 50; 
$height = 25; 
@header("Content-Type:image/png");
$_SESSION["captcha"] = $str;
//echo $str;
$im=imagecreate($width,$height);
$back=imagecolorallocate($im,0xFF,0xFF,0xFF);
$pix=imagecolorallocate($im,187,230,247);
$font=imagecolorallocate($im,41,163,238);
mt_srand();
for($i=0;$i<1000;$i++)
{
imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pix);
}
imagestring($im, 5, 7, 5,$str, $font);
imagerectangle($im,0,0,$width-1,$height-1,$font);
imagepng($im);
imagedestroy($im);
$_SESSION["captcha"] = $str;
?>


ログイン後にコピー


PHP コード
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<img src="captcha.php" width="60" height="22" border="1" onclick= "this.src='captcha.php?act=captcha&'+Math.random()" style="cursor: pointer; vertical-align:middle" title="看不清?点击更换!" />

ログイン後にコピー


------解決策解決策----------------------
この問題は非常に奇妙であり、最善の方法は分離することです。
これを試してください
checkcode.class.php
PHP コード

<?php
/*** 確認コードの生成
 ※クラス利用について
 * $checkcode = 新しいチェックコード();
 * $checkcode->doimage();
 * //認証を取得する
 * $_SESSION['code']=$checkcode->get_code();
    セッション開始();
    './checkcode.class.php' を含めます;
    $checkcode = 新しいチェックコード('C:WINDOWSFontsARIAL.TTF');
    $checkcode->doimage();
    $_SESSION['code']=$checkcode->get_code();*/
クラスチェックコード {
    //検証コードの幅
    パブリック $width=130;
    
    //検証コードが高い
    パブリック $height=50;
    
    //フォントのアドレスを設定する
    プライベート $font;
    
    //フォントの色を設定する
    パブリック $font_color;
    
    //ランダム生成係数を設定する
    public $charset = 'abcdefghkmnprstuvwyzABCDEFGHKLMNPRSTUVWYZ23456789';
    
    //背景色を設定する
    パブリック $background = '#EDF7FF';
    
    //認証コードの文字番号を生成
    パブリック $code_len = 4;
    
    //フォントサイズ
    パブリック $font_size = 20;
    
    //検証コード
    プライベート $コード;
    
    //画像メモリ
    プライベート $img;
    
    //テキストの X 軸の開始位置
    プライベート $x_start;
        
    関数 __construct($fontpath) {
        $this->font =$fontpath;
    }
    /*** ランダムな認証コードを生成します。*/
    保護された関数 creat_code() {
        $コード = '';
        $charset_len = strlen($this->charset)-1;
        for ($i=0; $icode_len; $i++) {
            $code .= $this->charset[rand(1, $charset_len)];
        }
        $this->code = $code;
    }
    
    /*** 認証コードを取得する*/
    パブリック関数 get_code() {
        return strto lower($this->code);
    }
    
    /*** 画像の生成*/
    パブリック関数 doimage() {
        $code = $this->creat_code();
        $this->img = imagecreatetruecolor($this->幅, $this->高さ);
        if (!$this->font_color) {
            $this->font_color = imagecolorallocate($this->img, rand(0,156), rand(0,156), rand(0,156));
        } それ以外 {
            $this->font_color = imagecolorallocate($this->img, hexdec(substr($this->font_color, 1,2)), hexdec(substr($this->font_color, 3,2)), hexdec(substr($this->font_color, 5,2)));
        }
        //背景色を設定する
        $background = imagecolorallocate($this->img,hexdec(substr($this->background, 1,2)),hexdec(substr($this->background, 3,2)),hexdec(substr( $this->background, 5,2)));
        //キャビネットの形状を描画し、背景色を設定します。
        imagefilledrectangle($this->img,0, $this->height, $this->width, 0, $background);
        $this->creat_font();
        $this->creat_line();
        $this->output();
    }
    
    /*** テキストの生成*/
    プライベート関数 creat_font() {
        $x = $this->width/$this->code_len;
        for ($i=0; $icode_len; $i++) {
            imagettftext($this->img, $this->font_size, rand(-30,30), $x*$i+rand(0,5), $this->height/1.4, $this-> ;font_color, $this->font, $this->code[$i]);
            if($i==0)$this->x_start=$x*$i+5;
        }
    }
    
    /*** 線を引く*/
    プライベート関数 creat_line() {
        画像の厚さ($this->img, 3);
        $xpos = ($this->font_size * 2) + rand(-5, 5);
        $width = $this->width / 2.66 + rand(3, 10);
        $height = $this->font_size * 2.14;
    
        if ( rand(0,100) % 2 == 0 ) {
          $start = rand(0,66);
          $ypos = $this->height / 2 - rand(10, 30);
          $xpos += ランド(5, 15);
        } それ以外 {
          $start = rand(180, 246);
          $ypos = $this->height / 2 + rand(10, 30);
        }
    
        $end = $start + rand(75, 110);
    
        imagearc($this->img, $xpos, $ypos, $width, $height, $start, $end, $this->font_color);
        
        if ( rand(1,75) % 2 == 0 ) {
          $start = rand(45, 111);
          $ypos = $this->height / 2 - rand(10, 30);
          $xpos += ランド(5, 15);
        } それ以外 {
          $start = rand(200, 250);
          $ypos = $this->height / 2 + rand(10, 30);
        }
    
        $end = $start + rand(75, 100);
    
        imagearc($this->img, $this->width * .75, $ypos, $width, $height, $start, $end, $this->font_color);
    }
    
    /***出力画像*/
    プライベート関数の出力() {
        header("コンテンツタイプ:画像/pngrn");
        imagepng($this->img);
        imagedestroy($this->img);
    }
<div class="clear"></div>
ログイン後にコピー
関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!