zoukankan      html  css  js  c++  java
  • php验证码

    PHP写的验证码

    Demo.php
    1
    function _code($_width = 75,$_height = 25,$_rnd_code = 4,$_flag = false) { 2 3 //创建随机码 4 for ($i=0;$i<$_rnd_code;$i++) { 5 $_nmsg .= dechex(mt_rand(0,15)); 6 } 7 8 //保存在session 9 $_SESSION['code'] = $_nmsg; 10 11 //创建一张图像 12 $_img = imagecreatetruecolor($_width,$_height); 13 14 //白色 15 $_white = imagecolorallocate($_img,255,255,255); 16 17 //填充 18 imagefill($_img,0,0,$_white); 19 20 if ($_flag) { 21 //黑色,边框 22 $_black = imagecolorallocate($_img,0,0,0); 23 imagerectangle($_img,0,0,$_width-1,$_height-1,$_black); 24 } 25 26 //随即画出6个线条 27 for ($i=0;$i<6;$i++) { 28 $_rnd_color = imagecolorallocate($_img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); 29 imageline($_img,mt_rand(0,$_width),mt_rand(0,$_height),mt_rand(0,$_width),mt_rand(0,$_height),$_rnd_color); 30 } 31 32 //随即雪花 33 for ($i=0;$i<100;$i++) { 34 $_rnd_color = imagecolorallocate($_img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)); 35 imagestring($_img,1,mt_rand(1,$_width),mt_rand(1,$_height),'*',$_rnd_color); 36 } 37 38 //输出验证码 39 for ($i=0;$i<strlen($_SESSION['code']);$i++) { 40 $_rnd_color = imagecolorallocate($_img,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)); 41 imagestring($_img,5,$i*$_width/$_rnd_code+mt_rand(1,10),mt_rand(1,$_height/2),$_SESSION['code'][$i],$_rnd_color); 42 } 43 44 //输出图像 45 header('Content-Type: image/png'); 46 imagepng($_img); 47 48 //销毁 49 imagedestroy($_img); 50 }

    上面的代码写在Demo.php

      这是Demo.php将是一个图片,此时想调用验证码

      只需在Demo1.php中使用:<img  id="code" src="Demo.php" />

    如果想点击图片使验证码发生改变:

      <script type="text/javascript">
         var code = document.getElementById('code'); 
         code.onclick = function(){
          this.src='code.php?tm='+Math.random();
         }
      </script>

  • 相关阅读:
    RATE-MAX——alpha冲刺总结随笔
    RATE-MAX alpha冲刺第十天
    RATE-MAX alpha冲刺第九天
    团队作业第六次——Daily6+1站立式会议+β冲刺汇总
    问题总结(事后诸葛亮和组员交换事宜)
    凡事预则立
    测试随笔
    冲刺随笔
    alpha冲刺——第十天
    alpha冲刺——第九天
  • 原文地址:https://www.cnblogs.com/changshaoshuai/p/3293330.html
Copyright © 2011-2022 走看看