Skip to content

gueff/myMVC_module_Captcha

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

this myMVC module provides a captcha image you could use in your own html forms.

Requirements


Install

cd into the modules folder of your myMVC3.2.x copy; e.g.:

cd /var/www/myMVC/modules/;

clone myMVC_module_Captcha as Captcha

git clone --branch 1.0.x https://github.com/gueff/myMVC_module_Captcha.git Captcha;

How to use

1. add the following route to your primary working Module

// captcha route
\MVC\Route::GET(
    \Captcha\Model\Index::$sRoute,
    '\Captcha\Controller\Index::index'
);

2. add captcha check and creation in your primary working Module's Controller

// check if captcha is valid
if (true === \Captcha\Model\Index::captchaIsValid())
{
    // captcha is OK!
}

// create captcha for new
$sCaptchaText = \Captcha\Model\Index::createCaptcha();

3. add this captcha Formular to your Frontend template

<form action="" method="post">

  <!-- captcha -->
  <label for="captcha">Captcha</label>
  <img src="{Captcha\Model\Index::$sRoute}">
  <input type="text"
         name="{Captcha\Model\Index::$sPostFieldName}"
         maxlength="{Captcha\Model\Index::$iCaptchaTextLength}"
         value=""
         placeholder="captcha code"
         autofocus
  >
  <!-- /captcha -->

  <button type="submit">submit</button>
</form>

🛈 Note: make sure your Route allows POST method

allow POST method in Routes you want to make use of Captcha.

\MVC\Route::MIX(
    ['GET', 'POST'],
    '/',
    '\Foo\Controller\Index::index',
    $oDTRoutingAdditional->getPropertyJson()
);

Customizing

In your primary Module's environment config file simply change the properties of the Captcha Model Class.

Here is an Example

//-------------------------------------------------------------------------------------
// Module Captcha

// load Class
require_once $aConfig['MVC_MODULES_DIR'] . '/Captcha/Model/Index.php';

// declare a custom route
\Captcha\Model\Index::$sRoute = '/captcha/MyFormularXY/';

// add some extra chars to choose from...
\Captcha\Model\Index::$sChar.= '_!=)(/&%$[].:,;+*~#';
// ...if you changed the $sChar, do not forget to adjust sanitizing rule
\Captcha\Model\Index::$sCharSanitizePattern = "/[^\\p{L}}\\p{M}\\p{S}\\p{N}\\p{P}']+/u";

// auto-name template input var (name="") like Route
\Captcha\Model\Index::$sPostFieldName = str_replace('/', '', \Captcha\Model\Index::$sRoute);

// set SessionName like $sPostFieldName
\Captcha\Model\Index::$sSessionName = \Captcha\Model\Index::$sPostFieldName;