First, you can use the following program to get the verification code image. Note that the session variable is adjusted when the program generates the image.
getImage.php
include('kcaptcha.php');
session_start();
$captcha = new KCAPTCHA();
$_SESSION['captcha_keystring'] = $captcha->getKeyString();
?>
Next, call the verification code image through the following form, and verify whether the user input matches the verification code image value.
index.php
session_start();
$true_key_string = $_SESSION['captcha_keystring'];
echo $true_key_string;
?>
< html>
if(isset($_SESSION['captcha_keystring']) && $true_key_string == $_POST['keystring'])
{
echo "Correct";
}else
{
echo "Wrong";
}
?>
So, how? How to use Captcha in Cakephp?
First copy the Kcaptcha folder to the vendor directory. Because the verification code will be used in many controllers, it is best to encapsulate this function with a component. This component includes two functions: image generation and verification. Create a new captcha.php file in the controllers/components directory.
class CaptchaComponent extends Object {
var $Controller = null;
function startup(&$controller)
{
$this->Controll er = $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;
}
}
?>
Next, in order to call the verification code image in the view, you can add the Captcha component to the controller (such as the Users controller). Or create a separate Captchas controller to generate verification code images.
class CaptchasController extends AppController {
var $name = 'Captchas';
var $uses = array();
var $components = array('Capt cha ');
var $helps = array('Cache');
var $cacheAction = true;
function index() {
Configure::write('debug', '0');
$this->autoRender = false;
$this->Captcha->render();
}
}
?>
The above is
Cakephp using Captcha to achieve more security For the content of the verification code, please pay attention to the PHP Chinese website (m.sbmmt.com) for more related content!