Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 03:36
Show Gist options
  • Save code-boxx/196e2904c2f18cfdf0365684abc8eccd to your computer and use it in GitHub Desktop.
Save code-boxx/196e2904c2f18cfdf0365684abc8eccd to your computer and use it in GitHub Desktop.
Simple PHP Captcha

PHP CAPTCHA

https://code-boxx.com/simple-php-captcha-script/

IMAGES

CapBack

NOTES

  1. Please enable the GD extension extension=gd (gd2 prior to PHP8) in php.ini.
  2. Change the captcha font $capF in 1-captcha.php to your own.
  3. Launch 2-form.php in your browser, that's all.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?php
class Captcha {
// (A) CAPTCHA SETTINGS
private $length = 8; // number of characters
private $capW = 300; // captcha width
private $capH = 100; // captcha height
private $capF = "C:/Windows/Fonts/arial.ttf"; // font - CHANGE TO YOUR OWN!
private $capFS = 24; // captcha font size
private $capB = __DIR__ . DIRECTORY_SEPARATOR . "CapBack.jpg"; // background image
// (B) AUTO START SESSION
function __construct () {
if (session_status()==PHP_SESSION_DISABLED) { exit("Sessions disabled"); }
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; $i<=$this->length; $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 "<img src='data:image/png;base64,$ob'>";
} 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();
<!DOCTYPE html>
<html>
<head>
<title>PHP Captcha Demo</title>
<meta charset="utf-8">
<link rel="stylesheet" href="x-dummy.css">
</head>
<body>
<form method="post" action="3-submit.php">
<!-- (A) FORM FIELDS -->
<label>Name</label>
<input name="name" type="text" required value="Jon Doe">
<label>Email</label>
<input name="email" type="email" required value="jon@doe.com">
<!-- (B) CAPTCHA HERE -->
<label>Are you human?</label>
<?php
require "1-captcha.php";
$PHPCAP->prime();
$PHPCAP->draw();
?>
<input name="captcha" type="text" required>
<!-- (C) GO! -->
<input type="submit" value="Go!">
</form>
</body>
</html>
<?php
// (A) LOAD CAPTCHA LIBRARY
require "1-captcha.php";
// (B) VERIFIED - DO SOMETHING
if ($PHPCAP->verify($_POST["captcha"])) {
$result = "Congrats, CAPTCHA is correct.";
print_r($_POST);
}
// (C) NOPE
else { echo "CAPTCHA does not match!"; }
/* (X) NOT IMPORTANT - COSMETICS */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
form {
max-width: 400px;
padding: 15px;
border: 1px solid #efefef;
background: #f5f5f5;
}
label, input {
display: block;
width: 100%;
}
label {
margin: 10px 0;
}
label:first-child { margin-top: 0; }
input {
padding: 10px;
border: 1px solid #e5e5e5;
}
img {
display: block;
margin: 10px auto;
}
input[type=submit] {
margin-top: 20px;
border: 0;
color: #fff;
background: #3c58bf;
cursor: pointer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment