Jump to content

php captcha


yandoo

Recommended Posts

Hiya I'm trying to get a captcha script to work but the images never appears. I've check out that the GD2 extension is enabled in wamp and also checked the php.ini file and can confirm extension=php_gd2.dll is without the semicolon so enabled. 

 

I'm following a tutorial and have the source code to test it so assume its something wrong with the installion of GD2? 

 

Any ideas?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/280251-php-captcha/
Share on other sites

I've tested it on another webserver with gd_2 enabled and it still doesnt show the image, maybe theres a problem with the code?

captcha.php

<?php
/* captcha.php file*/

	session_start();
	
	header("Expires: Tue, 01 Jan 2013 00:00:00 GMT"); 
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
	header("Cache-Control: no-store, no-cache, must-revalidate"); 
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache");
	
	$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';

    for ($i = 0; $i < 5; $i++) 
    {
        $randomString .= $chars[rand(0, strlen($chars)-1)];
    }
	
	$_SESSION['captcha'] = strtolower( $randomString );
	
	
	$im = @imagecreatefrompng("captcha_bg.png"); 
	

	imagettftext($im, 30, 0, 10, 38, imagecolorallocate ($im, 0, 0, 0), 'larabiefont.ttf', $randomString);
	
	header ('Content-type: image/png');
	imagepng($im, NULL, 0);
	imagedestroy($im);

?>

captcha-validate.php

<?php
/* captcha-validate file */

session_start();

if(strtolower($_POST['answer']) == $_SESSION['captcha'])
	echo 'Captcha solved sucesfully, now you can allow this user to submit comment/vote/upload/etc.';
else
	echo 'Sorry, captcha not solved. Offer user captcha again or what ever.';
?>

demo.html

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

		<title>Simple PHP captcha demo</title>
		<meta name="viewport" content="width=device-width; initial-scale=1.0" />
	</head>

	<body>
		<div>
			<header>
				<h1>Simple PHP captcha demo</h1>
			</header>
			
			<div>
				<img src="captcha.php" /> 
				<form action="captcha-validate.php" method="post">
					<input type="text" name="answer" placeholder="Enter captcha here" />
					<input type="submit" value="CHECK" />
					<input type="button" onClick="window.location.href = window.location.href" value="Reload" />					
				</form>

			</div>

			<footer>
				<p>
					© Copyright  by <a href="http://www.ivebe.com">Danijel Petrovic</a>
				</p>
			</footer>
		</div>
	</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/280251-php-captcha/#findComment-1441127
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.