Captcha php captcha class php

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

License

Gregwar/Captcha

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Читайте также:  Css hide text with dots

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Captchas examples

You can create a captcha with the CaptchaBuilder :

 use Gregwar\Captcha\CaptchaBuilder; $builder = new CaptchaBuilder; $builder->build();

You can then save it to a file :

 header('Content-type: image/jpeg'); $builder->output();

Or inline it directly in the HTML page:

 $builder->inline(); ?>" />

You’ll be able to get the code and compare it with a user input :

 // Example: storing the phrase in the session to test for the user // input later $_SESSION['phrase'] = $builder->getPhrase();

You can compare the phrase with user input:

if($builder->testPhrase($userInput)) < // instructions if user phrase is good > else < // user phrase is wrong >

You can use theses functions :

  • __construct($phrase = null), constructs the builder with the given phrase, if the phrase is null, a random one will be generated
  • getPhrase(), allow you to get the phrase contents
  • setDistortion($distortion), enable or disable the distortion, call it before build()
  • isOCRReadable(), returns true if the OCR can be read using the ocrad software, you’ll need to have shell_exec enabled, imagemagick and ocrad installed
  • buildAgainstOCR($width = 150, $height = 40, $font = null), builds a code until it is not readable by ocrad
  • build($width = 150, $height = 40, $font = null), builds a code with the given $width, $height and $font. By default, a random font will be used from the library
  • save($filename, $quality = 80), saves the captcha into a jpeg in the $filename, with the given quality
  • get($quality = 80), returns the jpeg data
  • output($quality = 80), directly outputs the jpeg code to a browser
  • setBackgroundColor($r, $g, $b), sets the background color to force it (this will disable many effects and is not recommended)
  • setBackgroundImages(array($imagepath1, $imagePath2)), Sets custom background images to be used as captcha background. It is recommended to disable image effects when passing custom images for background (ignore_all_effects). A random image is selected from the list passed, the full paths to the image files must be passed.
  • setInterpolation($interpolate), enable or disable the interpolation (enabled by default), disabling it will be quicker but the images will look uglier
  • setIgnoreAllEffects($ignoreAllEffects), disable all effects on the captcha image. Recommended to use when passing custom background images for the captcha.
  • testPhrase($phrase), returns true if the given phrase is good
  • setMaxBehindLines($lines), sets the maximum number of lines behind the code
  • setMaxFrontLines($lines), sets the maximum number of lines on the front of the code

If you want to change the number of character, you can call the phrase builder directly using extra parameters:

use Gregwar\Captcha\CaptchaBuilder; use Gregwar\Captcha\PhraseBuilder; // Will build phrases of 3 characters $phraseBuilder = new PhraseBuilder(4); // Will build phrases of 5 characters, only digits $phraseBuilder = new PhraseBuilder(5, '0123456789'); // Pass it as first argument of CaptchaBuilder, passing it the phrase // builder $captcha = new CaptchaBuilder(null, $phraseBuilder);

You can also pass directly the wanted phrase to the builder:

// Building a Captcha with the "hello" phrase $captcha = new CaptchaBuilder('hello');

If you want to see an example you can have a look at the demo/form.php , which uses demo/session.php to render a captcha and check it after the submission

You can have a look at the following repository to enjoy the Symfony 2 bundle packaging this captcha generator : https://github.com/Gregwar/CaptchaBundle

You can use the following extension for integrating with Yii2 Framework : https://github.com/juliardi/yii2-captcha

This library is under MIT license, have a look to the LICENSE file

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

A simple PHP CAPTCHA Class

gesf/captcha.class.php

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.markdown

This is a simple CAPTCHA Class, written in PHP4.

s: user defined captcha text

More settings can be changed in the class .

Just call the captcha.php file and pass the desired type and/or a predefined captcha text.

Output: A 6 digits random number (letters are discarded)

Output: A 6 digits random string (Lower/upper letters + numbers)

3 : Letters Only (upper and lower case)

4 : Lowercase Letters and Numbers

5 : Uppercase Letters and Numbers

Captcha php captcha class php

The image:

Verification code (sample): See captcha.php file .

This is a PHP4-like class, however it should work unchanged under PHP5. You can find more details in the code.

See gpl.txt and lgpl.txt for licensing terms.

Источник

3 Steps Simple Captcha In PHP (Free Download)

Welcome to a tutorial on how to create a simple captcha in PHP. Have a website that is constantly being spammed by angry keyboard warriors? Time to put in some security and prevention measures. Let us walk through an example in this guide, without the use of any 3rd party frameworks – Read on!

TABLE OF CONTENTS

PHP CAPTCHA

All right, let us now get into the details of creating a simple PHP captcha.

STEP 1) PHP CAPTCHA CLASS

 if (session_status()==PHP_SESSION_NONE) < session_start(); >> // (C) PRIME THE CAPTCHA - GENERATE RANDOM STRING IN SESSION function prime () : void < $char = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $max = strlen($char) - 1; $_SESSION["captcha"] = ""; for ($i=0; $ilength; $i++) < $_SESSION["captcha"] .= substr($char, rand(0, $max), 1); >> // (D) DRAW THE CAPTCHA IMAGE function draw ($mode=0) : void < // (D1) FUNKY BACKGROUND IMAGE if (!isset($_SESSION["captcha"])) < exit("Captcha not primed"); >$captcha = imagecreatetruecolor($this->capW, $this->capH); list($bx, $by) = getimagesize($this->capB); $bx = ($bx-$this->capW) < 0 ? 0 : rand(0, ($bx-$this->capW)); $by = ($by-$this->capH) < 0 ? 0 : rand(0, ($by-$this->capH)); imagecopy($captcha, imagecreatefromjpeg($this->capB), 0, 0, $bx, $by, $this->capW, $this->capH); // (D2) RANDOM TEXTBOX POSITION $ta = rand(-20, 20); // random angle $tc = imagecolorallocate($captcha, rand(120, 255), rand(120, 255), rand(120, 255)); // random text color $ts = imagettfbbox($this->capFS, $ta, $this->capF, $_SESSION["captcha"]); if ($ta>=0) < $tx = rand(abs($ts[6]), $this->capW - (abs($ts[2]) + abs($ts[6]))); $ty = rand(abs($ts[5]), $this->capH); > else < $tx = rand(0, $this->capW - (abs($ts[0]) + abs($ts[4]))); $ty = rand(abs($ts[7]), abs($ts[7]) + $this->capH - (abs($ts[3]) + abs($ts[7]))); > imagettftext($captcha, $this->capFS, $ta, $tx, $ty, $tc, $this->capF, $_SESSION["captcha"]); // (D3) OUTPUT CAPTCHA if ($mode==0) < ob_start(); imagepng($captcha); $ob = base64_encode(ob_get_clean()); echo ""; > else < header("Content-type: image/png"); imagepng($captcha); imagedestroy($captcha); >> // (E) VERIFY CAPTCHA function verify ($check) < if (!isset($_SESSION["captcha"])) < exit("Captcha not primed"); >if ($check == $_SESSION["captcha"]) < unset($_SESSION["captcha"]); return true; >else < return false; >> > // (F) CAPTCHA OBJECT $PHPCAP = new Captcha();

First, we start with the “core engine”, the Captcha library. Yes, this looks complicated at first, but keep calm and explore it slowly.

  • (A) A whole bunch of captcha settings. Feel free to change to your own font, change the background, captcha image dimensions, etc…
  • (B & F) When $PHPCAP = new Captcha() is created, the constructor automatically starts the session if it is not already started.
  • (C To E) There are only 3 “real captcha functions”.
    • prime() Step 1 of the process, pretty much just generates a random string into $_SESSION[«captcha»] .
    • draw() Step 2 of the process, use this to output the HTML captcha image.
    • verify() When the user submits the form, use this to verify the user’s captcha against the session.

    STEP 2) GENERATE CAPTCHA IN HTML FORM

    prime(); $PHPCAP->draw(); ?>

    Источник

Оцените статью