user4616313
user4616313

Reputation: 53

implementing php captcha into laravel framework

I am trying to get this captcha working in the laravel framework. I want it to work with a contactus page but I have no clue how to implement the php section into the laravel framework as I am trying to put it in the post contact us function in the controller but it breaks my program

The Html in the view

<img id="captcha" src="/securimage/securimage_show.php" alt="CAPTCHA Image" />
 <input type="text" name="captcha_code" size="10" maxlength="6" />

 <a href="#" onclick="document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); return false">[ DifferentImage ]</a>

This code I am trying to get in the contoller:

include_once $_SERVER['DOCUMENT_ROOT'] . '/securimage/securimage.php';

$securimage = new Securimage();
if ($securimage->check($_POST['captcha_code']) == false) {
  // the code was incorrect
  // you should handle the error so that the form processor doesn't continue

  // or you can use the following code if there is no validation or you do not know how                   
  echo "The security code entered was incorrect.<br /><br />";
  echo "Please go <a href='javascript:history.go(-1)'>back</a> and try again.";
  exit;
}

Here is the : documentation https://www.phpcaptcha.org/documentation/quickstart-guide/

Upvotes: 2

Views: 2075

Answers (1)

vladsch
vladsch

Reputation: 606

I am assuming that you ran the securimage_test.php on your server and the test passes. Here is what I did to get it working.

Add this line to your app's composer.json, in the "require": { ... },:

    "dapphp/securimage": "~3.5",

run composer update to install the securimage package and add its class to the autoload.

For the image and input I added this into my form:

<img id="captcha" style="width: 241px; height: 80px;" src="/captcha" alt="CAPTCHA Image" />
<input class="form-control" type="text" name="captcha_code" size="10" maxlength="6" />
<a href="#" onclick="document.getElementById('captcha').src = '/captcha?' + Math.random(); return false">[Generate a new image]</a>

Add '/captcha' or any other URL you want to use as the captcha image to your routes file to point to a function that is a copy of the function in securimage_show.php

in routes.php:

Route::get('/captcha', 'HomeController@getCaptcha');

in my case it is in HomeController.php:

public
function getCaptcha()
{
    $img = new Securimage();

    // set namespace if supplied to script via HTTP GET
    if (!empty($_GET['namespace'])) $img->setNamespace($_GET['namespace']);

    $img->show();  // outputs the image and content headers to the browser
    // alternate use:
    // $img->show('/path/to/background_image.jpg');
}

Then in the post method wherever you validate your form, add the validation for the captcha:

    $image = new Securimage();
    if ($image->check(Input::get('captcha_code')) !== true)
    {
        // add however you handle feedback to the user here
        $errors = new MessageBag();
        $errors->add('captcha_code', 'Text you entered does not match');
        return Redirect::back()
            ->with('errors', $errors)
            ->withInput(Input::only('email', 'name'));
    }
    // text matches, go on processing

Upvotes: 2

Related Questions