• php开发_图片验证码


    项目结构:

    提交表单:

    校验正确:

    校验错误:

    ===========================================================

         代码部分:

    ===========================================================

    yanzhengma.php

     1 <?php
     2 //验证码的制作
     3 //1.生成4位的随机数
     4 for($i=0;$i<4;$i++){
     5     $rand.=dechex(rand(1, 15));
     6 }
     7 
     8 //2.创建图片
     9 //创建一张图片
    10 $image=imagecreatetruecolor(100, 30);
    11 //背景颜色
    12 $bg=imagecolorallocate($image, 0, 0, 0);
    13 $te=imagecolorallocate($image, 255,255,255);
    14 
    15 
    16 //划线
    17 for($i=0;$i<4;$i++){
    18     $lineColor=imagecolorallocate($image, rand(20, 225), rand(20, 225), rand(20, 225));
    19     imageline($image, rand(0, 100), 0, 100,30, $lineColor);
    20 }
    21 //划点
    22 for($i=0;$i<200;$i++){
    23     imagesetpixel($image, rand()%100, rand()%100, $lineColor);
    24 }
    25 //添加文字
    26 //编码转换
    27 $str=iconv("GBK", "UTF-8", "广州GZ");
    28 imagettftext($image, 12,11, 20, 20, $lineColor, 'simhei.ttf', $str);
    29 
    30 //3.随机数写入图片$font=(1~6)
    31 imagestring($image, rand(3, 6), rand(3, 70), rand(0, 16), $rand, $te);
    32 
    33 //4.随机数保存在session中
    34 //启动session
    35 session_start();
    36 //把验证码放入session
    37 $_SESSION[vilidationCode]=$rand;
    38 
    39 
    40 header("Content-type: image/jpeg");
    41 imagejpeg($image);
    42 ?>

    form.php

     1 <?php
     2 
     3 session_start();
     4 
     5 if($_POST[yanzhengma]){
     6 
     7     if($_POST[yanzhengma]==$_SESSION[vilidationCode]){
     8         echo "你通过了验证!";
     9     }else{
    10         echo "你输入的验证码错误!";
    11     }
    12 }
    13 ?>
    14 <form action="" method="post"><img src="yanzhengma.php"><br />
    15 <input type="text" name="yanzhengma" /><br />
    16 <input type="submit" value="submit" /></form>

    注意啦:
    在项目结构中的COURBD.TTF和simhei.ttf这两个是字体库

    大家可以在自己电脑:C:\WINDOWS\Fonts 目录中找到

    (大多数在这里,部分同志可能安装在其他目录,T_T)。

    测试了一下下面的代码:

    1 //添加文字
    2 //编码转换
    3 $str=iconv("GBK", "UTF-8", "广州GZ");
    4 imagettftext($image, 12,11, 20, 20, $lineColor, 'simhei.ttf', $str);

    可以显示中文,但是如果是下面的代码:

    1 //添加文字
    2 //编码转换
    3 $str=iconv("GBK", "UTF-8", "广州GZ");
    4 imagettftext($image, 12,11, 20, 20, $lineColor, 'COURBD.TTF', $str);

    这时候,中文就不能显示啦,所以大家在使用字体库的时候还是要注意的!!

  • 相关阅读:
    Leetcode:Convert Sorted List to Binary Search Tree
    Leetcode:Unique Binary Search Trees & Unique Binary Search Trees II
    Leetcode::Longest Common Prefix && Search for a Range
    Leetcode::Flatten Binary Tree to Linked List
    Leetcode::JumpGame
    leetcode power(x,n)
    Leetcode Letter Combinations of a Phone Number
    leetcode Reverse Nodes in k-Group
    leetcode Merge k Sorted Lists
    word ladder
  • 原文地址:https://www.cnblogs.com/hongten/p/php_vilidationCode.html
Copyright © 2020-2023  润新知