当前位置:首页 > 代码 > 正文

php网页验证码代码(Php网络验证源码)[20240420更新]

admin 发布:2024-04-20 01:58 121


本篇文章给大家谈谈php网页验证码代码,以及Php网络验证源码对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

php的验证码代码

?php

/**

* 类说明:

* 使用时,可按自己的需要设置输出图片的宽度和高度,以及要产生的验证码个数和干扰部分;

* 使用时,只需要将validate实例化,然后调用show_image()可生即可生成验证码。

* 获取验证码的方法是在其它页面 首先开户session_start(),然后直接使用$_SESSION['code']即可;

* 注意,大多数新手可能会遇到一个问题,就是$_SESSION['code']的值总是要慢一拍,用户在输入验证码点提交后,

* session的值才会被刷新,这样使用不会有错,如果直接用JS去获取得取到的是上次的产生的.

* 最后:该类由游天小虾制作,您可以不保留此信息,可任意传播,如果您对本类有什么提意,

* 可发关邮件到:yiuked@vip.qq.com

* 或者加入我们的网页制作交流群(聚义堂) 69574955

* **/

class validate {

private $width = '80';//验证码的宽度

private $height = '20';//验证码的高度

private $randcode = '';//验证码, 无需赋值,后面会随机生成

private $num = '4';//验证码的字数

private $interferon = '80';//干扰素数量

private $line ='2';//线条干扰条数

private $im = '';//无需赋值,图片自动生成/**

* 输入网页类型

* */

private function conten_type(){

header("Content_type:image/gif");

}

/***

*打开session

* **/

private function session_star(){

session_start();

}/**

* 产生随机数

* **/

private function random(){

$this-randcode = strtoupper(substr(md5(rand()),0,$this-num));

return $this-randcode;

}

/**

* 置障session的值

* **/

private function resession(){

$_SESSION['code'] = $this-randcode;

}

/**

* 产生验证图片

***/

private function create_image(){

$this-im = imagecreate($this-width,$this-height);

imagecolorallocate ($this-im, rand(50,60), rand(150,200),rand(230,250));

return $this-im;

} /**

* 产生干扰素

* **/

private function create_interferon(){

for($i=0;$i$this-interferon;$i++){

$infcolor = imagecolorallocate($this-im,rand(0,255),rand(0,255),rand(0,255));

imagesetpixel($this-im,rand(0,80),rand(0,20),$infcolor);

} } /**

* 产生干扰线条

* **/

private function create_line(){

for($j=0;$j$this-line;$j++){

$lineColor = imagecolorallocate($this-im,rand(0,255),rand(0,255),rand(0,255));

imageline($this-im,rand(0,80),rand(0,20),rand(0,80),rand(0,20),$lineColor);

}

} /**

* 写入字符

* **/

private function read_text(){

for($i=0;$i$this-num;$i++){

$textColor = imagecolorallocate($this-im,rand(0,100),rand(0,100),rand(0,100));

$x = rand(($this-width/$this-num*$i),($this-width/$this-num)*($i+1)-10);

$y = rand(2,$this-height-13);

imagechar($this-im,rand(4,5),$x,$y,$this-randcode[$i],$textColor);

} } /**

* 输出验证码图片

* **/

public function show_image(){

$this-session_star();

$this-conten_type();

$this-random();

$this-resession();

$this-create_image();

$this-create_interferon();

$this-create_line();

$this-read_text();

imagepng($this-im);

imagedestroy($this-im);

}} $va = new validate(); $va-show_image();

?

求php注册页面验证码验证代码 代码如下:

你要判断啊,判断你这个填写的验证码是不是跟你之前验证码保存的是否一致

if($_POST["captcha"]!=$_SESSION["captcha"])

{

echo "验证码错误";

}

php验证码怎么实现

1. 新建code.php验证码生成文件

在此之前必须打开php的GD库,修改php.ini文件的配置,取消extension=php_gd2.dll前面的分号。代码如下:

?php

session_start();

//生成验证码图片

Header("Content-type: image/PNG");

$im = imagecreate(44,18);

$back = ImageColorAllocate($im, 245,245,245);

imagefill($im,0,0,$back); //背景

srand((double)microtime()*1000000);

//生成4位数字

for($i=0;$i4;$i++){

$font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255));

$authnum=rand(1,9);

$vcodes.=$authnum;

imagestring($im, 5, 2+$i*10, 1, $authnum, $font);

}

for($i=0;$i100;$i++) //加入干扰象素

{

$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));

imagesetpixel($im, rand()p , rand()0 , $randcolor);

}

ImagePNG($im);

ImageDestroy($im);

$_SESSION['Checknum'] = $vcodes;

?

2. 显示验证码图片

在需要显示验证码的页面中加入

input type="text" name="passcode"

img src="code.php"

3.判断并获取验证码的值

验证码是通过第一步骤代码中的$_SESSION['Checknum'] = $vcodes;赋的值,所以验证码的值存在$_SESSION['Checknum']当中。在验证页面,使用以下代码,

...

session_start();//启动会话

$code=$_POST["passcode"];

if( $code == $_SESSION["Checknum"])

{...}即可完成验证码登录。

运行截图:

望采纳,谢谢

php网页验证码代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Php网络验证源码、php网页验证码代码的信息别忘了在本站进行查找喔。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/42.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载