the idea i cant find anything about it via google. so hence this post.
i want to have atleast 5 questions to ask that will be (key word: randomly) displayed when someone wants to reply. meaning the person has to answer correctly. i DO NOT WANT WHATS 5+5 KIND IF THING i want questions i create based on my site. question is to all of you:
HOW DO I DO IT? i know there has to be some kind of answer and question related thing to check it correctly but i havent a clue how to do it any ideas? SOLUTION? WHAT NOT? plz help me.

Recommended Answers

All 6 Replies

Captchas are annoying. You can easily just use form timeouts and tokens. They actually work better.

thank you but i still want to make this "captcha like" thing

Ok then. I have some old captcha code I found a while back. I modified it to work the way you want.

<?php

session_start();

class captcha {

	private $width = null;
	private $height = null;
	private $text = null;
	private $font = null;

	public function setWidth( $width ) {
		$this->width = $width;
	}

	public function setHeight( $height ) {
		$this->height = $height;
	}

	public function setText( $text ) {
		$this->text = $text;
	}

	public function setFont( $file ) {
		$this->font = $file;
	}

	function output() {
		foreach( array('width','height','text','font') as $var ) {
			if ( is_null( $this->$var ) ) {
				die("{$var} is null, please define a {$var}");
			}
		}
		$font_size = $this->height * 0.75;
		$image = @imagecreate($this->width, $this->height) or die('Cannot initialize new GD image stream');
		$background_color = imagecolorallocate($image, 255, 255, 204);
		$text_color = imagecolorallocate($image, 204, 0, 0);
		$noise_color = imagecolorallocate($image, 255, 102, 102);
		for( $i=0; $i<($this->width*$this->height)/3; $i++ ) {
			imagefilledellipse($image, mt_rand(0,$this->width), mt_rand(0,$this->height), 1, 1, $noise_color);
		}
		for( $i=0; $i<($this->width*$this->height)/150; $i++ ) {
			imageline($image, mt_rand(0,$this->width), mt_rand(0,$this->height), mt_rand(0,$this->width), mt_rand(0,$this->height), $noise_color);
		}
		$textbox = imagettfbbox($font_size, 0, $this->font, $this->text) or die('Error in imagettfbbox function');
		$x = ($this->width - $textbox[4])/2;
		$y = ($this->height - $textbox[5])/2;
		imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $this->text) or die('Error in imagettftext function');
		header('Content-Type: image/jpeg');
		imagejpeg($image);
		imagedestroy($image);
	}

}

$sentences = array(
	array(
		'text' => 'The color of the site background is?',
		'width' => 550,
		'answer' => 'grey'
	),
	array(
		'text' => 'Something like above?',
		'width' => 75,
		'answer' => '???'
	),
	array(
		'text' => 'AWeasdkfjasdfaiwefjasdfkjasdlfasdf?',
		'width' => 85,
		'answer' => '????'
	)
);

$i = mt_rand(0,( count( $sentences ) - 1 ));

$sentence =& $sentences[$i];

$captcha = new captcha;
$captcha->setHeight( 35 );
$captcha->setWidth( $sentence['width'] );
$captcha->setFont('monofont.ttf');
$captcha->setText( $sentence['text'] );

$_SESSION['sentence_answer'] = $sentence['answer'];

$captcha->output();

?>

You need to change the sentences and then change the width to match the length of the sentence. That will be trial and error.

ok that code is huge! thanks. but how do i do it without images only text.

Just echo the sentence to the page with a textbox. Then store the answer into a session.

I thought you were wanting a captcha like image with a sentence.

no image. store itin a session? how? i dont understand. there also has to be ATLEAST 5 QUESTIONS and it all has to RANDOMIZE.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.