How to create a simple verification code in php

小云云
Release: 2023-03-22 10:20:01
Original
2160 people have browsed it

This article mainly shares with you how to create a simple verification code in PHP. I hope it can help you.

1. Create a verification code output file


session_start();

//5.创建一个验证码
$code_length = 4;
$codes = '';

for($i=0;$i<$code_length;$i++)
{
    $codes .= dechex(mt_rand(0,15));
}

//7.生成sesstion
$_SESSION[&#39;code&#39;] = $codes;

$width = 75;
$height = 25;

//1. 创建一张图片
$img = imagecreatetruecolor($width,$height);

//4. 创建图片静态内容
//白色
$white = imagecolorallocate($img, 255, 255, 255);
//黑色
$black = imagecolorallocate($img, 100, 100, 100);
//红色
$red = imagecolorallocate($img, 255, 0, 0);

//图片背景
imagefill($img, 0, 0, $white);


//随机线条
for($i=0;$i<4;$i++)
{
    //创建随机颜色
    $img_mt_color = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));

    //画线
    imageline($img, mt_rand(0,$width),mt_rand(0,$height), mt_rand(0,$width),mt_rand(0,$height), $img_mt_color);
}

//随机打雪花
for($i=0;$i<100;$i++)
{
    //创建随机浅淡色
    $img_mt_color = imagecolorallocate($img, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255));

    //画*
    imagestring($img,1, mt_rand(0,$width),mt_rand(0,$height),&#39;*&#39;,$img_mt_color);
}

//imagestring($img, 3, 10, 10, strlen($codes), $red);


//6.绘制验证码
for($i=0;$i<$code_length;$i++)
{
    //创建验证码颜色(偏深色)
    $img_mt_color = imagecolorallocate($img, mt_rand(0,100), mt_rand(0,150), mt_rand(0,200));
    //生成验证码每一位字符
    imagestring($img, mt_rand(3,5), $i*$width/4 + mt_rand(0,10), mt_rand(1,$height/2), $_SESSION[&#39;code&#39;][$i], $img_mt_color);
}

//创建红色矩形边框
imagerectangle($img, 0, 0, $width-1, $height-1, $red);

//2. 让浏览器知道输出的是一张图片
header(&#39;Content-type:image/png&#39;);
//3. 输出
imagepng($img);
//4. 销毁
imagedestroy($img);
Copy after login


2. Call this verification code on another page, And realize click refresh verification code:



<img src="php_code_img.php" id="myCodeImg" />

<script type="text/javascript">
(function(){
    var myCodeImg = document.getElementById_x(&#39;myCodeImg&#39;);
    myCodeImg.onclick = function()
    {
        this.src = &#39;./php_code_img.php?mt=&#39;+Math.ceil(Math.random()*1000);
    }
})();
</script>
Copy after login

Related recommendations:

Use PHP drawing function to achieve simplicity Method of verification code function_php example

php implements simple verification code

Generates simple verification code based on PHP, PHP generates simple verification code _PHP tutorial

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!