Home  >  Article  >  Backend Development  >  How to automatically generate verification code in php

How to automatically generate verification code in php

王林
王林Original
2021-10-21 16:41:382972browse

php method to automatically generate verification codes: [session_start(); $width = 150; $height = 40; $image = imagecreatetruecolor($width, $height);$bgcol...].

How to automatically generate verification code in php

The operating environment of this article: windows10 system, php 7, thinkpad t480 computer.

Nowadays, verification codes are used more and more in forms, but it is always inconvenient to implement it with js, so I use php to implement it and record it here.

Of course, we can also encapsulate it into a function, which is very convenient for future use. It is not encapsulated here, and interested friends can encapsulate it by themselves.

Specific implementation code:

Create a new cap_sz.php file:

<?php
session_start(); //设置session,一定要在顶部

$width = 150; //设置图片宽为300像素
$height = 40; //设置图片高为40像素

$image = imagecreatetruecolor($width, $height); //设置验证码大小的函数
$bgcolor = imagecolorallocate($image, 255, 255, 255); //验证码颜色RGB为(255,255,255)#ffffff
imagefill($image, 0, 0, $bgcolor); //区域填充

$cap_code = "";
for($i=0;$i<4;$i++){
    $fontsize = 7; //设置字体大小
    $fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    //数字越大,颜色越浅,这里是深颜色0-120
    $fontcontent = rand(0,9);
    $cap_code.=$fontcontent; //.=连续定义变量
   
    $x = ($i*150/4)+rand(5,10);
    $y = rand(5,10);
    //设置坐标
   
    imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
}

$_SESSION[&#39;code&#39;] = $cap_code; //存到session

//设置干扰元素,设置雪花点
for($i=0;$i<300;$i++){
    $inputcolor = imagecolorallocate($image, rand(50,200), rand(20,200), rand(50,200));
    //设置颜色,20-200颜色比数字浅,不干扰阅读
    imagesetpixel($image, rand(1,149), rand(1,39), $inputcolor);
    //画一个单一像素的元素
}

//增加干扰元素,设置横线(先设置线的颜色,在设置横线)
for ($i=0;$i<4;$i++) {
    $linecolor = imagecolorallocate($image, rand(20,220), rand(20,220),rand(20,220));
    //设置线的颜色

    imageline($image, rand(1,149), rand(1,39), rand(1,299), rand(1,149), $linecolor);  

}

//因为有些浏览器,访问的content-type会是文本型(乱码),所以我们需要设置成图片的格式类型
header(&#39;Content-Type:image/png&#39;);

imagepng($image); //建立png函数
imagedestroy($image); //结束图形函数,消除$image

Then create a new index.php for verification

<?php
header("Content-Type: text/html;charset=utf-8");
if (isset($_REQUEST[&#39;code&#39;])){
    session_start();

    if ($_REQUEST[&#39;code&#39;] == $_SESSION[&#39;code&#39;]){
        echo "输入正确";
    }else{
        echo "输入错误,请重新输入";
    }

    exit();
}

?>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> //这里不能少,不然乱码
    <title>验证码测试</title>
</head>
<body>
<form>
<p>验证码:<img src="cap_sz.php" onClick="this.src=&#39;cap_sz.php?nocache=&#39;+Math.random()" style="cursor:hand" alt="点击换一张"/>点击图片可更换验证码</p>
<p>请输入图片中的内容:<input type="text" name="code" value=""/></p>
<p><input type="submit" width="20px" height=19px value="提交"></input></p>

</form>
</body>
</html>

Recommended learning: phptraining

The above is the detailed content of How to automatically generate verification code in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn