Home > Backend Development > PHP Tutorial > 008-php generates random verification code

008-php generates random verification code

不言
Release: 2023-03-23 10:20:01
Original
1473 people have browsed it

This article introduces the random verification code generated by php. Now I share it with everyone. Friends in need can refer to it


<?php 

/**
* 生成随机字符串
* @param int $num 生成的随机字符串的个数
* @return str 生成的随机字符串
*/
function randStr($num=6) {
	$str = str_shuffle(&#39;abcedfghjkmnpqrstuvwxyzABCEDFGHJKMNPQRSTUVWXYZ23456789&#39;);
	return substr($str, 0 , $num);
}

//1.创建画布
$pic = imagecreatetruecolor(80, 50);

//2.创建颜料(RGB)
$red = imagecolorallocate($pic, 255, 0, 0);
$blue = imagecolorallocate($pic, 127, 127, 127);

//3.背景填充
imagefill($pic, 0, 0, $red);

//4.写入文字
imagestring($pic, 5, 5, 5, randStr(4), $blue);

//5. 输出/保存图形
header(&#39;Content-type:image/png&#39;);
imagepng($pic);

//6. 销毁画布(关闭画板)
imagedestroy($pic);

?>
Copy after login

Related recommendations:

007-PHP GD drawing process

006-PHP common function encapsulation


The above is the detailed content of 008-php generates random verification code. 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