Jump to content

Help please. For project.


pnssassin

Recommended Posts

Hello,

 

I used code to make a very basic forum for a project but now people are posting random crap so I need to add recaptcha. The forum worked fine before but now I cant get recaptcha to work and i just get the entered incorrect error.

 

create_topic.php:

 

 
<body bgcolor="yellow">
<table cellspacing="1" cellpadding="0" width="400" align="center" bgcolor="#cccccc" border="1">
<tbody>

<tr>


<form id="form1" name="form1" method="post" action="add_topic.php">

   <?php
     require_once('recaptchalib.php');
     $publickey = ""; // you got this from the signup page
     echo recaptcha_get_html($publickey);
   ?>
  


<td>
<table cellspacing="1" cellpadding="3" width="100%" bgcolor="#ffffff" border="1">
<tbody>
<tr>
<td bgcolor="#e6e6e6" colspan="3"><strong>
<center>Create New Topic</center></strong></td></tr>
<tr>
<td width="14%"><strong>Topic</strong></td>
<td width="2%">:</td>
<td width="84%"><input id="topic" size="50" name="topic" /></td></tr>
<tr>
<td valign="top"><strong>Detail</strong></td>
<td valign="top">:</td>
<td><textarea id="detail" name="detail" rows="3" cols="50"></textarea></td></tr>
<tr>
<td><strong>Name</strong><br />
(enter "Anonymous" if you wish)</td>
<td>:</td>
<td><input id="name" size="50" name="name" /></td></tr>
<tr>
<td><strong>Email</strong> (unnecessary)</td>
<td>:</td>
<td><input id="email" size="50" name="email" /></td></tr>
<tr>
<td> </td>
<td> </td>


<td><input type="submit" value="Submit" name="Submit" /> <input type="reset" value="Reset" name="Submit2" /></td></tr></tbody></table></td></form></tr></tbody></table><br /><hr />
<font color="green"><center><a href="http://www.skoolforum.com/main_forum.php" >Back to Forum</a> <a href="http://www.skoolforum.com" >Back to Main Page</a></center></font>
</body>

 

add_topic.php

 <body bgcolor="ffff00">
<?php
$host="";
$username="";
$password="";
$db_name="";
$tbl_name="";



mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$topic=$_POST['topic'];
$detail=$_POST['detail'];
$name=$_POST['name'];
$email=$_POST['email'];

$datetime=date("d/m/y h:i:s");

require_once('recaptchalib.php');
$privatekey = "";
$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 . ")");
} 

$sql="INSERT INTO $tbl_name(topic, detail, name, email, datetime)VALUES('$topic', '$detail', '$name', '$email', '$datetime')";
$result=mysql_query($sql);
echo "<CENTER><H1><STRONG>Successful post!</STRONG></H1></CENTER><BR><HR />"; 
echo "<CENTER><FONT COLOR='GREEN'><a href='http://www.skoolforum.com/main_forum.php' >View your topic</a> <a href='http://www.skoolforum.com' >Back to Main Page</a></FONT></CENTER>";



mysql_close(); 
?>
</body> 

 

Please tell me what I am doing wrong? the forum works before i added the recaptcha and i cant just revert it i need to have the recaptcha on there. Help me please!

 

Link to comment
https://forums.phpfreaks.com/topic/154415-help-please-for-project/
Share on other sites

I can't see anything obvious wrong with what you've done. What happens now when you try to post? Do you get any errors? Does the script stop?

 

Please also look at mysql_real_escape_string() (http://uk2.php.net/mysql_real_escape_string) for escaping the data before you insert it into your database.

 

EDIT: Apologies, I've just seen that you did mention that the script replies with invalid response. Are your public and private keys definitely correct? I wouldn't be posting them on a public form either. Is probably wise to delete them from your original post.

 

EDIT2: I've just checked http://wiki.recaptcha.net/index.php/FAQ#It.27s_not_working.21_Help.21 and it appears that the key is domain specific. Are you sure that you're sending the correct domain? echoing $_SERVER["REMOTE_ADDR"] should confirm.

I would do something like this:

 

http://beta.phpsnips.com/snippet.php?id=71

 

The above is an ascii captcha, so you probably want everything up to line 58 (without the HTML).

 

then after line 58, I would add this code:

 

$_SESSION['CAPTCHA'] = $string;
imagejpeg($image, NULL, 100);

In that case, maybe try some debugging in add_topic.php by var_dump()'ing the values ot  $_POST["recaptcha_challenge_field"] and $_POST["recaptcha_response_field"] to confirm they are being set to what you have just typed.

 

How do i do that? im very new to php.

php's website will tell you all about var_dump. All it does really is echo values to the screen.

 

Before you call the recaptcha function just add:

 

<?php
var_dump($_POST["recaptcha_challenge_field"]);
var_dump($_POST["recaptcha_response_field"]);

 

This should show what you have just typed. If it doesn't then something is most likely wrong with the HTML form.

in advance thanks for the help :) .

 

I tried the var dump thing and i got a new error that says:

 

NULL NULL The reCAPTCHA wasn't entered correctly. Go back and try it again.(reCAPTCHA said: incorrect-captcha-sol)

 

so that means for some reason or another the variables arent going in... but why would the challenge field be blank?

Can you post a view source of your page starting at the first <form> tag to the last form tag please? Obviously remove anything identifying if you need to.

 

Those besides the home page and view forum topics page those are the only files I have. The form that is being submitted is the first code i posted and the second one is the code add the entered information to the sql.

I would agree that your code looks right.  Are you 100% sure you have the right public key and private key in the scripts, and those keys match the domain you signed up for at recaptcha?  I tried out your page, and verified that it is rejecting at the recaptcha.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.