因为 echo $_SESSION["code"];  的时候,a.php 还没运行!

解决方案 »

  1.   

    你这样,刷新echo $_SESSION["code"];得出的值会和刷新前图片显示的值一样你不用echo出来的么,直接提交后台比较就行了或者单独写个页面,去验证
    session_start();
    echo $_SESSION["code"];
      

  2.   

    <? 
    //checkNum.php 
    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["code"] = $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["code"] = $str; 
    ?> 
    <?php 
    session_start(); 
    //echo " <img src=a.php border=0 align=absbottom>";//生成图片 
    //echo $_SESSION["code"];//生成验证码
    if  ($_SESSION["code"]==$_POST["textfield"])
    {
    echo "1";
    }else
    {
    echo "2";
    }
    ?>
    <?php 
    session_start(); ?>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    </head><body>
    <form id="form1" name="form1" method="post" action="date.php">
      <label>
      <input type="text" name="textfield" />
    <?php  echo " <img src=a.php border=0 align=absbottom>";//生成图片   ?>
      </label>
      <label>
      <input type="submit" name="Submit" value="提交">
      </label>
    </form>
    </body>
    </html>
    这样也不对呀。输入验证码,返回的是2。