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

      首先验证码的原理就是,画一张图片,然后在这张图片上写一些字,然后加一些干扰的线条,像素点之类的东西就ok了,

    这里要使用php那就要知道php中画图的函数是那些,然后拿来用便是了。

      如果要用php的画图函数,首先要启用这个模块的功能。就是把php.ini中php_gd2.dll前面的注释去掉就好了。

      下面开始画图:

       1 <?php

     2     session_start();
     3     //生成验证码图片
     4     Header("Content-type: image/PNG");
     5     $im = imagecreate(44,18); // 画一张指定宽高的图片
     6     $back = ImageColorAllocate($im, 245,245,245); // 定义背景颜色
     7     imagefill($im,0,0,$back); //把背景颜色填充到刚刚画出来的图片中
     8     $vcodes = "";
     9     srand((double)microtime()*1000000);
    10     //生成4位数字
    11     for($i=0;$i<4;$i++){
    12     $font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255)); // 生成随机颜色
    13     $authnum=rand(1,9);
    14     $vcodes.=$authnum;
    15     imagestring($im, 5, 2+$i*10, 1, $authnum, $font);
    16     }
    17     $_SESSION['VCODE'= $vcodes;
    18 
    19     for($i=0;$i<100;$i++//加入干扰象素
    20     {
    21     $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
    22     imagesetpixel($im, rand()%70 , rand()%30 , $randcolor); // 画像素点函数
    23     }
    24     ImagePNG($im);
    25     ImageDestroy($im);
    26 ?>

      基本就是这样实现了,其实如果给图片打水印也无非就是往图片里面写字,原理都差不多的。 

      使用的地方直接

      <img src="xxx.php" /> 填写这个php文件的名字,就可以使用了。 

  • 相关阅读:
    简单读取winfrom资源文件
    string.Format对C#字符串格式化
    如何在SQL中使用循环结构
    Linq to SQL 多条件动态组合查询(实战篇)
    提问的艺术
    XtraGrid 单元格加边框颜色
    凭证控件制作
    C# double 四舍五入
    自定义光标样式
    触发窗体事件(例如按Esc关闭窗体)
  • 原文地址:https://www.cnblogs.com/sunnystone85/p/1747745.html
Copyright © 2011-2022 走看看