Jump to content

Recommended Posts

I am trying to add a captcha to a registration form that I all ready have made.

 

I keep getting told that the captcha in the image and the characters that I am entering do not match. I am hoping that an extra set of eyes will help me find the problem.

 

This is the registration page

 

 

"<?php include('function/init.php'); ?>
<!doctype html>
<?php include('includes/head.php'); ?>

<body>
<div class="gridContainer clearfix">
<?php include('includes/header.php'); ?>
<div id="register">
 <?php	
	if(!empty($_POST['human_check'])) {
		$filtered_email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);

		if (user_exsits($_POST['username']) === true) {
			$errors[] = "The username <em>" . $_POST['username'] . "</em> is already taken";
		}

		if (preg_match("/\\s/", $_POST['username']) == true) {
			$errors[] = "your username can not contain any spaces";
		}

		if (strlen($_POST['password']) < 6 && !empty($_POST['password'])) {
			$errors[] = "Password must be longer than 6 characters";
		}

		if ($filtered_email === FALSE && !empty($_POST['email'])) {
			$errors[] = "<em>".$_POST['email']."</em> is not a valid email";
		} else {
			if (email_exsits($filtered_email) === true) {
				$errors[] = "The email <strong>'" . $_POST['email'] . "'</strong> is already in use";
			}
		}

		if($_POST['human_check'] != $_SESSION['is_human']){
			$errors[] = "Captcha was wrong please try again!";
		}

		if (empty($_POST) === false && empty($errors) === true) {
				$register_data = array(
					'username' => $_POST['username'],
					'password' => $_POST['password'],
					'name' => $_POST['name'],
					'email' => $_POST['email'],
				);

				register_user($register_data);
				header('Location: register.php?success');
				exit();
		} else if (empty($errors) === false) {
			echo output_errors($errors);
		}
		} else if (isset($_GET['success'])) {
				echo "<div class='reg-success'>You have successfully registered!</div>";
				echo "<br /><br />";
				echo "<form action='index.php' method='post' name='login'>
						<ul>
						<li>
							Username<br />
							<input ID='user' TYPE='text' NAME='user' required autocomplete='off' /></li>
						<li>Password<br />
							<input ID='pass' TYPE='password' NAME='pass' required autocomplete='off' /></li>
						<li><input id='submit-log' type='submit' name='submit-log' value='log in' /></li>
						</ul>
					</form>";
	}
	?>

<h1 class="header">Register</h1>
			<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
				<ul>
				 <li>
					 Name<br />
					 <input ID="user" TYPE="text" NAME="name" required /></li>
				 <li>
					 E-mail<br />
					 <input ID="user" TYPE="text" NAME="email" required /></li>
				 <li>
					 Username<br />
					 <input ID="user" TYPE="text" NAME="username" required /></li>
				 <li>Password<br />
					 <input ID="user" TYPE="password" NAME="password" required /></li>
<li id='captcha_part'>
<span class="explain">Click on captcha to refresh image</span>
						<img id ="captcha" src="captcha.php" onclick="javascript:reloadCaptcha()">
					</li>
					<li>Captcha<br />
						<input id="user" type='text' name='human_check' required placeholder='Enter captcha characters'>
					</li>
				 <li><input id="submit" type="submit" name="register" value="Register" /></li>
				</ul>
			</form>
</div>
<?php include('includes/footer.php'); ?>
</div>
</body>
<script language="javascript" type="text/javascript">
		function reloadCaptcha(){
			document.getElementById('captcha').src = document.getElementById('captcha').src+ '?' +new Date();
		}
	</script>
</html>"

 

 

This is the captcha page

"<?php
///////////Editable Variables///////////
$image_width = 220; //sets image's width
$image_height = 80; //sets image's height

$red = rand(0,255); //random colors for lines
$green = rand(0,255);
$blue = rand(0,255);

$red1 = rand(100, 200); //random colors for lines
$green1 = rand(90, 190);
$blue1 = rand(80, 180);

$min_font_size = 20;
$max_font_size = 25;

///////////May not want to edit below this section///////////
$human = "";
for($x = 0; $x < 2; $x++){
$human .= chr(rand(48,57));
}
for($x = 0; $x < 3; $x++){
$human .= chr(rand(65,90));
}
for($x = 0; $x < 2; $x++){
$human .= chr(rand(97,122));
}

$_SESSION['is_human'] = $human;	

$image = imagecreate($image_width, $image_height); //creates image

$background = imagecolorallocate($image, 255, 255, 255); //sets background color	
$text_color = imagecolorallocate($image, $red, $green, $blue); //sets text color

$angle = rand(0,15);

$fonts = array('appleberry_with_cyrillic.ttf', 'BlackCastleMF.ttf','DennisHillSpeeding.ttf','Diskoteque.ttf','the beautiful ones.ttf'); //sets an array of fonts
$font = rand(0, 4); // generates a random number to used to load a font
$font_path = "css/fonts/";

$human_text = $_SESSION['is_human']; // sets variable captcha_text = to the session called captcha

for($i=0; $i<75; $i++) { // for loop to generate random lines and line colors for captcha background
$x1 = rand(0, 220);
$y1 = rand(0, 220);
$x2 = rand(0, 220);
$y2 = rand(0, 220);

$line_color = imagecolorallocate($image, $red1, $green1, $blue1); //sets line color

imageline($image, $x1, $y1, $x2, $y2, $line_color); //creates a line
}

imagettftext($image, rand($min_font_size, $max_font_size), $angle, 40, 70, $text_color, $font_path.$fonts[$font], $human_text); //creates captcha text
header("Content-type: image/jpeg");
imagejpeg($image); //makes the image
imagedestroy($image); //to save ram

?>"

 

Would appreciate it if someone could help me or point me in the right direction

Link to comment
https://forums.phpfreaks.com/topic/269848-captcha-is-not-working-properly/
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.