PHP code example of jacopovalanzano / php-captcha

1. Go to this page and download the library: Download jacopovalanzano/php-captcha library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jacopovalanzano / php-captcha example snippets

  
    // Create a new Captcha  
    $captcha = new Captcha("My super difficult to read string.");  
  
    // Add 3 lines over and 3 behind the text,  
    // then build the image.  
    $captcha->linesFront(3)->linesBack(3)->build(175,50); // width, height  
  
    // Returns a string containing the captcha passphrase  
    $captcha->getPassphrase(); // Returns "My super difficult to read string."  
  
    // Renders the captcha.  
    $captcha->out();  
  
  
    // This file represents the "www.example.com/get_captcha_image" url that generates our captcha  
   
    // ...      
  
    // A list of words  
    $attributes = [ "easy", "green", "digital" ];  
  
    // One more list of words  
    $nouns = [ "compare", "dungeon", "clip" ];  
  
    // Compose a phrase  
    $words = $attributes[array_rand($attributes)]." ".$nouns[array_rand($nouns)];  
  
    // Create a new captcha with some random words  
    $captcha = new Captcha($words);  
  
    // Add 2 lines over and 5 behind the text,  
    // then build the image.  
    $captcha->linesFront(2)->linesBack(5)->build(175,50); // width, height  
  
    // Save the captcha passphrase to session, so it can be retrieved later...   
    $_SESSION["captcha_passphrase"] = $captcha->getPassphrase();  
  
    // Render the actual captcha image  
    $captcha->out();  
  
    // Compare the captcha passphrase with the one submitted  
    if($_POST["captcha_passphrase"] !== $_SESSION["captcha_passphrase"]) {  
        die("Wrong captcha!");  
    }  
bash  
composer 

echo '<img src="' . $captcha->inline() . '">';