《PHP实战:使用php实现网站验证码功能【推荐】》要点:
本文介绍了PHP实战:使用php实现网站验证码功能【推荐】,希望对您有用。如果有疑问,可以联系我们。
PHP实战验证码是网站常用的一项安全措施,也是新人站长较难掌握的一项技能,这里我向大家介绍一简单有效的验证码实现方法.
PHP实战开始之前
PHP实战在正式开始之前我们需要打开php的gd2图形库支持(在php.ini,中搜索“php_gd2.dll”,找到“;extension=php_gd2.dll”并去掉句首的分号) .
PHP实战可以参考:如何打开php的gd2库
PHP实战核心:img.php
PHP实战这个页面生成一张验证码并将正确数值写入 Session
PHP实战随机一个4位验证码
PHP实战$check=rand(1000,9999);
PHP实战将生成的验证码写入session
- PHP实战
- Session_start();
- $_SESSION["check"] = $check;
PHP实战创建一张图片
PHP实战$im = imagecreate(80,30);
PHP实战由于这种图片的背景默认是黑色的所以我们要用白色填充.
PHP实战imagefill($im,0,0,ImageColorAllocate($im, 255,255,255));
PHP实战使用imageline随机绘制两条实线
- PHP实战
- $y1=rand(0,30);
- $y2=rand(0,30);
- $y3=rand(0,30);
- $y4=rand(0,30);
- imageline($im,0,$y1,70, $y3,000);
- imageline($im,0,$y2,70, $y4,000);
PHP实战在随机位置绘制文字
- PHP实战
- $strx=rand(3,15);
- $stry=rand(2,15);
- imagestring($img,5,$strx,$stry,substr($check,0,1),ImageColorAllocate($img,34,87,100));
- $strx+=rand(15,20);
- $stry=rand(2,15);
- imagestring($img,5,$strx,$stry,substr($check,1,1),ImageColorAllocate($img,781,117,78));
- $strx+=rand(15,20);
- $stry=rand(2,15);
- imagestring($img,5,$strx,$stry,substr($check,2,1),ImageColorAllocate($img,160,40,40));
- $strx+=rand(15,20);
- $stry=rand(2,15);
- imagestring($img,5,$strx,$stry,substr($check,3,1),ImageColorAllocate($img,25,55,10));
PHP实战输出图像
- PHP实战
- Header("Content-type: image/PNG");
- ImagePNG($img);
PHP实战结束,下面是完整代码
- PHP实战
- <?php $check=rand(1000,9999);
- Session_start();
- $_SESSION["check"] = $check;
- $img = imagecreate(80,30);
- imagefill($img,0,0,ImageColorAllocate($img,255,255,255));
- $y1=rand(0,30);
- $y2=rand(0,30);
- $y3=rand(0,30);
- $y4=rand(0,30);
- imageline($img,0,$y1,70, $y3,ImageColorAllocate($img,55,255,25));
- imageline($img,0,$y2,70, $y4,ImageColorAllocate($img,55,55,255));
- $strx=rand(3,15);
- $stry=rand(2,15);
- imagestring($img,5,$strx,$stry,substr($check,0,1),ImageColorAllocate($img,34,87,100));
- $strx+=rand(15,20);
- $stry=rand(2,15);
- imagestring($img,5,$strx,$stry,substr($check,1,1),ImageColorAllocate($img,781,117,78));
- $strx+=rand(15,20);
- $stry=rand(2,15);
- imagestring($img,5,$strx,$stry,substr($check,2,1),ImageColorAllocate($img,160,40,40));
- $strx+=rand(15,20);
- $stry=rand(2,15);
- imagestring($img,5,$strx,$stry,substr($check,3,1),ImageColorAllocate($img,25,55,10));
- Header("Content-type: image/PNG");
- ImagePNG($img);
PHP实战用户界面:index.php
PHP实战想必大家都知道怎么做,我就直接给出代码了
- PHP实战
- <!DOCTYPE html>
- <html>
- <body>
- <form action="action.php" method="post">
- <input type="text" name="cikle" placeholder="验证码">
- <br>
- <img id="cikle" style="-webkit-user-select: none" src="img.php"><input type="submit" value="Submit">
- </form>
- </body>
- </html>
PHP实战以上的代码将用户输入的数值传递到“action.php”中
PHP实战检查:action.php
PHP实战这一步要将用户输入数值与session中的数值进行比对
PHP实战相等,输出“正确”
PHP实战不相等,输出“不正确”
- PHP实战
- <?php
- Session_start();
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- if($_SESSION["check"]!=intval($_POST["cikle"])){
- echo "不正确";
- }else{
- echo "正确";
- }
- }
PHP实战以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持维易PHP!
转载请注明本页网址:
http://www.vephp.com/jiaocheng/1828.html