Code Monkey home page Code Monkey logo

phimcap's Introduction

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

Requirements

  • Linux
  • php >=7.4
    • GD Library extension
  • existance of a true type font like FreeMono.ttf

Install

{
  "require": {
    "gueff/phimcap":"1.0.*"
  }
}

How to use

... a forgotPassword - Example using myMVC 3.2.x

assuming your primary working Module is called Foo:

1) dealing with html form

create a MIX /forgotPassword/ route, leading to \Foo\Controller\Index::forgotPassword

\MVC\Route::MIX(
    ['GET', 'POST'],
    '/forgotPassword/',
    '\Foo\Controller\Index::forgotPassword',
    $oDTRoutingAdditional
        ->set_sLayout($sTheme . '/Frontend/layout/index.tpl')
        ->set_sContent($sTheme . '/Frontend/content/forgotPassword.tpl')
        ->getPropertyJson()
);

\Foo\Controller\Index::forgotPassword: create new captcha text & save to Session

public function forgotPassword()
{
    // grab for POSTed captcha; sanitize
    $sCaptcha = substr(
        preg_replace("/[^\\p{L}\\p{N}']+/u", '', get($_POST['captcha'])),
        0,
        strlen(Session::is()->get('mymvc.captcha'))
    );

    if (
        ($sCaptcha === Session::is()->get('mymvc.captcha'))
    )
    {
        // stuff...
    }

    // create new captcha text & save to session
    $sCaptchaText = \Phimcap::text();
    Session::is()->set('mymvc.captcha', $sCaptchaText);

    $sContent = $this->oView->loadTemplateAsString('/Frontend/content/forgotPassword.tpl');
    $this->oView->assign('sContent', $sContent);
}

template /Frontend/content/forgotPassword.tpl

<form action="" method="post">
    <label for="captcha">Captcha</label>
    <img src="/captcha/">
    <input type="text"
           name="captcha"
           id="captcha" 
           class="form-control"
           value=""
           maxlength="5"
           placeholder="captcha code"
    >
</form>

2) serving the captcha image

create \Foo\Controller\Index::captcha

public function captcha()
{
    \Phimcap::image(
        Session::is()->get('mymvc.captcha')
    );
}
  • here the captcha text for the image is taken from session

create a /captcha/ route, leading to \Foo\Controller\Index::captcha

\MVC\Route::GET(
    '/captcha/',
    '\Foo\Controller\Index::captcha'
);

So the captcha image is callable via

<img src="/captcha/">

Customizing

using different Font

Phimcap uses /usr/share/fonts/truetype/freefont/FreeMono.ttf, which is likely to be installed by default on Ubuntu Linux systems.

If you want a different Font, add the absolute path as second parameter:

\Phimcap::image(
    Session::is()->get('mymvc.captcha'),
    '/usr/share/fonts/truetype/msttcorefonts/andalemo.ttf'
);

text length

per default the text length is set to 5.

you can change it to a value between 5 up to 10 by adding a value as parameter.

$sCaptchaText = \Phimcap::text(10);

phimcap's People

Contributors

gueff avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.