<php
class ValidateCode {
private $charset = 'abcdefjhjkmnpqrstuvwxyzABCDEFJHCKLMNPQRSTUVWXYZ123456789';
private $charsetlen; //随机码长度
private $width = 130; //长度
private $height = 50; //高度
private $codelen = 4; //数量
private $fontsize = 18; //字体大小
private $code; //验证码
private $img; //图像
private $fontfile; //字体
private $createfont = 30; //随机数字数量
private $createfontfile; //随机码字体
private $createline = 6; //随机线条
public function __construct() {
$this->charsetlen = strlen($this->charset)-1;
$this->fontfile = elephantttf';
$this->createfontfile = MISTRALTTF';
}
private function createCode() {
for ($i = 0;$i < $this->codelen;$i++) {
$code = $this->charset[mt_rand(0, $this->charsetlen)];
}
$this->code = $code;
}
private function createBackground() {
$this->img = imagecreatetruecolor($this->width, $this->height);
$color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color);
}
private function createFontCode() {
if ($this->createfont) {
for ($i = 0;$i < $this->createfont;$i++) {
$color = imagecolorallocate($this->img, mt_rand(188, 255), mt_rand(188, 255), mt_rand(188, 255));
imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $this->width / $this->createfont$i+mt_rand(-10, 10), mt_rand(0, $this->height), $color, $this->createfontfile, $this->charset[mt_rand(0, $this->charsetlen)]);
}
}
if ($this->createline) {
for ($i = 0;$i < $this->createline;$i++) {
$color = imagecolorallocate($this->img, mt_rand(188, 255), mt_rand(188, 255), mt_rand(188, 255));
imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
}
}
}
private function createFont() {
$x = $this->width / $this->codelen;
for ($i = 0;$i < $this->codelen;$i++) {
$color = imagecolorallocate($this->img, mt_rand(0, 155), mt_rand(0, 155), mt_rand(0, 155));
imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $x$i+mt_rand(5, 10), $this->height / 14+mt_rand(-5, 5), $color, $this->fontfile, $this->code[$i]);
}
}
private function outPut() {
header('Content-Type:img/png');
imagepng($this->img);
imagedestroy($this->img);
}
public function doImg() {
$this->createCode();
$this->createBackground();
$this->createFontCode();
$this->createFont();
$this->outPut();
}
public function getCode() {
return strtolower($this->code);
}
}
运行方法:
<php$code = new ValidateCode();
$code->doImg();
$_SESSION['code'] = $code->getCode();
>
$this->fontfile = elephantttf';
$this->createfontfile = MISTRALTTF';
这两个是字体路径,改成你所需要的字体(要把字体复制到这个类的目录下来。)
大哥!!!哪里没有起作用了啊!????
checknumphp
你看最后一行你写了什么???
session_destroy();
你都销毁了所有的SESSION
那么你在code_checkphp
肯定是看不到 $_SESSION['CODE'];的啊!!!
你删除session_destroy();看下
还有就是你在code_checkphp这个页面中
$_SESSION['CODE'];大小写错了!!!
应该是$_SESSION['code'];,拜托以后看清楚啊!!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)