Forum Moderators: coopster

Message Too Old, No Replies

Math captcha with session not working correctly in PHP 4.3.11

         

wsamoht

5:16 am on Jul 9, 2009 (gmt 0)

10+ Year Member



I am having a problem with a math captcha that I created. It is working fine in PHP 5.2.6 on my local machine however the server that it will be on is using 4.3.11.

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?

penders

11:45 am on Jul 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What do your variables contain? ie:
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 ?)

wsamoht

2:17 pm on Jul 9, 2009 (gmt 0)

10+ Year Member



I echoed out the
$captcha
and
$_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.

wsamoht

7:02 am on Jul 10, 2009 (gmt 0)

10+ Year Member



I never figured it out, but I changed the session to a cookie and it works fine. Thanks for your help!

penders

10:21 am on Aug 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sorry, a bit late in the day, but anyway...

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;

wsamoht

12:42 pm on Aug 1, 2009 (gmt 0)

10+ Year Member



Thanks for the pointers. I will definitely change some of my code.