Forum Moderators: coopster
The following line is not working. It is not catching when the answer is filled in but incorrect. If I set $captcha != a specific number it works but when I check it against the session variable it does not.
if(!empty($captcha) && $captcha != $_SESSION['captcha']['answer']) $errors[] = 'The answer to the math question was wrong. Please try again.';
Here is all the code:
<?php
session_start();
//Captcha
function captcha() {
if(isset($_SESSION['captcha'])) {
unset($_SESSION['captcha']);
}
$_SESSION['captcha'] = Array();
$num1 = rand(0,9);
$num2 = rand(0,9);
$answer = $num1 + $num2;
$_SESSION['captcha']['num1'] = $num1;
$_SESSION['captcha']['num2'] = $num2;
$_SESSION['captcha']['answer'] = $answer;
}
if(!$_POST['submit']) {
captcha();
}
$Name = $_POST['Name'];
$Address = $_POST['Address'];
$City = $_POST['City'];
$State = $_POST['State'];
$Zip = $_POST['Zip_Code'];
$Parish = $_POST['Parish'];
$Email = $_POST['Email'];
$Phone_Number = $_POST['Phone_Number'];
$Start = $_POST['Start'];
$Parish1 = $_POST['Parish1'];
$Scholarship = $_POST['Scholarship'];
$Comments = $_POST['Comments'];
$captcha = $_POST['captchaAns'];
$errors = Array();
if($_POST['submit']) {
if(empty($Name)) $errors[] = 'Please fill in your name.';
if(empty($Email)) $errors[] = 'Please fill in your email.';
if(empty($Phone_Number)) $errors[] = 'Please fill in your phone number.';
if(empty($captcha)) $errors[] = 'Please answer the math question.';
if(!empty($captcha) && $captcha != $_SESSION['captcha']['answer']) $errors[] = 'The answer to the math question was wrong. Please try again.';
captcha();
if(empty($errors)) {
mail(blahblahblah);
unset($_SESSION['captcha']);
header( "Location: blah blah" );
}
}
echo $_SESSION['captcha']['answer'] . '<br />';
print_r($_SESSION['captcha']);
?>
I printed out the session array and that is working fine. Any suggestions?
echo '"'.$captcha.'" == "'.$_SESSION['captcha']['answer'].'"';
Bear in mind also that $captcha is a string and $_SESSION['captcha']['answer'] is an integer, so it's going to have to do some type conversion along the way. Does this mess with the comparison? (Although works with PHP5 and not PHP4 ?)
$captchaand
$_SESSION['captcha']['answer']and in php 5 I got what I expected. In php 4 it prints out the session variable on page load. When the form is submitted and the captcha is not filled in nothing prints out. If the captcha is filled in it prints out $captcha for both variables even if the session doesn't equal it. That is very strange. I checked the server in both cases and the session is set with the correct numbers.
I also have
print_r($_SESSION['captcha']);In php 4 it prints out the array fine the first time the page loads. However, when the form submits it prints out the user input of $captcha.
In PHP 4 how would I convert the integer to a string? I have tried settype but that did not work.
In PHP 4 how would I convert the integer to a string? I have tried settype but that did not work.
Not sure whether this would be strictly necessary as PHP should do some type conversion of its own, but it's not always correct. You can use type casting in your comparison:
if(!empty($captcha) && ($captcha != (string)$_SESSION['captcha']['answer'])) Also, depending on the level of error_reporting set on your server(s) (ie. it might work on one server and not another), checking the value of a $_POST[] variable when it is not actually set (ie. when the page first loads) will produce a warning and should be avoided. ie.
if($_POST['submit']) { Should be:
if(isset($_POST['submit'])) {
And statements such as:
$Name = $_POST['Name'];
Should either be contained within your if (isset($_POST['submit']))
statement or be of the form:
$Name = isset($_POST['Name']) ? $_POST['Name'] : NULL;