<?php /**********************************************\ * Copyright (c) 2013 Manolis Agkopian * * See the file LICENCE for copying permission. * \**********************************************/ require_once 'vendor/autoload.php'; session_start(); $mathCaptcha = new MathCaptcha\MathCaptcha(); try { $mathCaptcha->generate(); $mathCaptcha->output(); } catch (MathCaptcha\MathCaptchaException $e) { // Here you normally log the error, and you can output an error image // to notify the user that something went wrong, if you want. }
<?php /**********************************************\ * Copyright (c) 2013 Manolis Agkopian * * See the file LICENCE for copying permission. * \**********************************************/ require_once 'vendor/autoload.php'; session_start(); $msg = ''; // Check if form has been submited if (isset($_POST['submit']) && !empty($_POST['submit'])) { // Check if user answered the question if (!isset($_POST['captcha_ans']) || $_POST['captcha_ans'] === '') { $msg = '<span class="error">Please fill the answer to the math question</span>'; } else { $mathCaptcha = new MathCaptcha\MathCaptcha(); // Validate the answer if ($mathCaptcha->check($_POST['captcha_ans']) === true) { // In a real application here you can register/login the user, insert a comment in the database etc $msg = '<span class="success">SUCCESS</span>'; } else { $msg = '<span class="error">You didn\'t answered the question correctly</span>'; } } } ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>PHP Math Captcha</title>