验证码类和分页类

来源:互联网 发布:中国铁塔 网络强国 编辑:程序博客网 时间:2024/04/25 20:56

1 . 验证码类

<?php

$v = new Verify();

$v->outImg();

class Verify
{
//宽
protected $width;
//高
protected $height;
//图片类型
protected $imgType;
//文字类型
protected $codeType;
//文字的个数
protected $num;
//保存的验证码字符串
protected $verifyCode;
//保存验证码资源的一个成员属性
protected $res;

//初始化
//上面这些参数
public function __construct($width = 100, $height = 50, $imgType = 'png', $codeType = 3, $num = 4)
{
$this->width = $width;
$this->height = $height;
$this->imgType = $imgType;
$this->codeType = $codeType;
$this->num = $num;
$this->verifyCode = $this->createVerifyCode();

}

protected function  createVerifyCode()
{
$string = '';

switch ($this->codeType) {
case 1:
$string = implode('',array_rand(range(0,9),$this->num));
break;
case 2:
$string = join('',array_rand(array_flip(range('a','z')),4));
break;
case 3:
/*
for ($i = 0; $i < $this->num; $i++) {
$r= mt_rand(0,2);
switch ($r) {
case 0:
$ascii = mt_rand(48,57);
break;
case 1:
$ascii = mt_rand(65,90);
break;
case 2:
$ascii = mt_rand(97,122);
break;
}
$string .= chr($ascii);

}
*/
$str = 'abcdefghijkmnpqrstuvwxzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
$string = substr(str_shuffle($str),0,$this->num);
break;
}

return $string;
}

//调验证码显示的一个方法 output
//1.画图
//2.分配颜色(写两个成员方法,调的时候直接调对应的成员方法即可)
//3.背景填充
//4.画干扰点
//5.画干扰线
//6.写字 
//7. 输出类型
//8. 输出图片
public function outImg()
{
$this->createImg();
$this->fillBgColor();
$this->fillPix();
$this->fillArc();
$this->writeFont();
$this->output();
}

protected function output()
{
//imagepng
$func = 'image'.$this->imgType;
$mime = 'Content-type:image/'.$this->imgType;
header($mime);
$func($this->res);
}

protected function writeFont()
{
for ($i = 0; $i < $this->num; $i++) {

$width = ceil($this->width / $this->num);
$x = $width * $i;
$y= mt_rand(5,$this->height - 10);
$c = $this->verifyCode[$i];
imagechar($this->res,5,$x,$y,$c,$this->darkColor());
}

}
protected function fillArc()
{
for($i = 0; $i < 10; $i++) {
imagearc($this->res,
mt_rand(10,$this->width - 10),
mt_rand(10,$this->height - 10),
mt_rand(0,$this->width),
mt_rand(0,$this->height),
mt_rand(0,180),
mt_rand(181,360),
$this->lightColor()
);
}
}

protected function fillPix()
{
$num = $this->pixNum();
for ($i = 0; $i < $num; $i++) {

imagesetpixel($this->res,mt_rand(0,$this->width),mt_rand(0,$this->height),$this->darkColor());
}
}

protected function pixNum()
{
$area = ceil(($this->width * $this->height) / 20);
return $area;
}


protected function fillBgColor()
{
imagefill($this->res,0,0,$this->lightColor());
}


protected function lightColor()
{
return imagecolorallocate($this->res,
  mt_rand(130,255),
  mt_rand(130,255),
  mt_rand(130,255)
);
}

protected function darkColor()
{
return imagecolorallocate($this->res,
  mt_rand(0,120),
  mt_rand(0,120),
  mt_rand(0,120)
);
}

protected function createImg()
{
$this->res = imagecreatetruecolor($this->width,$this->height);
}

//9 .销毁图片资源
public function __destruct()
{
//imagedestroy($this->res);
}

//可以做一个魔术方法__get专门用于得到验证码字符串
public function __get($key)
{
if ($key == 'verifyCode') {
return $this->$key;
}

return false;
}


}


2.分页类


<?php


$page = new Page(60);


class Page
{

//总条数
protected $total;
//总页数
protected $pageCount;
//当前页码
protected $page;
//每页显示数
protected $num;
//URL
protected $url;
//偏移$offset
protected $offset;


//初始化一批成员属性
//总页数
//每页显示数
//总条数
//当前页码
//url
public function __construct($total, $num = 5)
{
$this->total = ($total > 0) ?(int) $total : 1;
$this->num = $num;
$this->pageCount = $this->getCount();
$this->page = $this-> getPage();
$this->url = $this->getUrl();
}

public function limit()
{
$offset = ($this->page - 1) * $this->num;
$str = $offset .',' .$this->num; 
return $str;
}

public function first()
{
return $this->setUrlString('page=1');
}

public function next()
{
$page = ($this->page > $this->pageCount) ? $this->pageCount : ($this->page + 1);
return $this->setUrlString('page='.$page);
}

public function prev()
{
$page = ($this->page < 2) ? 1 : ($this->page - 1);

return $this->setUrlString('page='.$page);

}

public function end()
{
return $this->setUrlString('page='.$this->pageCount);
}

public function render()
{
return [
'first' => $this->first(),
'next' => $this->next(),
'prev' => $this->prev(),
'end' => $this->end(),
  ];
}

protected function setUrlString($page)
{
if (strpos($this->url,'?')) {
 $url = $this->url .'&'.$page;
} else {
 $url = $this->url .'?'.$page;
}
return $url;
}

//设置 url的成员方法
//协议 http://
//主机www.baidu.com
//$_SERVER['SERVER_PORT']端口


//文件路径  parse_url   path  query
// $_SERVER['REQUEST_URI']   /a/b/index.php?username=123
//参数
protected function getUrl()
{
$path = $_SERVER['REQUEST_URI'];

$parse = parse_url($path);

if (isset($parse['query'])) {

parse_str($parse['query'],$query);
unset($query['page']);
$path = $parse['path'] .'?'.http_build_query($query);
}

$path = rtrim($path,'?');

$protocal = (
isset($_SERVER['SERVER_PORT']) 
&&
$_SERVER['SERVER_PORT'] == 443
) ? 'https://' : 'http://';


if ( 80 == $_SERVER['SERVER_PORT']) {
$url = $protocal . $_SERVER['SERVER_NAME'].$path;
} elseif ( 443 == $_SERVER['SERVER_PORT']  ) {
$url = $protocal . $_SERVER['SERVER_NAME'].$path;
} else {
$url = $protocal . $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$path;
}

return $url;

}



protected function getPage()
{
if (isset($_GET['page'])) {
return (int) $_GET['page'];
} else {
return 1;
}
}

protected function getCount()
{
return ceil($this->total / $this->num);
}

//render,一次性输出:上一页,下一页,首页,尾页

//未来数据库做准备的一次性输出limit的值

//上一页

//下一页

//总页数

//最后一页

//首页

//得到url的成员属性





}

0 0
原创粉丝点击