1.2_php验证码

使用php生成动态的验证码图片

 1 <!DOCTYPE html>
 2 <html>  
 3     <head>
 4         <meta charset="utf-8">
 5         <meta http-equiv="X-UA-Compatible" content="IE=edge">
 6         <title>GETTING STARTED WITH BRACKETS</title>
 7         <style>
 8             img{vertical-align: bottom;}
 9         </style>
10     </head>
11     <body>
12         <h2>PHP中的函数</h2>
13         <?php
14             function add($a,$b){
15                 $sum = 0;
16                 $sum = $a+$b;
17                 return $sum;
18             }
19             echo add(1,2);
20         ?>
21         <hr/>
22         <?php
23             $s = 'abc';
24             echo $s[1];//b
25         ?>
26         <h2>使用PHP生成动态的验证码图片</h2>
27         <?php
28   
29         ?>
30         <hr/>
31         <h2>用户登录</h2>
32         用5-1.php语言脚本生成我需要的图片,是服务器端动图生成的图片
33         验证码:<input/><img src="5-1.php"/>
34     </body>
35 </html>
index.php
<?php
    //注意,此文件是要向客户端输出一副动态生成的图片,所以,前前后后不能有任何的字符输出
    
    //告诉客户端,本页面输出的内容类型,是一个图片
    header('Content-Type','image/png');
    $w = 100;
    $h = 30;
    
//在服务器的内存中创建一副真彩色图片
    $img = imagecreatetruecolor($w,$h);
    
//分配一个即将使用的颜色(固定颜色)
//    $c=imagecolorallocate($img,255,0,0);//红色

//分配一个即将使用的颜色(随机颜色)
    $c=imagecolorallocate($img,rand(150,230),rand(150,230),rand(150,230));

//在图片上画一个矩形,作为背景色
    imagefilledrectangle($img,0,0,$w,$h,$c);

//生成四个随机的字符
    $src = 'ABCDEFGHIJKLMNPQRSTUVXWYZ23456789';//除去字母O和数字0,1,容易混淆
    $txt = $src[rand(0,strlen($src))];
    $txt .= $src[rand(0,strlen($src))];
    $txt .= $src[rand(0,strlen($src))];
    $txt .= $src[rand(0,strlen($src))];

//再重新分配一个颜色
    $c=imagecolorallocate($img,rand(150,230),rand(150,230),rand(150,230));

//把字符串刷到图片上
    imagestring($img,20,30,8,$txt,$c);

//向客户端输出图片中的二进制数据
    imagepng($img);

//从服务器端的内存中删除创建的图片
    imagedestroy($img);
?>
原文地址:https://www.cnblogs.com/wang715100018066/p/6569341.html