您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

Cakephp中使用Captcha实现更加安全的验证码

2024/4/24 13:43:16发布5次查看
首先可以使用如下程序得到验证码图片,注意在程序在生成图片时就调设置了会话变量。
getimage.php
<?php
include('kcaptcha.php');
session_start();
$captcha = new kcaptcha();
$_session['captcha_keystring'] = $captcha->getkeystring();
?>
接下来通过如下的表单调用验证码图片,并验证用户输入是否与验证码图片值相符。
index.php
<?php
session_start();
$true_key_string = $_session['captcha_keystring'];
echo $true_key_string;
?>
<html>
<body>
<form action="" method="post">
<img src="getimage.php?<?php echo session_name()?>=<?php echo session_id()?>/><br/>
<input type="text" value="" name="keystring"/>
<input type="submit" value="check" />
</form>
<?php
if(isset($_session['captcha_keystring']) && $true_key_string == $_post['keystring'])
{
echo "correct";
}else
{
echo "wrong";
}
?>
</body>
</html>
那么,如何在cakephp中使用captcha呢?
首先把kcaptcha文件夹拷贝到vendor目录。因为验证码要在很多控制器中使用,因此最好把该项功能用组件进行封装,该组件包括生成图片和验证两项功能。在controllers/components目录下新建captcha.php文件。
<?php
class captchacomponent extends object {
var $controller = null;
function startup(&$controller)
{
$this->controller = $controller;
}
function render()
{
app::import('vendor', 'kcaptcha/kcaptcha');
$kcaptcha = new kcaptcha();
$this->controller->session->write('captcha', $kcaptcha->getkeystring());
exit;
}
function checkcaptcha($str)
{
if ($this->controller->session->check('captcha'))
{
$s_captcha = $this->controller->session->read('captcha');
if(!empty($str) && $str == $s_captcha)

return true;
}
}
return false;
}
}
?>
接下来,为了能在视图中调用验证码图片,可以在控制器(比如users控制器)中加入captcha组件。或者单独创建一个captchas控制器来生成验证码图片。
<?php
class captchascontroller extends appcontroller {
var $name = 'captchas';
var $uses = array();
var $components = array('captcha');
var $helps = array('cache');
var $cacheaction = true;
function index() {
configure::write('debug', '0');
$this->autorender = false;
$this->captcha->render();
}
}
?>
 以上就是
cakephp中使用captcha实现更加安全的验证码的内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录