Need Code for generating Security Image

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
srikanth
Forum Newbie
Posts: 2
Joined: Mon Feb 27, 2006 1:16 pm

Need Code for generating Security Image

Post by srikanth »

Hi


can any one give me the link for any good site for the code refference for generating Security Image in PHP??!!


Thanks,

Srikanth.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

do you mean CAPTCHA? as in they have to input a text based image into a field?

if so to start you off
srikanth
Forum Newbie
Posts: 2
Joined: Mon Feb 27, 2006 1:16 pm

Post by srikanth »

Thanks........ :D
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

You might get problems, like no image appearing this is because your server may not support image creation functions.

In this case you would have to contact your host and ask them to enable it.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Here is a very simple one:

Code: Select all

<?php

session_start();

if(isset($_GET['i'])){
	
	// image creation section
	captcha_image();
}elseif(isset($_POST['captcha'])){
	
	// validation section
	echo(captcha_validate())?'That was correct.<br>':'That was not correct.<br>';
	echo '<a href="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'">Try another?</a>';
	exit;
	
}else{
	
	// form section
	echo
	'<img src="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?i='.uniqid().'" alt=""><br>'."\n".
	'<form action="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'" method="POST">'."\n".
	'<input type="text" name="captcha" ><input type="submit" value="test it"></form>';
	
}
	

function captcha_validate()
{
	return($_POST['captcha'] == $_SESSION['captcha']);
}

function captcha_image()
{
	$_SESSION['captcha'] = substr(uniqid(), 0, ;
	$image = imagecreate(80, 20);
	$background_colour = imagecolorallocate($image, 255,255,255);
	$text_shadow = imagecolorallocate($image, 127,127,127);
	$text_colour = imagecolorallocate($image, 0,0,0);
	imagestring($image, 5, 1, 1, $_SESSION['captcha'], $text_shadow);
	imagestring($image, 5, 0, 0, $_SESSION['captcha'], $text_colour);
	header ('Content-type: image/png');
	imagepng($image, null, 100);
	imagedestroy($image);	
	exit;
}

?>
If you want audio backup check out http://bokehman.com/captcha_verification
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Thank you for that. It is perfect!

I dont know why I couldnt get the others to work, I kept on getting no picture.

Thanks!
Post Reply