Jump to content

php captcha within javascript problem


gckmac

Recommended Posts

I am trying to add a captcha from reCaptcha.net (Carnegie Mellon project) to an error submission form. 

 

I have succeeded in getting the captcha input form to show.  However, I cannot get the captcha process code to work.  Can someone point out WHERE on this form it would go?  (You will see the captcha input code, which is fine).

 

<?php

require_once("../scripts/dbinfo.php");

$tplpath = "../scripts/tpl";

 

$link = mysql_connect($server, $userid, $password);

mysql_select_db($db, $link);

 

if (isset($_GET['id'])) {

$id = $_GET['id'];

$type = $_GET['type'];

 

if ($type == "event") {

$checkquery = "select title from maine_festivals where id = '$id'";

$title = "Report Update";

} else {

$checkquery = "select name from maine_hotels where id = '$id'";

$title = "Report Error";

}

 

$checkresult = mysql_query($checkquery, $link);

 

$row = mysql_fetch_array($checkresult);

$propertyname = $row[0];

 

$from_url = $_SERVER['HTTP_REFERER'];

 

}

 

if (isset($_POST['signup']) && $_POST['signup'] == "Submit") {

$msg = quote_smart($_POST['msg']);

$email = quote_smart($_POST['email']);

$from_url = $_POST['from_url'];

$propertyname = $_POST['propertyname'];

 

$subject = "Error Report about $propertyname";

$body = $msg;

 

if ($email != "") {

$body .= "\n Reported by $email";

}

 

$body .= "\nURL: $from_url";

 

$headers = 'From: [email protected]' . "\r\n";

 

mail("[email protected]", $subject, $body, $headers);

 

header("Location: $from_url");

}

 

function tpl_head() {

global $tplpath;

global $title;

 

echo <<<tpl

 

<title>Maine.info - $title</title>

<!-- PUT THIS TAG IN THE head SECTION -->

<script type="text/javascript" src="http://partner.googleadservices.com/gampad/google_service.js">

</script>

<script type="text/javascript">

  GS_googleAddAdSenseService("ca-pub-xxx");

  GS_googleEnableAllServices();

</script>

<script type="text/javascript">

  GA_googleAddSlot("ca-pub-xxx", "Maine_General_LHS_120x60");

  GA_googleAddSlot("ca-pub-xxx", "Maine_General_LHS_120x600_1st");

  GA_googleAddSlot("ca-pub-xxx", "Maine_General_LHS_120x600_2nd");

  GA_googleAddSlot("ca-pub-xxx", "Maine_General_LHS_120x90");

  GA_googleAddSlot("ca-pub-xxx", "Maine_General_RHS_300x250_1st");

  GA_googleAddSlot("ca-pub-xxx", "Maine_General_RHS_300x250_2nd");

  GA_googleAddSlot("ca-pub-xxx", "Maine_General_TOP_468x60");

</script>

<script type="text/javascript">

  GA_googleFetchAds();

</script>

<!-- END OF TAG FOR head SECTION -->

 

 

<link rel="stylesheet" type="text/css" media="screen" href="$tplpath/cmxforms/cmxform.css" />

<link rel="stylesheet" type="text/css" media="screen" href="$tplpath/cmxforms/cmxformTemplate.css" />

<script src="$tplpath/jquery/jquery.pack.js" type="text/javascript"></script>

<script src="$tplpath/cmxforms/cmxforms.js" type="text/javascript"></script>

<script src="$tplpath/jquery/jquery.validate.pack.js" type="text/javascript"></script>

<script src="$tplpath/jquery/jquery.metadata.js" type="text/javascript"></script>

 

 

 

 

<script type="text/javascript">

 

 

 

/*

$().ready(function() {

// validate the comment form when it is submitted

 

$("#signupform").validate({

rules: {

email: {

required: true,

email: true

}

},

messages: {

email: "Please enter a valid email address"

}

});

 

});

*/

</script>

 

<style type="text/css">

 

#signupform { width: 90%; margin: 25px; }

#signupform label {

width: 100%;

/* text-align: right; */

display:block;

}

 

/*#signupform input.submit { margin-left: 183px; }*/

#signupform label.error {

margin-left: 155px;

width: auto;

text-align: left;

/* display: inline;*/

}

body {

min-width: 1000px;

}

 

</style>

 

tpl;

 

}

include($tplpath . "/header.inc");

 

?>

 

<div id="LHSAd">

 

<!-- PUT THIS TAG IN DESIRED LOCATION OF SLOT Maine_General_LHS_120x60

    -->

<script type="text/javascript">

  GA_googleFillSlot("Maine_General_LHS_120x60");

</script>

<!-- END OF TAG FOR SLOT Maine_General_LHS_120x60

    -->

 

<!-- PUT THIS TAG IN DESIRED LOCATION OF SLOT Maine_General_LHS_120x90

    -->

<script type="text/javascript">

  GA_googleFillSlot("Maine_General_LHS_120x90");

</script>

<!-- END OF TAG FOR SLOT Maine_General_LHS_120x90

    -->

 

<!-- PUT THIS TAG IN DESIRED LOCATION OF SLOT Maine_General_LHS_120x600_1st

    -->

<script type="text/javascript">

  GA_googleFillSlot("Maine_General_LHS_120x600_1st");

</script>

<!-- END OF TAG FOR SLOT Maine_General_LHS_120x600_1st

    -->

 

<!-- PUT THIS TAG IN DESIRED LOCATION OF SLOT Maine_General_LHS_120x600_2nd

    -->

<script type="text/javascript">

  GA_googleFillSlot("Maine_General_LHS_120x600_2nd");

</script>

<!-- END OF TAG FOR SLOT Maine_General_LHS_120x600_2nd

    -->

 

</div>

<div id="map">

 

<!-- PUT THIS TAG IN DESIRED LOCATION OF SLOT Maine_General_TOP_468x60

    -->

<script type="text/javascript">

  GA_googleFillSlot("Maine_General_TOP_468x60");

</script>

<!-- END OF TAG FOR SLOT Maine_General_TOP_468x60

    -->

 

 

<h1 align="center"><?=$title?></h1>

 

<?php

 

if ($type == "event") {

$msg = "Thank you for updating our listing for <strong>$propertyname</strong>. We appreciate your time. Other folks will too.";

} else {

$msg = "Thank you for reporting an error in our listing for <strong>$propertyname</strong>. We appreciate your time. Other folks will too.";

}

 

?>

 

<p><?=$msg?></p>

<form class="cmxform" id="signupform" method="post" action="reporterror.php">

<fieldset>

<legend></legend>

 

<p>

<label for="msg">Describe Problem</label>

<textarea id="msg" name="msg" cols="40" rows="10"></textarea>

</p>

<p>

<label for="email">Email (Optional) </label>

<input type="text" name="email" id="email" type="text" class=""  />

 

<?php

require_once("../captcha/recaptchalib.php");

$publickey = "6Le_LAYAAAAAAOuWVFAZzv-zRO_jN7km48LHHYEq"; // you got this from the signup page

echo recaptcha_get_html($publickey);

?>

 

 

 

 

<input type="submit" value="Submit" class="submit" id="signup" name="signup" />

</p>

</fieldset>

<input type = "hidden" id = "from_url" name = "from_url" value = "<?=$from_url ?>" />

<input type = "hidden" id = "propertyname" name = "propertyname" value = "<?=$propertyname ?>" />

 

 

 

 

 

 

 

</form>

 

<?php

 

?>

 

 

 

</div>

 

<div id="sidebar">

 

<div id="ads">

<!-- PUT THIS TAG IN DESIRED LOCATION OF SLOT Maine_General_RHS_300x250_1st

    -->

<script type="text/javascript">

  GA_googleFillSlot("Maine_General_RHS_300x250_1st");

</script>

<!-- END OF TAG FOR SLOT Maine_General_RHS_300x250_1st

    -->

 

<!-- PUT THIS TAG IN DESIRED LOCATION OF SLOT Maine_General_RHS_300x250_2nd

    -->

<script type="text/javascript">

  GA_googleFillSlot("Maine_General_RHS_300x250_2nd");

</script>

<!-- END OF TAG FOR SLOT Maine_General_RHS_300x250_2nd

    -->

</div>

 

</div>

<?php

 

include($tplpath . "/footer.inc");

?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/155971-php-captcha-within-javascript-problem/
Share on other sites

You are missing the recaptcha_check_answer() function call which returns you a response object on which you can check if the response was valid as part of your form validation.

 

<?php
$response = recaptcha_check_answer('your-secret-key', $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);
if ($response->is_valid) {
    // valid captcha response
}
?>

 

The full explanation can be found on http://recaptcha.net/plugins/php/

Yes, I've added the process code (didn't show it before).  I made progress rearranging the code.  The remaining issue is that when wrong captcha is entered, the form forgets the event ID that went with it ... see code below ...

<?php

 

if ($type == "event") {

$msg = "Thank you for updating our listing for <strong>$propertyname</strong>. We appreciate your time. Other folks will too.";

} else {

$msg = "Thank you for reporting an error in our listing for <strong>$propertyname</strong>. We appreciate your time. Other folks will too.";

}

 

?>

 

<p><?=$msg?></p>

<form class="cmxform" id="signupform" method="post" action="reporterror.php">

<fieldset>

<legend></legend>

 

<p>

<label for="msg">Describe Problem</label>

<textarea id="msg" name="msg" cols="40" rows="10"></textarea>

</p>

<p>

<label for="email">Email (Optional) </label>

<input type="text" name="email" id="email" type="text" class=""  />

 

<?php

require_once("../captcha/recaptchalib.php");

$publickey = "6Le_LAYAAAAAAOuWVFAZzv-zRO_jN7km48LHHYEq"; // you got this from the signup page

echo recaptcha_get_html($publickey);

?>

 

 

 

 

<input type="submit" value="Submit" class="submit" id="signup" name="signup" />

</p>

</fieldset>

<input type = "hidden" id = "from_url" name = "from_url" value = "<?=$from_url ?>" />

<input type = "hidden" id = "propertyname" name = "propertyname" value = "<?=$propertyname ?>" />

 

</form>

 

 

 

<?php

 

 

 

 

if (isset($_POST['signup']) && $_POST['signup'] == "Submit") {

 

require_once("../captcha/recaptchalib.php");

$privatekey = "xxxxxx";

$resp = recaptcha_check_answer ($privatekey,

                                $_SERVER["REMOTE_ADDR"],

                                $_POST["recaptcha_challenge_field"],

                                $_POST["recaptcha_response_field"]);

 

if (!$resp->is_valid) {

  die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .

      "(reCAPTCHA said: " . $resp->error . ")");

}

 

?>

 

<?php

$msg = quote_smart($_POST['msg']);

$email = quote_smart($_POST['email']);

$from_url = $_POST['from_url'];

$propertyname = $_POST['propertyname'];

 

$subject = "Error Report about $propertyname";

$body = $msg;

 

if ($email != "") {

$body .= "\n Reported by $email";

}

 

$body .= "\nURL: $from_url";

 

$headers = 'From: [email protected]' . "\r\n";

 

mail("[email protected]", $subject, $body, $headers);

 

header("Location: $from_url");

}

?>

 

 

 

 

So the problem is basically a failure to properly loop ...

 

1. If captcha is correct, I come back to the captcha form and dont' tell anyone AOK (though it does submit with correct database id).

 

2. If captcha is incorrect, I come back to the captcha form and lose the database event ID.  So if user submits again, the event it applies to is unknown.

 

I am learning php ... but too slowly. 

 

thx,

gckmac

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.