Skip to content

Instantly share code, notes, and snippets.

@devluis
Forked from Swader/demo.php
Created March 15, 2014 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devluis/9561679 to your computer and use it in GitHub Desktop.
Save devluis/9561679 to your computer and use it in GitHub Desktop.
<?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