php高洛峰_PHP 验证码   高洛峰 细说PHP

前端页面index.php<?php

header('content-type:text/html;charset=utf-8');

if(isset($_POST['dosubmit'])){

session_start();

if(strtoupper($_SESSION['code']) == strtoupper($_POST['code'])){

echo '输入成功!
';

}else{

echo '输入不对
';

}

}

?>

用户名:

标题:

内容:

验证码:

生成验证码图片test.php<?php

//开启session

session_start();

require 'print.php';//导入验证码类文件

$vcode =new Vcode(80,30,4);//实例化验证码类

//将验证码放到服务器自己的空间保存一份

$_SESSION['code'] = $vcode->getCode();

//将验证码的图片输出

$vcode->outimg();//调用方法

验证码类 print.php<?php

class Vcode{

private $width;         //宽

private $heigth;        //高

private $num;           //数量

private $code;          //验证码

private $img;           //图像资源

//构造方法

function __construct($width=80,$height=25,$num=4){

$this->width        =   $width;

$this->heigth       =   $height;

$this->num          =   $num;

$this->code        =   $this->createCode();

}

//获取字符的验证码

function getCode(){

return $this->code;

}

//输出验证码图形

function outimg(){

//创建背景 颜色 大小 边框

$this->createBack();

//画字 大小 字体颜色

$this->outString();

//干扰元素 点 线条

$this->setDisturb();

//输出图像

$this->printImg();

}

//创建背景

private function createBack(){

//创建资源

$this->img = p_w_picpathcreatetruecolor($this->width, $this->heigth);

//设置随机背景颜色

$bgcolor = p_w_picpathcolorallocate($this->img, rand(225, 255), rand(225, 255), rand(225, 255));

//填充背景色

p_w_picpathfill($this->img, 0, 0, $bgcolor);

//画矩形

$bordercolor = p_w_picpathcolorallocate($this->img, 0, 0, 0);

p_w_picpathrectangle($this->img, 0, 0, $this->width-1, $this->heigth-1, $bordercolor);

}

//画字

private function  outString(){

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

$color  =   p_w_picpathcolorallocate($this->img, rand(0, 128), rand(0, 128), rand(0, 128));

$font = rand(3,5);

$x = 3 + ($this->width/$this->num)*$i;

$y = rand(1, 5);

p_w_picpathstring($this->img, $font,$x, $y, $this->code{$i}, $color);

}

}

//设置干扰元素

private function setDisturb(){

//加上点数

for($i=0;$i<100;$i++){

$color  =   p_w_picpathcolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255));

p_w_picpathsetpixel($this->img, rand(1, $this->width-2), rand(1, $this->heigth-2), $color);

}

//加上线条

for($i=0;$i<10;$i++){

$color  =   p_w_picpathcolorallocate($this->img, rand(0, 255), rand(0, 128), rand(0, 255));

p_w_picpatharc($this->img, rand(-10, $this->width+10), rand(-10, $this->heigth+10), rand(30, 300), rand(30, 300), 55, 44, $color);

}

}

//输出图像

private function printImg(){

//      header("Content-Type:p_w_picpath/jpeg");

//     p_w_picpathjpeg($this->img);

if(p_w_picpathtypes() & IMG_GIF){

header("Content-Type:p_w_picpath/gif");

p_w_picpathjpeg($this->img);

}elseif(p_w_picpathtypes() & IMG_JPEG){

header("Content-Type:p_w_picpath/jpeg");

p_w_picpathjpeg($this->img);

}elseif(p_w_picpathtypes() & IMG_JPG){

header("Content-Type:p_w_picpath/jpg");

p_w_picpathjpeg($this->img);

}elseif(p_w_picpathtypes() & IMG_PNG){

header("Content-Type:p_w_picpath/png");

p_w_picpathjpeg($this->img);

}

}

//生成验证码

private function  createCode(){

$codes = "23456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";

$code = "";

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

$code .=$codes{rand(0,strlen($codes)-1)};

}

return $code;

}

//释放图像资源

function __destruct(){

p_w_picpathdestroy($this->img);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
申明:本资源来源于互联网,如有侵权,请联系本人或者CSDN进行删除,谢谢!细说PHP》开发Web应用程序PHP是最理想的工具,易于使用、功能强大、成本低廉、安全性、开发速度快且执行灵活。《细说PHP》以实用为目标设计,包含PHP开发最主流的各项技术,对每一个知识点都进行了深入详细的讲解,并附有大量的实例代码,图文并茂。系统地介绍了PHP的相关技术及其在实际Web开发中的应用。 《细说PHP》共17章,每一章都是PHP独立知识点的总结。内容涵盖了PHP的运行环境搭建、Web服务器Apache的配置与应用、动态网站开发的前台技术、PHP编程语言的语法、PHP的常用功能模块和实用技巧、MySQL数据库的设计与应用、PHP 5面向对象的程序设计思想、Web开发的设计模式,以及包含DIV+CSS、mysqli扩展模块、数据库抽象层PDO、Smarty模板技术等目前PHP开发中最主流的技术。每一章中都有大量的实用示例,以及详尽的注释,加速读者的理解和学习,也为每章的技术点设置了大量的自测试题。最后以一个比较完整的、采用面向对象思想,以及通过MVC模式设计,并结合Smarty模板的CMS系统为案例,详细介绍了Web系统开发从设计到部署的各个细节,更好地进行开发实践。 申明:本资源来源于互联网,如有侵权,请联系本人或者CSDN进行删除,谢谢!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值