Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Mehul Jain's captcha sample

View demo.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
<?php
session_start();
$_SESSION['count'] = time();
$image;
?>
 
<title>demo.php</title>
<body style="background-color:#ddd; ">
 
<?php
$flag = 5;
if (isset($_POST["flag"])) {
$input = $_POST["input"];
$flag = $_POST["flag"];
}
if ($flag == 1) {
if ($input == $_SESSION['captcha_string']) {
?>
 
<div style="text-align:center;">
<h1>Your answer is correct!</h1>
 
<form action=" <?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="submit" value="refresh the page">
</form>
</div>
 
<?php
} else {
?>
 
<div style="text-align:center;">
<h1>Your answer is incorrect!<br>please try again </h1>
</div>
 
<?php
create_image();
display();
}
} else {
create_image();
display();
}
function display()
{
?>
 
<div style="text-align:center;">
<h3>TYPE THE TEXT YOU SEE IN THE IMAGE</h3>
<b>This is just to check if you are a robot</b>
 
<div style="display:block;margin-bottom:20px;margin-top:20px;">
<img src="image<?php echo $_SESSION['count'] ?>.png">
</div>
<form action=" <?php echo $_SERVER['PHP_SELF']; ?>" method="POST"
/ >
<input type="text" name="input"/>
<input type="hidden" name="flag" value="1"/>
<input type="submit" value="submit" name="submit"/>
</form>
 
<form action=" <?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="submit" value="refresh the page">
</form>
</div>
 
<?php
}
function create_image()
{
global $image;
$image = imagecreatetruecolor(200, 50) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 255, 255);
$line_color = imagecolorallocate($image, 64, 64, 64);
$pixel_color = imagecolorallocate($image, 0, 0, 255);
imagefilledrectangle($image, 0, 0, 200, 50, $background_color);
for ($i = 0; $i < 3; $i++) {
imageline($image, 0, rand() % 50, 200, rand() % 50, $line_color);
}
for ($i = 0; $i < 1000; $i++) {
imagesetpixel($image, rand() % 200, rand() % 50, $pixel_color);
}
$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$len = strlen($letters);
$letter = $letters[rand(0, $len - 1)];
$text_color = imagecolorallocate($image, 0, 0, 0);
$word = "";
for ($i = 0; $i < 6; $i++) {
$letter = $letters[rand(0, $len - 1)];
imagestring($image, 7, 5 + ($i * 30), 20, $letter, $text_color);
$word .= $letter;
}
$_SESSION['captcha_string'] = $word;
$images = glob("*.png");
foreach ($images as $image_to_delete) {
@unlink($image_to_delete);
}
imagepng($image, "image" . $_SESSION['count'] . ".png");
}
?>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.