有一个生成验证码的文件--image.php,我现在调用的时候是这样的
<img src="image.php">
image.php如下:
<?php
//小猪会气功
session_start();
session_register('code');
$width = "70";//图片宽
$height = "25";//图片高
$len = "4";//生成几位验证码
$bgcolor = "#ffffff";//背景色
$noise = true;//生成杂点
$noisenum = 200;//杂点数量
$border = false;//边框
$bordercolor = "#000000";
$image = imageCreate($width, $height);
$back = getcolor($bgcolor);
imageFilledRectangle($image, 0, 0, $width, $height, $back);
$size = $width/$len;
if($size>$height) $size=$height;
$left = ($width-$len*($size+$size/10))/$size;
for ($i=0; $i<$len; $i++)
{
        $randtext = rand(0, 9);
        $code .= $randtext;
$textColor = imageColorAllocate($image, rand(0, 100), rand(0, 100), rand(0, 100));
$font = rand(1,4).".ttf"; 
$randsize = rand($size-$size/10, $size+$size/10);
$location = $left+($i*$size+$size/10);
imagettftext($image, $randsize, rand(-18,18), $location, rand($size-$size/10, $size+$size/10), $textColor, $font, $randtext); 
}
if($noise == true) setnoise();
$bordercolor = getcolor($bordercolor); 
if($border==true) imageRectangle($image, 0, 0, $width-1, $height-1, $bordercolor);
header("Content-type: image/png");
$_SESSION['code'] = $code;
imagePng($image);
imagedestroy($image);
function getcolor($color)
{
     global $image;
     $color = eregi_replace ("^#","",$color);
     $r = $color[0].$color[1];
     $r = hexdec ($r);
     $b = $color[2].$color[3];
     $b = hexdec ($b);
     $g = $color[4].$color[5];
     $g = hexdec ($g);
     $color = imagecolorallocate ($image, $r, $b, $g); 
     return $color;
}
function setnoise()
{
global $image, $width, $height, $back, $noisenum;
for ($i=0; $i<$noisenum; $i++){
$randColor = imageColorAllocate($image, rand(0, 255), rand(0, 255), rand(0, 255));  
imageSetPixel($image, rand(0, $width), rand(0, $height), $randColor);

}
?> 
现在的情况是:如果图片显示1234 那么$_session['code']的值将是另外一个值(上一次)。刷新页面之后,图片显示假如是 3342,那么$_session['code']则是1234.请问怎样才能即时取得图片的值?

解决方案 »

  1.   

    <img src="image.php">?I haven't seen Sb. call a file using this method.
      

  2.   

    purplecalm([email protected]
    你sb,那不叫调用,也不看看那文件是干什么的,就出来唧唧歪歪。
      

  3.   

    xiaorenwu5(林冲) 想在未提交时通过javascript验证就不对。
      

  4.   

    我说的不是javascript验证
    是提交到服务器段判断你输入的与那个session记录的对不对就可以知道了
      

  5.   

    meiguzhu()I do not have any words for this gink.
      

  6.   

    meiguzhu()you have the very making of making everyone boring of you.Sb. want help you, only because of you bad expression and then not clearly understanding, you abused us.should a good guy do this?
      

  7.   

    header("Content-type: image/png");
    $_SESSION['code'] = $code;
    这个似乎有点不对头,怎么能先发http header呢?
    $_SESSION['code'] = $code;
    header("Content-type: image/png");
    这样看看