submit comment to site, recapatcha added and errors!

Ok so i run in to a spot of bother,

Up on user submitting comments to my site i had no issues i dident need to use anything like recapatcha, but now spammers alert!

The script i am using works perfectly when a user submits, then i added recapatcha to it, which i added with no problems atall, until i run the script testing it on the site. I dont see no errors in the recapatcha script so it tells me there is something wrong with my code. I did move the recapatch script around and made the recapatcha finish before it runs my code just to see if i could figure it out, when i put it before my code it all went through but of course the code was not secure with re. So when i drop my code inside the recapatcha server side code i pull out a

Server error
The website encountered an error while retrieving http://www.chulse.co.uk/output/addguestbook.php. It may be down for maintenance or configured incorrectly.

Could someone please take a look see if they cans spot the problem? thanks!

[php]

<?php require_once('../CP/Scripts/recaptchalib.php'); $privatekey = "6LeOVNMSAAAAAAXyYr8QlNkVs6zb-xC7JM-RhmiH"; $resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]); if (!$resp->is_valid) { // What happens when the CAPTCHA was entered incorrectly die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." . "(reCAPTCHA said: " . $resp->error . ")"); } else { // Your code here to handle a successful verification $dbservertype='mysql'; $servername='name'; // username and password to log onto db server $dbusername='dbname'; $dbpassword='dbpass'; // name of database $dbname='dbname'; //////////////////////////////////////// ////// DONOT EDIT BELOW ///////// /////////////////////////////////////// connecttodb($servername,$dbname,$dbusername,$dbpassword); function connecttodb($servername,$dbname,$dbuser,$dbpassword){ global $link; $link=mysql_connect ("$servername","$dbuser","$dbpassword"); if(!$link){die("Could not connect to MySQL");} mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error()); } $email=$_POST['email']; $name=$_POST['name']; $country=$_POST['country']; $dtl=$_POST['dtl']; $status = "OK"; // setting the flag for form validation $msg=""; // error message string is blank // now let us check email address if (!stristr($email,"@") OR !stristr($email,".")) { $msg="Your email address is not correct
"; $status="NOT OK"; } // Now let us check if name is entered or not if(strlen($name) < 2 ){ // if name is less than two char length $msg .="Please enter your name
"; $status="NOT OK"; } if($status<>"OK"){ // if form validation is not passed echo "

"; echo $msg. "



"; }else{ $tm=time(); // reading the time of entry // adding data to mysql database //first thing is to escape single quotes! $dtl = str_replace("'","\'",$dtl); //now process $rt=mysql_query("insert into guest_book(name,email,country,tm,dtl) values('$name','$email','$country','$tm','$dtl')"); echo mysql_error(); echo "Thank you for your comment! You will automaticly be redirected..."; //send e mail to self with the users e mail address for a thank you message! ?> <? $to = "[email protected]"; $subject = "Comment submission"; $message = $name . "\n\n" . $dtl . "\n\n" . $email; $from = $email; $headers = "From:" . $from; mail($to,$subject,$message,$headers); //now lets send an e mail to the user and thank them! $to = $email; $subject = "We want to thank you!"; $message = "You recently submited a review on www.chulse.co.uk. We want to thank you for taking the time to give us your feedback! it is greatly appriciated. If you have any questions at all please contact me using this e mail address or by using the contact form on the website. http://www.chulse.co.uk \n\n Cerris Hulse."; $from = "[email protected]"; $headers = "From:" . $from; mail($to,$subject,$message,$headers); } } header('Refresh: 5; URL=../'); ?>

[/php]

Hi Timms,

Since your script was working fine before, the problem looks like it must be somewhere near the top. Are you certain that you have the correct location for your required file?

Try adding…[php]if(!file_exists(’…/CP/Scripts/recaptchalib.php’)) {
echo ‘Required file not found’;
exit;
}[/php]…before your first line (require_once…)

This will tell us whether it is finding the recaptchalib.php file or not. If it isn’t you will throw a fatal error when you attempt to use the function recaptcha_check_answer(). If you don’t get the “Required file not found” message, then it might be worth looking at the code for recaptchalib.php, as I don’t see anything else in the section that you added that should cause the error you are receiving.

Let us know…

Appriciate you replying, im having a tough time trying to get the answer for this even though i have posted it on so many forums, appoligies for my late replying had doctors to attend to so had a tough day.

As far as it goes for what you said above, i added that line to the very top of my script and attempted to post, as it pulled out the suspect error i belive it is finding the file and did not give me the echo.

You are correct it is at the top somewhere as i have moved some of the code below the capatcha script just so i can see what works and get a rough guess.

As for it being recaptchalib.php i dont think it is this but i could be wrong, this script was actually working a couple of months ago and has not been changed since, on saying that nor has the code i posted. I am really confused on this situation.

Also i forgot to post it says aswell

HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfil the request.

What happens if you remove everything after the opening php tag (<?php) through “else {”. This should eliminate the captcha component temporarily and tell us whether the issue is with that component or the remainder of the script.

Also, are you certain that your form is properly setting the required captcha fields? (recaptcha_challenge_field and recaptcha_response_field) I know this is a long shot, but I just don’t see anything in your code that should be creating your problem. I am not familiar with the recaptcha script though…

It might be worth starting your script like this:[php]<?php
echo ‘

’;
print_r($_POST);
exit;[/php] and carefully look at the $_POST data. Obviously you will want to revert this as soon as you have captured the incoming $_POST values.

Going off what you just said i just completly stripped it of my code inside it like you said and did this,

[php]

<?php require_once('../CP/Scripts/recaptchalib.php'); $privatekey = "key"; $resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]); if (!$resp->is_valid) { // What happens when the CAPTCHA was entered incorrectly die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." . "(reCAPTCHA said: " . $resp->error . ")"); } else { echo "works"; } header('Refresh: 5; URL=../'); [/php] It actually echoed "works" so maybe i should strip it and put in a line then test and keep doing that tell i get the error?

Ok we are on our way here i think!

I just did what you said and my result is as follows,

[php]Array
(
[name] => Tim
[email] => [email protected]
[country] =>
[dtl] => Hawks, doduchu
[recaptcha_challenge_field] => 03AHJ_Vuu6N8I_d1g4yvG6ITVrMotCVSZfjXAD7517T5K_3JE2-RPmB6NswaRKeXWd0XKp1HW_3unPEwqdcRmOgFmroTgp3zAgzRay52OpYxxdQ8s57dngYzuLcH6c3ZSA61NbKgFRu57QPN9Z02ztkQNwRw0OVk5a6A
[recaptcha_response_field] =>
)[/php]

Now i do belive that is incorrect.

dtl for a start shouldent be Hawks, doduchu that is what i typed in for capatcha, so it is filling it in there when it should be on the next line down!

Awesome, we are making progress. This is definitely a problem with the form code. Take a look at it and see if you can correct it. Feel free to post the code for you form if you are having difficulty identifying the problem (sometimes it helps to have fresh eyes!).

Fingers crossed…

Right well this is strange, dtl which is the id of the field for the comment… never the less i can fill it in and press it and sometimes it goes in the right place and sometimes it dosent this is what i can make out

here is what i got on my last try

[php]Array
(
[name] => Tim
[email] => [email protected]
[country] =>
[dtl] => test
[recaptcha_challenge_field] => 03AHJ_VutX7dgEytukdDoOwHVpjlsQ4b1QPdsHsI-PjhEWgBbdHdgnEIEPXW-zZiEvqTpB9LcO3JnxV7ervUb17h6qELsJ86aHgO5WoiiQAo5Z7pItFlzlqS_vAgl5Dncd7uyyg5hdMSrm-IygUOGL41xtePQ7fbw-uCE23WVCW2i_wvUYYQuvhZg
[recaptcha_response_field] => cnaddia neceffary
)[/php]

I did exactly the same thing as what i did in my previous post, so maybe this is something to do with my form that i am subbmitting.

here it is…

<form method="post" action="output/addguestbook.php">
<table border='0'  cellspacing='0' cellpadding='0' align=center width=300>
<tr><td><font face='Verdana' size='2'>Your Name *</font><td><input type='text' name='name'></td></tr>
<tr><td><font face='Verdana' size='2'>Email *</font><td><input type='text' name='email'></td></tr>
<tr><td><font face='Verdana' size='2'>Country</font><td><input type='tex't name='country'></td></tr>
<tr bgcolor=#'f1f1f1' ><td colspan=2 align='center'><textarea name='dtl' rows='5' cols='40'></textarea></td></tr>
<tr  ><td colspan=2 align=center>
 <?php
          require_once('CP/Scripts/recaptchalib.php');
          $publickey = "mypublickey"; // you got this from the signup page
          echo recaptcha_get_html($publickey);
        ?>
  <br />
<input type='submit' value='Add Comment'></td></tr>

</table></form>

but to me this looks right so now im real baffeled

it seems to me but i could be wrong, the response field and the challenge field are coming out the wrong way around atleast thats how it seems to me but im still a novice to all this stuff in learning still

I am just heading home, so I will have a look at it in about an hour or two. Real quickly though, I noticed that your html for your country input has a misplaced single-quote

Instead of<tr><td><font face='Verdana' size='2'>Country</font><td><input type='tex't name='country'></td></tr>

It should be<tr><td><font face='Verdana' size='2'>Country</font><td><input type='text' name='country'></td></tr>

I changed that made no difference to the error, had a quick run through see if i could spot anything and couldent. I think it may be an idea to just re do this script from scratch, i think i am defeated.

Timms,

I am looking at your form now. I have found several other issues with it. I am going to redo it and I’ll post shortly. Don’t give up yet!!!

OK, I rewrote the form and eliminated the table layout. I attempted to duplicate the previous look using css. Note that you will probably need to make some additional adjustments to get the look you want - I would be happy to help you with that.

The version below is now fully HTML5 validated. The previous version had an enormous amount of depreciated attributes or elements, incorrectly formatted attributes, and unclosed or open HTML tags. This version should be clean of all of those things. In my testing, I am reliably passing the values from it. The only thing I am unsure about it what will be generated by the php, as I have never worked with recaptchalib.php and don’t know what it will return.

Good Luck!

[php]

Add to Guestbook div#formContainer{ width: 300px; margin: auto; font-family: verdana; font-size: 10pt; } div.labels{ width: 100px; float: left; clear: left; text-align: left; padding: 0 5px 0 0; } div.userInput{ width: 190px; float: left; clear: right; text-align: left; } div#dtl{ float: left; clear: right; width: 100%; height: 100px; } textarea#tdtl{ background: none repeat scroll 0 0 #F1F1F1; } div#subButton{ float: right; }
Your Name *
Email *
Country
<?php require_once('CP/Scripts/recaptchalib.php'); $publickey = "mypublickey"; // you got this from the signup page echo recaptcha_get_html($publickey); ?>
[/php]

Apprciate what you have done, dident work still getting this error. This script was one i downloaded and i think it is a bit old and probably not working so well.

I just adjusted some of my code to how i needed it to be… i just find it odd how it was working with capatcha and all of a sudden when i re add capatcha to the form it wont work. I will keep having a look around on my site today maybe its something else on the page as the form you see is actually an include, although i dont think it would be something on the page but hey who knows. Failing this, i am going to re write the whole guest book i dont think it is really worth all this effort on old code and which seems to be in my eyes probably poor code, can probably be written much cleaner and neater.

Really do appriciate you attempts though thanks so much!

I think a rewrite is probably the best idea.

I would suggest creating a test form and handler before going forward. I would make sure the form has all the same fields that you are going to be using. It should submit to a test script that confirms that the recaptcha is working properly and that the post variables were received as intended. This will tell you that you have a good working form and are implementing the recaptcha correctly. In addition, it will let you rule out (for the most part), the recaptcha as the source of problems in the final version.

Should you wish to continue to work with what you have, I am more than happy to help you try to solve it.

Sponsor our Newsletter | Privacy Policy | Terms of Service