Jump to content

php captcha


yandoo
Go to solution Solved by .josh,

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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.