<?php$img_width=100;
$img_height=20;
srand(microtime()*100000); //随机数
for($i=0; $i<4; $i++){
   $new_number[]=dechex(rand(0,15));
}
$_SESSION["check_auth"]=$new_number;header("content_type:image/png");
$image=@imagecreate($img_width,$img_height);       //创建图像对象
$background_color=imagecolorallocate($image,222,45,58); //背景色
$text_color=imagecolorallocate($image,233,14,91);
for($i=0; $i<count($_SESSION["check_auth"]); $i++){
   $font=mt_rand(3,7);
   $x=mt_rand(1,8)+$img_width*$i/4;
   $y=mt_rand(1,$img_height/4);
   $color=imagecolorallocate($image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));  //字符颜色
   $str=$_SESSION["check_auth"][$i];
   imagestring($image,$font,$x,$y,"{$str}",$color);  //输出字符
}imagepng($image);    //输出png图像
imagedestroy($image);  //销毁图像对象?>我刚学习了这个,做了一个简单的生成验证码,然后遇到了一个难题!
要求是:
做一个带验证码的登陆页面,有求验证码刷新至少能变换十次。跪求大虾们的帮助!!!!

解决方案 »

  1.   

    换说GD库什么时候变成DG库了?
    既然是随机数,别说10次,100次都可以....
      

  2.   

    首先把你的代码文件保存为gd.php(就是你的代码单独保存为gd.php)
    新建一个文件为js.php
    其中gd.php的代码为:
    <?php$img_width=100;
    $img_height=20;
    srand(microtime()*100000); //随机数
    for($i=0; $i<4; $i++){
      $new_number[]=dechex(rand(0,15));
    }
    $_SESSION["check_auth"]=$new_number;header("content_type:image/png");
    $image=@imagecreate($img_width,$img_height); //创建图像对象
    $background_color=imagecolorallocate($image,222,45,58); //背景色
    $text_color=imagecolorallocate($image,233,14,91);
    for($i=0; $i<count($_SESSION["check_auth"]); $i++){
      $font=mt_rand(3,7);
      $x=mt_rand(1,8)+$img_width*$i/4;
      $y=mt_rand(1,$img_height/4);
      $color=imagecolorallocate($image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)); //字符颜色
      $str=$_SESSION["check_auth"][$i];
      imagestring($image,$font,$x,$y,"{$str}",$color); //输出字符
    }imagepng($image); //输出png图像
    imagedestroy($image); //销毁图像对象?>
    其中js.php的代码为:<script language="javascript" type="text/javascript">
      function RefreshImage()
      
      {
      
      var el =document.getElementById("img11");
      
      el.src=el.src+'?';//这个特别重要
      
      }
    var i=10
    function change()
    {
          i--;
          if(i<=0)
          {
            i=10;
            clearTimeout("change()",100);
          }
          else
          {
          RefreshImage();
            setTimeout("change()",100);
          }
      }</script><form method="post" action="admin_login_check.php" name="logform"><p>
      
      <label>验证码:</label>
      
      <input name="xym" type="text" size="8" />
      
      &nbsp;&nbsp;<img id="img11" src="gd.php" style="cursor:pointer" onmouseup="change()" alt="点击刷新" title="点击刷新" /><span>点击图片刷新</span></p>