Forum Moderators: coopster

Message Too Old, No Replies

How to pass form information through a php file to perl/cgi?

How to pass form information through a php file to perl/cgi?

         

echoniner

6:49 am on Nov 25, 2010 (gmt 0)

10+ Year Member



I'm using a formmail.pl file in my cgi-bin to run a basic email form on my site. I'd like to add a ReCaptcha using the following php methodology:

[code.google.com...]

Previously, in my email form's html, I had the code:


<form id="contact" method="post" enctype="multipart/form-data" action="http://www.mydomain.com/cgi-bin/formmail.pl">


But now, per the link above, I need to make it:


<form id="contact" method="post" enctype="multipart/form-data" action="http://www.mydomain.com/recaptcha/verify.php">


Inside verify.php, as the link above states, I have:


<?php
require_once('http://www.mydomain.com/recaptcha/recaptchalib.php');
$privatekey = "blahblah";
$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
HERE IS MY QUESTION! <-------------------------------------
}
?>


So, if it makes it through the check, to the else, as noted above, how do I then call up the [mydomain.com...] and pass the post information to it?

Thanks!

jackherbert

6:39 pm on Nov 25, 2010 (gmt 0)

10+ Year Member



I'm no expert and there has to be an easier way, but could this work?



?>

<form id="contact" ... action="formmail.pl">
<input type="hidden" id="foo1" value="<?php echo $someData ?>">
...
</form>

<script type="text/javascript">
var frm = document.getElementById("contact");
window.onload = frm.submit();
</script>

<?php

jackherbert

7:39 pm on Nov 25, 2010 (gmt 0)

10+ Year Member



Right, I wasn't very clear. In the hidden input field, the value could be <?php $_POST['name'] ?> then in the next $_POST['title'] and so on.

echoniner

8:03 pm on Nov 25, 2010 (gmt 0)

10+ Year Member



For those that can't read it, as the forum corrupted my intension, the last line of my original post reads:

So, if it makes it through the check, to the else, as noted above, how do I then call up the
http://www.mydomain.com/cgi-bin/formmail.pl
and pass the post information to it?


----------


Jack: thanks for the reply. To confirm: I match the code above, which I place in my verify.php, so that their id is the same as the original form in the html?

So, for instance, in my html form, I have the line:

<input type=text name="email" size="40">


Thus, in the verify.php, I'd have:

<input type="hidden" id="email" value="<?php $_POST['email'] ?>">


?

Matthew1980

8:13 pm on Nov 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello echoniner,

Unless I have misinterpreted your question, it sounds like your wanting to have a redirect there to the pl file, and as the globals are set at this point they should be able to be carried through to the next page, either that or just use sessions to carry the data through.

Again, I may not have fully understood your question there, WRT your last post:-

<input type="hidden" id="email" value="<?php $_POST['email'] ?>">

Would need to be this:-

<input type="hidden" id="email" value="<?php echo $_POST['email']; ?>">

in order for the information to be held in the value part of that input element. Only other thing as I will add is this, if this data is coming from a form, sanitise the user data, never trust a users input, filter it, sanitise it, as this could result in hacking attempts to your site.

Cheers,
MRb

Demaestro

8:55 pm on Nov 25, 2010 (gmt 0)

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



scho,

I think what you want is to POST the content from PHP to the formail.pl script.

You can use curl

$url = 'http://www.mydomain.com/cgi-bin/formmail.pl';
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, {field:value});
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

jackherbert

9:36 pm on Nov 25, 2010 (gmt 0)

10+ Year Member



echoniner: yes, it is exactly what I meant.

curl is probably the easier way I was referring to :)

echoniner

9:58 pm on Nov 25, 2010 (gmt 0)

10+ Year Member



I'm completely unfamiliar w/ curl... do you I need some kind of wrapper code or add-on or something?

Also, how can I easily sanitize the data?

Demaestro

10:21 pm on Nov 25, 2010 (gmt 0)

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



curl is a library you will have import.

You shouldn't need to sanitize it, just pass it along.

curl_setopt($ch, CURLOPT_POSTFIELDS, {'email': $_POST['email'], 'somedata': $someData}

Demaestro

10:27 pm on Nov 25, 2010 (gmt 0)

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



I should have mentioned if both methods live on the same box you may be able to use localhost or 127.0.0.1

$url = 'http://localhost/cgi-bin/formmail.pl';

Or

$url = 'http://127.0.0.1/cgi-bin/formmail.pl';

These will be more efficient as they don't require a DNS lookup or a visit to the outside world, but they may not work depending on how things are set up.

echoniner

7:08 am on Nov 27, 2010 (gmt 0)

10+ Year Member



sorry if I'm slow on the uptake here... but we are pushing the envelope on my knowledge:

Here's the bulk of my form, in my contact.html:


<input type="hidden" name="require" value="realname,email,comments">
<input type="hidden" name="env_report" value="REMOTE_HOST,HTTP_USER_AGENT,REMOTE_ADDR,REMOTE_USER">
<input type=hidden name="redirect" value="contact_thankyou.html">
<input type=hidden name="missing_fields_redirect" value="contact_error.html">
<input name="subject" size="40">
<input type=text name="realname" size="40">
<input type=text name="email" size="40">
<textarea name="comments" cols="60" rows="10" wrap="soft">


So, first question:

How do import the curl library? And do I need to install something somewhere on my host to use it?

Next: in my verify.php, what exactly do I need to put there, betwen the else brackets, given my form above?

I think your localhost option will work for me, as everything running here is on the same host and account, on a shared server host type.

Thanks, and sorry for my inexperience here!

echoniner

9:07 pm on Dec 12, 2010 (gmt 0)

10+ Year Member



ping?

genestoy

3:36 am on Feb 25, 2011 (gmt 0)

10+ Year Member



Hi all, newbie here and I would like to resurrect this topic as I have the same issue.
I have two forms, one that is a registration form and one that is a cancellation form. I have recaptcha showing on both forms just fine but nee to know how to get the recaptcha to check for the proper response and then continue on to run my ezform.cgi script which processes the form and sends me the emails. I have tried the verify.php script and all it ever does is display a blank page and never goes to the ezform.cgi script to complete the process.

Here is the form code:

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<title>Cancellation</title>

</head>
<body background="../bg.gif" text="#000000" bgcolor="#E5E5E5" link="#0000ff" vlink="#800080" alink="#ff0000">
<div align="center"><img src="../ezform/logo.jpg" width="610" height="109" border="0"></div>

<p>
<div align="center"><font face="Arial" size="3" color="#0066CC"><b><b>WE ARE SORRY THAT YOU ARE UNABLE TO ATTEND THE RALLY THIS YEAR</b>
<p>
<div align="center"><font face="Arial" size="3" color="#0066CC"><b>PLEASE USE THIS FORM TO CANCEL YOUR REGISTRATION</b>

</div>

<p>
<font face="Arial" size="2" color="#ff0000">Please note that this form is for cancelling your registration with the event only<br> and does not cancel your reservation with the State Parks (Reserve America)</font>
</div>
<p>

<script type="text/javascript">
var RecaptchaOptions = {
theme : 'white'
};
</script>


<div align="center">
<form method="post" action="../verify.php" name="regform3">
<input type="hidden" name="fname" value="Cancellation">
<table width=65% border="5" bordercolor="#0066CC" bgcolor="#C4DEFF">
</tr>
<tr align=right>
<td><font face="MS Sans Serif" size="2">(Attendee Name) First:</font></td>
<td align=left>
<input type=text name="FirstName" size=25><font face="MS Sans Serif" size="2">&nbsp;&nbsp;Last:</font> <input type=text name="LastName" size=25>&nbsp;&nbsp;<font face="MS Sans Serif" size="2" color="blue">Required</font></td>
</tr>

<tr align=right>
<td><font face="MS Sans Serif" size="2">Your&nbsp;email&nbsp;address:</font></td>
<td align=left>
<input type=text name="Email Address" size=40>&nbsp;&nbsp;<font face="MS Sans Serif" size="2" color="blue">Required</font></td>
</tr>

<tr align=right>
<td><font face="MS Sans Serif" size="2">Your loop (A, B, or C):</font></td>
<td align=left>
<input type=text name="Loop" size=1 Maxlength="1">&nbsp;</font><font face="MS Sans Serif" size="2" color="black">Your site #:</font>
<input type=text name="Site Number" size=1 Maxlength="2">&nbsp;&nbsp;<font face="MS Sans Serif" size="2" color="blue">Required</td></td>
</tr>

<tr>
<td align=right valign=top><font face="MS Sans Serif" size="2">Comments: <br>
</td>
<td align=left>
<textarea name ="Comments" cols="55" rows="6" wrap="virtual"></textarea>
</td>
</tr>
<input type="hidden" name="gif">
</table>

<p>
<div align="center"><font face="MS Sans Serif" size="2" color="#ff0000"><b>Please be aware that this form is to cancel your registration only.<br>This form will not cancel your reservation with State Parks (Reserve America)<br> and you will have to do that on your own</b></font>
</div>


<script type="text/javascript"

src="http://www.google.com/recaptcha/api/challenge?k=my_key">
</script>
<noscript>
<iframe src="http://www.google.com/recaptcha/api/noscript?k=my_key"
height="300" width="500" frameborder="0"></iframe><br>
<textarea name="recaptcha_challenge_field" rows="3" cols="40">
</textarea>
<input type="hidden" name="recaptcha_response_field"
value="manual_challenge">
</noscript>
<p>
<div align="center"><font face="MS Sans Serif" size="2" color="red">Type in the two words above before clicking the "Submit" button! If you can not read the words then click the&nbsp;<img src="../refresh.png">&nbsp;above</font></div>

<p>
<input type="image" SRC="cancel.gif" HEIGHT="38" WIDTH="224" BORDER="0" ALT="Submit Form"></form></div>

</body>

</html>


Here is the verify.php code:

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


I have tried the "curl" posted above but just don't understand how to make that work. Please help if you can.
Thanks, Gene

genestoy

2:33 pm on Feb 25, 2011 (gmt 0)

10+ Year Member



I have changed the verify.php to this and the recaptcha verify is working ok but still will not go to my ezform.cgi script with the information from the form? Any ideas why?


<?php

require_once('recaptchalib.php');
$privatekey = "my_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 {
// Your code here to handle a successful verification


?>

<form name="regform3" action="../cgi-bin/ezform.cgi">

<input type="hidden" name="fname" value="<?php $_POST['Cancellation']; ?>">

</form>

<script type="text/javascript">
var frm = document.getElementsByName("fname");
window.onload = frm.submit();
</script>

<?php

}

?>

genestoy

3:40 pm on Feb 25, 2011 (gmt 0)

10+ Year Member



I have also tried curl like this and get errors (see below code for error message)


<?php

require_once('recaptchalib.php');
$privatekey = "my_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 {
// Your code here to handle a successful verification

//create array of data to be posted
$post_data['fname'] = 'Cancelation';
$post_data['FirstName'] = 'FirstName';
$post_data['LastName'] = 'LastName';
$post_data['Email Adres'] = 'Email Adress';
$post_data['Loop'] = 'Loop';
$post_data['Site Number'] = 'Site Number';
$post_data['Comments'] = 'Comments';
$post_data['gif'] = 'gif';


//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//create cURL connection
$curl_connection =
curl_init('http://www.my_domain.com/cgi-bin/ezform.cgi');

//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

//perform our request
$result = curl_exec($curl_connection);

//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' .
curl_error($curl_connection);

//close the connection
curl_close($curl_connection);
}

?>


here is the errors that file generates:


Array ( [url] => http://www.my_domain.com/cgi-bin/ezform.cgi [content_type] => text/plain [http_code] => 200 [header_size] => 149 [request_size] => 351 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.275799 [namelookup_time] => 0.174788 [connect_time] => 0.174969 [pretransfer_time] => 0.174978 [size_upload] => 140 [size_download] => 51 [speed_download] => 184 [speed_upload] => 507 [download_content_length] => -1 [upload_content_length] => 0 [starttransfer_time] => 0.274824 [redirect_time] => 0 ) 0-