13.2.2 验证码类的编写1
code.php
<?php
//开启session
session_start();
include "vcode.class.php";
//构造方法
$vcode = new Vcode(80, 25, 4);
//将验证码放到服务器自己的空间保存一份
$_SESSION = $vcode->getcode();
//将验证码图片输出
$vcode->outimg();
reg.php
<body>
<form action="reg.php" method="post">
username: <input type="text" name="username"> <br>
password: <input type="password" name="password"> <br>
code: <input type="text" size="4" name="code">
<img src="code.php" /> <br>
<input type="submit" name="dosubmit" value="登 录"> <br>
</form>
</body>
vcode.class.php
<?php
class Vcode {
private $width; //宽
private $height; //高
private $num; //数量
private $code; //验证码
//构造方法, 三个参数
function __construct($width, $height, $num) {
$this->width = $width;
$this->height = $height;
$this->num = $num;
$this->code = $this->createcode(); //调用自己的方法
}
//获取字符的验证码, 用于保存在服务器中
function getcode() {
return $this->code;
}
//输出图像
function outimg() {
//创建背景 (颜色, 大小, 边框)
$this->createback();
//画字 (大小, 字体颜色)
//干扰元素(点, 线条)
//输出图像
}
//创建背景
private function createback() {
}
//画字
private function outstring() {
}
//设置干扰元素
private function setdisturbcolor() {
}
//输出图像
private function printimg() {
}
//生成验证码字符串
private function createcode() {
$codes = "3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXY";
$code = "";
for($i=0; $i < $this->num; $i++) {
$code .=$codes{rand(0, strlen($codes)-1)};
}
return $code;
}
}