PHP로 간단한 인증 코드를 만드는 방법

小云云
풀어 주다: 2023-03-22 10:20:01
원래의
2128명이 탐색했습니다.

이 글은 주로 PHP에서 간단한 인증 코드를 생성하는 방법을 공유합니다. 도움이 되기를 바랍니다.

1. 인증 코드 출력 파일을 생성하세요


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);
로그인 후 복사


2. 다른 페이지에서 이 인증 코드를 호출하고 클릭하여 인증 코드를 새로 고치세요.



<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>
로그인 후 복사

관련 권장 사항:

PHP 그리기 기능을 이용한 간편 인증코드 구현 방법 function_php 예시

php 간편 인증 코드 구현

PHP를 기반으로 간편 인증 코드 생성, PHP 간편 인증 코드 생성_PHP 튜토리얼

위 내용은 PHP로 간단한 인증 코드를 만드는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!