2

嘿,我通过制作验证码遇到了一些疯狂的问题。

我创建了一个 shuffle 变量并将其提供给会话,然后我打开 image.php

验证码.php

<?php 
error_reporting(-1);
ini_set('display_errors', 1);

// Session start
SESSION_START();


// delet old CAPTCHA-Code from Session 
unset( $_SESSION["captcha"] );



// the Array
$text[] = "2";
$text[] = "4";
$text[] = "5";
$text[] = "7";
$text[] = "8";
$text[] = "A";
$text[] = "B";
$text[] = "C";
$text[] = "D";
$text[] = "E";
$text[] = "F";
$text[] = "G";
$text[] = "H";
$text[] = "J";
$text[] = "K";
$text[] = "M";
$text[] = "N";
$text[] = "P";
$text[] = "Q";
$text[] = "R";
$text[] = "S";
$text[] = "T";
$text[] = "U";
$text[] = "V";
$text[] = "W";
$text[] = "X";
$text[] = "Y";
$text[] = "Z";

//mixup array
shuffle ( $text );

//getting the first 5 Charakters
$outputtext = array_slice ($text, 0, 5);


//give it to the session
$_SESSION["captcha"] = implode($outputtext);
session_write_close();

echo "Captcha ist: ".$_SESSION["captcha"];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

 <img src="captchaimage.php" alt="" style="vertical-align: middle;">

</html>

验证码图片.php

<?php

error_reporting(-1);
ini_set('display_errors', 1);


SESSION_START();

Header ("Content-type: image/png");

$str = $_SESSION["captcha"];



$bild = ImageCreateFromPNG ("http://www.php.de/images/captcha.logo1.png");


$farbe_w = ImageColorAllocate ($bild, 255, 255, 255);
$farbe_b = ImageColorAllocate ($bild, 0, 0, 0);


ImageTTFText ($bild, 32, -30, 224,  112, $farbe_w, "arial.ttf",
              $str[0]);
ImageTTFText ($bild, 32, -90, 288, 208, $farbe_w, "arial.ttf",
              $str[1]);
ImageTTFText ($bild, 32,   0, 208, 336, $farbe_w, "arial.ttf",
              $str[2]);
ImageTTFText ($bild, 32,  45,  128, 304, $farbe_w, "arial.ttf",
              $str[3]);
ImageTTFText ($bild, 32,  99,  128, 192, $farbe_w, "arial.ttf",
              $str[4]);


header( "Expires: Mon, 26 Jul 1997 05: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" ); 



ImagePng     ($bild);


ImageDestroy ($bild);

?>

这工作正常,但如果我从 captcha.php 中删除回声

这一行:echo "Captcha ist: ".$_SESSION["captcha"];

我得到一个没有字符的图像,比如会话是空的,但如果会话真的是空的,图像将不会被创建。

如果我在 captcha.php 中写了 echo 行,为什么要让 captchaimage.php 只获得正确的会话?

4

1 回答 1

2

一个小测试:

稍微更改一下代码 - 不要调用 captchaimage.php 作为您的图像的来源,而是尝试以这种方式将您的 captchaimage.php 逻辑移动到 captcha.php 中:

验证码.php

<?php 
error_reporting(-1);
ini_set('display_errors', 1);

// Session start
SESSION_START();

// delet old CAPTCHA-Code from Session 
unset( $_SESSION["captcha"] );

// the Array
$text = array("2", "4", "5", "7", "8", "A", "B", "C", "D", "E", /*...*/);

//mixup array
shuffle ( $text );

//getting the first 5 Charakters
$outputtext = array_slice ($text, 0, 5);

//give it to the session
$_SESSION["captcha"] = implode($outputtext);

$bild = ImageCreateFromPNG ("http://www.php.de/images/captcha.logo1.png");

$farbe_w = ImageColorAllocate ($bild, 255, 255, 255);
$farbe_b = ImageColorAllocate ($bild, 0, 0, 0);

ImageTTFText ($bild, 32, -30, 224,  112, $farbe_w, "arial.ttf", $str[0]);
ImageTTFText ($bild, 32, -90, 288, 208, $farbe_w, "arial.ttf", $str[1]);
ImageTTFText ($bild, 32,   0, 208, 336, $farbe_w, "arial.ttf", $str[2]);
ImageTTFText ($bild, 32,  45,  128, 304, $farbe_w, "arial.ttf", $str[3]);
ImageTTFText ($bild, 32,  99,  128, 192, $farbe_w, "arial.ttf", $str[4]);

ob_start();
ImagePng     ($bild);
$image = ob_get_clean();

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

 <img src="data:image/png;base64,<?php echo base64_encode($image); ?>" alt="" style="vertical-align: middle;">

</html>

我希望这会奏效。

于 2012-09-11T09:26:41.163 回答