Created
November 27, 2015 17:11
-
-
Save AlifArnado/cc683fc9ab1ee2fc6a15 to your computer and use it in GitHub Desktop.
membuat captcha menggunakan php image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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("error image"); | |
$background_color = imagecolorallocate($image, 255, 225, 255); | |
$text_color = imagecolorallocate($image, 0, 225, 225); | |
$line_color = imagecolorallocate($image, 64, 64, 64); | |
$pixel_color = imagecolorallocate($image, 0, 0, 225); | |
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 $images_to_delete) { | |
@unlink($image_to_delete); | |
} | |
imagepng($image, "image" . $_SESSION['count']. ".png"); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment