PHP生成可點擊刷新的驗證碼簡單示例

字號:


    本文實例講述了PHP生成可點擊刷新的驗證碼。分享給大家供大家參考,具體如下:
    html文件:
    <html>
     <head>
       <title>驗證碼</title>
     </head>
      <script type="text/javascript">
        function yanzheng(){
          var im=document.getElementsByTagName("img");
          im[0].src="gd.php?temp="+(new Date().getTime().toString(36));
        }
      </script>
     <body>
      <img src="gd.php"/><a href="#" onclick="yanzheng()">換一張</a>
     </body>
    </html>
    驗證碼文件gd.php:
    <?php
    $im=imagecreate(50,20);
    $b=imagecolorallocate($im,0,0,0);
    $w=imagecolorallocate($im,255,255,255);
    $mask="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $text="";
    for($i=1;$i<=4;$i++){
      $index=rand(0,61);
      $text.=$mask{$index};
    }
    imagestring($im,3,rand(10,20),rand(0,10),$text,$w);
    header("Content-type:image/jpeg");
    imagejpeg($im);
    ?>
    希望本文所述對大家PHP程序設(shè)計有所幫助。