PHP gd库 验证码
需要的簡單前臺頁面
?
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<title>驗(yàn)證碼</title>
</head>
<script type="text/javascript">
?? ?function yanz()
?? ?{
?? ??? ?document.getElementById('code').src='auth.php?tm='+Math.random();
?? ?}
</script>
<body>
<h1>請輸入驗(yàn)證碼</h1>
<form action="check_auth.php" method="post">
?? <input name="auth" type="text">
?? <a href="#" οnclick="yanz()"><b style="color: #FF0000">看不清,換一張</b></a>
?? <img src="auth.php" border="0" id="code" /><br/><br/>
?? <input type="submit" value="提交">
</form>
</body>
</html>
通過前臺的傳輸數(shù)據(jù)判斷是否輸入正確
<?php
?? session_start();
?? $auth=$_POST['auth'];
?? if(empty($auth))
?? {
?????? echo '錯誤:驗(yàn)證碼不能為空';
?????? die;
?? }
?? if($auth==$_SESSION['check_auth'])
?? {
?????? echo '正確';
?? }
?? else
?? {
?????? echo '錯誤:驗(yàn)證碼輸入錯誤';
?? }
?>
?
這是一個封裝好的一個驗(yàn)證類,也可以自己修改,可以百度PHP畫布,原理就是PHP畫圖組成的
?
<?php
error_reporting(0);
?? session_start();
?? header("Content-type:image/png");
?? $img_width=100;
?? $img_height=20;
?? srand(microtime()*100000);
?? for($i=0;$i<4;$i++)
?? {
??????? $new_number.=dechex(rand(0,15));
?? }
?? $_SESSION[check_auth]=$new_number;
?? $new_number=imageCreate($img_width,$img_height);//創(chuàng)建圖象
?? ImageColorAllocate($new_number,255,255,255);? //設(shè)置背景色為白色
?? for($i=0;$i<strlen($_SESSION[check_auth]);$i++)
?? {
?????? $font=mt_rand(3,5);
?????? $x=mt_rand(1,8) + $img_width*$i/4;
?????? $y=mt_rand(1,$img_height/4);
?????? $color=imageColorAllocate($new_number,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));//設(shè)置字符顏色
?????? imageString($new_number,$font,$x,$y,$_SESSION[check_auth][$i],$color);//輸出字符
?? }
?? ImagePng($new_number);
?? ImageDestroy($new_number);
?>
?
轉(zhuǎn)載于:https://www.cnblogs.com/yinyusoso/p/8350114.html
總結(jié)
以上是生活随笔為你收集整理的PHP gd库 验证码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到亲人骨折什么预兆
- 下一篇: PHP表达式