±¾ÎĽeÉܵÄÊÇ»uÓÚPHPʵÏÖÓû§×¢²aµÇ¼¹¦ÄÜ£¬±¾ÏiÄ¿ÖΪËIJ¿ÖÄÚÈÝ£º1Ç°¶ËÒ³ÃaeÖÆ×÷£¬2ÑeÖ¤ÂeÖÆ×÷£¬3ʵÏÖ×¢²aµÇ½£¬4¹¦ÄÜÍeÉÆ¡£¾ßÌaÇe¿o¿ÉÒÔÍuÏ¿´¡£
ÑeÖ¤ÂeÖÆ×÷
Ò»¡¢ÊµÑe¼o½e
±¾´ÎʵÑe½«»a´øÁi´o¼ÒʹÓÃÃaeÏo¶ÔÏoµÄ˼Ïe*a×°Ò»¸oÑeÖ¤ÂeÀa¡£²¢ÔÚ×¢²aºÍµÇ½½çÃaeչʾʹÓá£Í¨¹ý±¾´ÎʵÑeµÄѧϰ£¬Äa½«»aÁiÎoµ½ PHP µÄ OOP ˼Ïe£¬ÒÔ¼° GD ¿aµÄʹÓã¬ÑeÖ¤ÂeÉu³É¡£
1.1 Éae¼°µ½µÄ֪ʶµa
1.2 ¿ª*¢¹¤¾ß
sublime£¬Ò»¸o½±a¿iËÙµÄÎı¾±a¼Æ÷¡£µa»÷×ÀÃae×oϽǣº Ó¦ÓóÌÐo²Ëµ¥/¿ª¢/sublime
¶þ¡¢*a×°ÑeÖ¤ÂeÀa
2.1 ½¨Á¢Ä¿Â¼ÒÔ¼°×¼±¸×ÖÌa
ÔÚ web Ŀ¼Ï½¨Á¢Ò»¸o admin Ŀ¼×÷ΪÎÒÃǵĺǫĿ¼£¬´aeźǫ´uÂeÎļþ¡£ÔÚ admin Ͻ¨Á¢Ò»¸o fonts Ŀ¼£¬ÓÃÓÚ´aeÅÖÆ×÷ÑeÖ¤ÂeËuÐe×ÖÌa¡£
ÔÚ admin ÏÂн¨Ò»¸o Captcha.php Îļþ£¬Õa¾ÍÊÇÎÒÃÇÐeÒª±a¼µÄÑeÖ¤ÂeÀaÎļþ¡£
µ±Ç°Ä¿Â¼²a´Î½a¹¹£º
±a¼ Captcha.php Îļþ£º
<?php
/**
* Captcha class
*/
class Captcha
{
function __construct()
{
# code...
}
}
Ìi¼Ó¸ÃÀaµÄ˽ÓÐÊoÐԺ͹¹Ôi½¨£º
<?php
/**
* Captcha class
*/
class Captcha
{
private $codeNum; //ÑeÖ¤ÂeλÊý
private $width; //ÑeÖ¤ÂeͼƬ¿i¶È
private $height; //ÑeÖ¤ÂeͼƬ¸ß¶È
private $img; //ͼÏñ×ÊÔ´¾a±u
private $lineFlag; //ÊÇ*ñÉu³É¸ÉÈÅÏßÌo
private $piexFlag; //ÊÇ*ñÉu³É¸ÉÈŵa
private $fontSize; //×ÖÌa´oС
private $code; //ÑeÖ¤Âe×Ö*u
private $string; //Éu³ÉÑeÖ¤ÂeµÄ×Ö*u¼¯
private $font; //×ÖÌa
function __construct($codeNum = 4,$height = 50,$width = 150,$fontSize = 20,$lineFlag = true,$piexFlag = true)
{
$this->string = 'qwertyupmkjnhbgvfcdsxa123456789'; //È¥³ýÒ»Ð(C)Ïa½uµÄ×Ö*u
$this->codeNum = $codeNum;
$this->height = $height;
$this->width = $width;
$this->lineFlag = $lineFlag;
$this->piexFlag = $piexFlag;
$this->font = dirname(__FILE__).'/fonts/consola.ttf';
$this->fontSize = $fontSize;
}
}
×ÖÌaÎļþ¿Éͨ¹ýÒÔÏÂÃuÁiÏÂÔص½ fonts Ŀ¼£º
$ wget http://labfile.oss.aliyuncs.com/courses/587/consola.ttf
½ÓÏÂÀ´¿ªÊ¼±aд¾ßÌaµÄ½¨£º
´´½¨Í¼Ïñ×ÊÔ´¾a±u
//´´½¨Í¼Ïñ×ÊÔ´
public function createImage(){
$this->img = imagecreate($this->width, $this->height); //´´½¨Í¼Ïñ×ÊÔ´
imagecolorallocate($this->img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100)); //Ìi³aͼÏñ±³¾°£¨Ê¹ÓÃdzɫ£(C)
}
Óõ½µÄÏa¹Øº¯Êý
´´½¨ÑeÖ¤Âe×Ö*u´®²¢Êa³oµ½Í¼Ïñ
//´´½¨ÑeÖ¤Âe
public function createCode(){
$strlen = strlen($this->string)-1;
for ($i=0; $i < $this->codeNum; $i++) {
$this->code .= $this->string[mt_rand(0,$strlen)]; //´Ó×Ö*u¼¯ÖÐËae»uÈ¡³oËĸo×Ö*uÆ´½Ó
}
$_SESSION['code'] = $this->code; //¼ÓÈe session ÖÐ
//¼ÆËaÿ¸o×Ö*u¼a¾a
$diff = $this->width/$this->codeNum;
for ($i=0; $i < $this->codeNum; $i++) {
//Ϊÿ¸o×Ö*uÉu³ÉÑÕÉ«£¨Ê¹ÓÃÉiÉ«£(C)
$txtColor = imagecolorallocate($this->img,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
//дÈeͼÏñ
imagettftext($this->img, $this->fontSize, mt_rand(-30,30), $diff*$i+mt_rand(3,8), mt_rand(20,$this->height-10), $txtColor, $this->font, $this->code[$i]);
}
}
Óõ½µÄÏa¹Øº¯Êý
´´½¨ÑeÖ¤Âe×Ö*u´®²¢Êa³oµ½Í¼Ïñ
//´´½¨ÑeÖ¤Âe
public function createCode(){
$strlen = strlen($this->string)-1;
for ($i=0; $i < $this->codeNum; $i++) {
$this->code .= $this->string[mt_rand(0,$strlen)]; //´Ó×Ö*u¼¯ÖÐËae»uÈ¡³oËĸo×Ö*uÆ´½Ó
}
$_SESSION['code'] = $this->code; //¼ÓÈe session ÖÐ
//¼ÆËaÿ¸o×Ö*u¼a¾a
$diff = $this->width/$this->codeNum;
for ($i=0; $i < $this->codeNum; $i++) {
//Ϊÿ¸o×Ö*uÉu³ÉÑÕÉ«£¨Ê¹ÓÃÉiÉ«£(C)
$txtColor = imagecolorallocate($this->img,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
//дÈeͼÏñ
imagettftext($this->img, $this->fontSize, mt_rand(-30,30), $diff*$i+mt_rand(3,8), mt_rand(20,$this->height-10), $txtColor, $this->font, $this->code[$i]);
}
}
Óõ½µÄÏa¹Øº¯Êý£º
´´½¨¸ÉÈÅÏßÌo
//´´½¨¸ÉÈÅÏßÌo£¨Ä¬ÈÏËÄÌo£(C)
public function createLines(){
for ($i=0; $i < 4; $i++) {
$color = imagecolorallocate($this->img,mt_rand(0,155),mt_rand(0,155),mt_rand(0,155)); //ʹÓÃdzɫ
imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}
}
Óõ½µÄÏa¹Øº¯Êý£º
´´½¨¸ÉÈŵa
//´´½¨¸ÉÈŵa £¨Ä¬ÈÏÒ»°Ù¸oµa£(C)
public function createPiex(){
for ($i=0; $i < 100; $i++) {
$color = imagecolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagesetpixel($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}
}
ʹÓõÄÏa¹Øº¯Êý£º
¶ÔÍaÊa³oͼÏñ£º
public function show()
{
$this->createImage();
$this->createCode();
if ($this->lineFlag) { //ÊÇ*ñ´´½¨¸ÉÈÅÏßÌo
$this->createLines();
}
if ($this->piexFlag) { //ÊÇ*ñ´´½¨¸ÉÈŵa
$this->createPiex();
}
header('Content-type:image/png'); //ÇeÇoÒ³ÃaeµÄÄÚÈÝÊÇpng¸ñʽµÄͼÏñ
imagepng($this->img); //ÒÔpng¸ñʽÊa³oͼÏñ
imagedestroy($this->img); //Ça³ýͼÏñ×ÊÔ´£¬ÊÍ*ÅÄÚ´ae
}
Óõ½µÄÏa¹Øº¯Êý£º
¶ÔÍaÌa¹(C)ÑeÖ¤Âe£º
public function getCode(){
return $this->code;
}
ÍeÕu´uÂeÈçÏ£º
<?php
/**
* Captcha class
*/
class Captcha
{
private $codeNum;
private $width;
private $height;
private $img;
private $lineFlag;
private $piexFlag;
private $fontSize;
private $code;
private $string;
private $font;
function __construct($codeNum = 4,$height = 50,$width = 150,$fontSize = 20,$lineFlag = true,$piexFlag = true)
{
$this->string = 'qwertyupmkjnhbgvfcdsxa123456789';
$this->codeNum = $codeNum;
$this->height = $height;
$this->width = $width;
$this->lineFlag = $lineFlag;
$this->piexFlag = $piexFlag;
$this->font = dirname(__FILE__).'/fonts/consola.ttf';
$this->fontSize = $fontSize;
}
public function createImage(){
$this->img = imagecreate($this->width, $this->height);
imagecolorallocate($this->img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
}
public function createCode(){
$strlen = strlen($this->string)-1;
for ($i=0; $i < $this->codeNum; $i++) {
$this->code .= $this->string[mt_rand(0,$strlen)];
}
$_SESSION['code'] = $this->code;
$diff = $this->width/$this->codeNum;
for ($i=0; $i < $this->codeNum; $i++) {
$txtColor = imagecolorallocate($this->img,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
imagettftext($this->img, $this->fontSize, mt_rand(-30,30), $diff*$i+mt_rand(3,8), mt_rand(20,$this->height-10), $txtColor, $this->font, $this->code[$i]);
}
}
public function createLines(){
for ($i=0; $i < 4; $i++) {
$color = imagecolorallocate($this->img,mt_rand(0,155),mt_rand(0,155),mt_rand(0,155));
imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}
}
public function createPiexs(){
for ($i=0; $i < 100; $i++) {
$color = imagecolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagesetpixel($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}
}
public function show()
{
$this->createImage();
$this->createCode();
if ($this->lineFlag) {
$this->createLines();
}
if ($this->piexFlag) {
$this->createPiexs();
}
header('Content-type:image/png');
imagepng($this->img);
imagedestroy($this->img);
}
public function getCode(){
return $this->code;
}
}
ÒÔÉϾÍÊÇÑeÖ¤ÂeÀaµÄÈ«²¿´uÂe¡£¿´ÆðÀ´Èʵͦ¼oµ¥µÄ£¬²»¹ýÓõÄͼÏñ´¦Àiº¯Êý±È½Ï¶a£¬ÉÏÃaeÏa¹ØµÄº¯ÊýÎÒÒ²×oÁ˱ØÒªµÄÁ´½ÓºÍÓÃ;˵Ã÷¡£ÕaÐ(C)º¯ÊýÒ²²»ÓÃËÀ¼ÇÓ²±³£¬Óoµ½²»Ça³þµÄ£¬Ëaeʱ²eÔÄ PHP ¹Ù½Îĵµ£¬×iÖØÒªµÄÊÇ»¹ÓÐÖÐÎÄÎĵµ¡£
2.2 ʹÓÃÑeÖ¤Âe
¼ÈÈ»ÒѾa×°Íe±Ï£¬ÄǾͿÉÒÔ¿ªÊ¼Ê¹ÓÃÁË¡£ÕaÀiΪÁ˽±a£¬Ö±½ÓÔÚ Captcha ÀaµÄÏÂ*½µ÷ÓøÃÀa£º
session_start(); //¿ªÆosession
$captcha = new Captcha(); //ʵÀý»¯ÑeÖ¤ÂeÀa(¿É×Ô¶¨Òa²ÎÊý)
$captcha->show(); //µ÷ÓÃÊa³o
Èý¡¢Ç°¶Ëչʾ
ºo¶ËÒѾ׼±¸ºÃÁËÑeÖ¤Âe£¬Ç°¶Ë½çÃae¾Í¿ÉÒÔչʾÁË£¬ÐÞ¸Ä index.php ÖеÄ×¢²aÓeµÇ½±iµ¥µÄÑeÖ¤Âe²¿*Ö£º
<div class="form-group">
<div class="col-sm-12">
<img src="admin/Captcha.php" alt="" id="codeimg" onclick="javascript:this.src = 'admin/Captcha.php?'+Math.random();">
<span>Click to Switch</span>
</div>
</div>
img ±eÇ(C)Ìi¼ÓÁ˵a»÷ʼþµÄ js ´uÂe£¬ÕaÑu¾Í¿ÉÒÔʵÏÖµa»÷¸u»»ÑeÖ¤ÂeµÄ¹¦ÄÜ£¡
Ч¹uͼ£º
ËÄ¡¢ÍeÉÆ
µ½Ä¿Ç°ÎªÖ¹£¬ÎÒÃǵÄÑeÖ¤ÂeÄ£¿e»u±¾¾ÍÍe³ÉÁË¡£Ñ§Ï°µ½ÕaÀi£¬´o¼ÒÓ¦¸Ã¶ÔÃaeÏo¶ÔÏo±a³ÌÓÐÁ˽øÒ»²½µÄÀi½a¡£Ò²ÁiÎoµ½ÁËһ˿ OOP ˼Ïe¡£OOP µÄÈý´oÌØÕ÷£ºa×°£¬¼Ì³Ð£¬¶a̬¡£ÎÒÃÇÕaÀiÖ»Óõ½ÁËÒ»µaa×°µÄ˼Ïe¡£´o¼Ò¿ÉÒÔ¼ÌÐøÍeÉƺ͸ĽøÕa¸oÑeÖ¤ÂeÀa£¬Ée¼Æ³o¸u¼ÓÍeÃÀµÄÀa¡£Õa¸oʵÑeÒ²¸aeËßÎÒÃÇ£¬PHP µÄº¯ÊýºÜ¶a£¬²»ÒªËÀ¼ÇÓ²±³£¬¶a¿´¹Ù*½Îĵµ¡£