Jump to content

[SOLVED] Basic Image Captcha


MasterACE14

Recommended Posts

hello,

 

I made a basic image captcha, and the image generates fine, but I am using a Session variable to hold the 3 digits in the image, which are then checked in my login.php page against a text box for the 3 digits. And if I put in the wrong digits, it displays my error, so that works fine. But if I put in the correct digits, it still displays my error... and I cant figure out why.

 

here's my image_verification.php file:

<?php
//error_reporting(E_ALL);

// variables
$number_1 = rand(1,9);
$number_2 = rand(1,9);
$number_3 = rand(1,9);

$code = $number_1 . $number_2 . $number_3;

$_SESSION['SECURITY_CODE'] = $code;

$image = "http://www.crikeygames.com.au/conflictingforces/images/image_captcha.PNG";  
$im = imagecreatefrompng($image); 
$wc = ImageColorAllocate ($im, 255, 255, 255);  
ImageString($im, 5, 12, 2, $code, $wc);  
header("Content-Type: image/png");  
Imagepng($im);  
ImageDestroy ($im); 

?>

 

and here's login.php(just whats necessary)

Note: $_POST['imagecaptcha'] is the text input on the homepage where you insert the 3 digits:

<?php

// Check to see if security codes match.
if ($_POST['imagecaptcha'] !== $_SESSION['SECURITY_CODE'])
{

	echo "<center>The Security Code was incorrect!</center><br>";
	echo "<center><input type=button value=\"Back\" onClick=\"history.go(-1)\"></center>";
	die();

} else {

// check username against database etc etc...

?>

 

Regards ACE

Link to comment
Share on other sites

Consider this:

$x = 6;
$y = 7;
$z = 8;
$a = $x.$y.$z;
print ":".$a.":<br>";
if($a !== 678)
{
print "one<br>";
}
else
{
print "two<br>";
}
// AND
if($a !== '678')
{
print "one<br>";
}
else
{
print "two<br>";
}

 

Be sure of what variables your using, if they are both strings i'd advise using:

if(strcmp($a, $b)!=0)

Link to comment
Share on other sites

yes your issue is your using a !== which only is false (meaning its true) if they are the exact same variable type and value  i.e

 

<?php
$var1 = "1";
$var2 = 1;

if($var1  !== $var2){
echo "This is true.<br />";
}
if($var1 != $var2){
echo "This is false.<br />";
}i
if($var1  == $var2){
echo "this is true.<Br />";
}
if($var1 === $var2){
echo "this is false.<br />";
}
?>

 

See what I am saying

 

$_POST is all string variables (unless they are files)

Link to comment
Share on other sites

you do have session_start(); on that page? 

 

Also just a rule of thumb if you try and compare a session value that is set after headers are sent it will be on the value pre headers sent

i.e

 

<?php
session_start();
$_SESSION['test'] = "Bob";
#Send some output
echo "The Session is: ";
$_SESSION['test'] = "Joe";
echo $_SESSION['test'];
#Output The Session is: Bob
?>

 

Make sense

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.