Jump to content

Filled form is passing empty string on server (but works OK on localhost)


Recommended Posts

I am trying to implement a simple CAPTCHA on my web site.

 

It works as I intend on localhost (Apache server on my laptop), but $_POST['human'] appears to reach test.php as an empty string (MD5=d41d8cd98f00b204e9800998ecf8427e) when I upload the same files to my web server.

 

The CAPTCHA image is of a random number; the number is stored as an MD5 string for comparison as $_SESSION['randomnum']. This bit seems to work properly, and test.php picks up this number (and, hence, MD5 string) correctly.

 

 

The form HTML:

<form action="test.php" method="post">
<img src="captcha02.php" align="absmiddle" />
<input type="text" size="20" name="human" />  
<input type="submit" name="done" value="I'm human!" />
<br /><br />
</form>

 

test.php:

<?php
session_start();

if (md5($_POST['human']) == $_SESSION['randomnum'])	{ echo 
	'<script language="javascript"><!--
location.replace("astunit_course_booking_form.php")
//-->
</script>';


}	

else { echo '<script language="javascript"><!--
location.replace("astunit_course_human_test.php")
//-->
</script>';
}

?>

 

I have used this to try to see what is happening:

echo $_POST['human'];
echo "<br />";
echo md5($_POST['human']);
echo "<br /><br />";
echo $_SESSION['randomnr'];
echo "<br />";
   echo $_SESSION['randomnum'];

 

randomnr is the number generated for the image:

$randomnr = rand(10000, 99999);
$_SESSION['randomnum'] = md5($randomnr);

 

Confirms that the form passes human correctly when testing on localhost my laptop, but not when it is uploaded to the web server, where it passes an empty string. Sample output on localhost:

54052
67e3aa6a8dce9aaf1ec9563bbac40582

54052
67e3aa6a8dce9aaf1ec9563bbac40582

 

Sample output on web server:


d41d8cd98f00b204e9800998ecf8427e

46850
1cb9a2cdf84ff27e0ff8aa17644b15e3

 

Any ideas? (This has flummoxed me for a day and a half!  :D) I have double-checked that files on the web server are latest versions and are in the correct directories.

 

Bingo! Cracked it at last: initialised $-SESSION variables in test.php

 

<?php
session_start();
$_SESSION['ishuman']= md5($_POST['human']);
$_SESSION['randomnum']= md5($_SESSION['randomnr']);

if ($_SESSION['ishuman'] == $_SESSION['randomnum'])	{ echo 

(etc)

 

Not sure why I need to do that on the web server, but not on localhost, but hey...

 

Now, how do I mark this as [sOLVED]..?

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.