Trying to combine two sets of someone else's code

Trying to combine two sets of someone else’s code to put a Captcha on my form
My efforts so far are nearly there
But it not quite working properly for some reason if you don’t fill in the Captcha it goes to the home page
and dose not display the error message.

Below is my form

[code]

First Name *
Last name *
House name or number *
Address
Address
Address
Post code *
Phone Number *
Email *
Interested in
Security Code
 
 
[/code]

Next the php for the form

[php]<?php ob_start();
$fromemail=“No-Reply <@kensington-sashwindows.co.uk>”; // change here if you want
$toemail="[email protected]"; // change here if you want
$sub=“Online Feedback”; // change here if you want
$success_page_name=“success.html”;
////// do not change in following
if($_SERVER[‘REQUEST_METHOD’]==“POST”)
{
$fieldnm_1=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_1’]));
$fieldnm_2=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_2’]));
$fieldnm_3=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_3’]));
$fieldnm_4=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_4’]));
$fieldnm_5=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_5’]));
$fieldnm_6=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_6’]));
$fieldnm_7=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_7’]));
$fieldnm_8=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_8’]));
$fieldnm_9=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_9’]));
$fieldnm_10=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_10’]));

$contentmsg=stripslashes("
$sub

First Name *: $fieldnm_1
Last name *: $fieldnm_2
House name or number *: $fieldnm_3
Address: $fieldnm_4
Address: $fieldnm_5
Address: $fieldnm_6
Post code *: $fieldnm_7
Phone Number *: $fieldnm_8
Email *: $fieldnm_9
Comments : $fieldnm_10
"); //// $headers = "MIME-Version: 1.0 "; $headers .= "Content-type: text/html; charset=iso-8859-1 "; $from=$fromemail; $headers .= "From: ".$from." "; @mail($toemail,$sub,$contentmsg,$headers); header("Location:$success_page_name"); } ?>[/php]

The code for the Captcha

[php]<?php
session_start();

if( isset($_POST[‘submit’]))
{
if( $_SESSION[‘security_code’] == $_POST[‘security_code’] && !empty($_SESSION[‘security_code’] ) )
{
// Insert you code for processing the form here, e.g emailing the submission, entering it into a database.
echo ‘Thank you. Your message said "’.$_POST[‘message’].’"’;
unset($_SESSION[‘security_code’]);
}
else
{
// Insert your code for showing an error message here
echo ‘Sorry, you have provided an invalid security code’;
}
} else {
?>[/php]

My efforts so far any ideas why it goes to the home page, many thanks Paul

[php]<?php session_start();
if(($_SESSION[‘security_code’] == $_POST[‘security_code’]) && (!empty($_SESSION[‘security_code’])) ) {
// Insert you code for processing the form here, e.g emailing the submission, entering it into a database.
$fromemail=“No-Reply <@kensington-sashwindows.co.uk>”; // change here if you want
$toemail="[email protected]"; // change here if you want
$sub=“Online Feedback”; // change here if you want
$success_page_name=“success.html”;
////// do not change in following

unset($_SESSION['security_code']);

}else { “Sorry, you have provided an invalid security code”;

}

////// do not change in following
if($_SERVER[‘REQUEST_METHOD’]==“POST”){

$fieldnm_1=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_1’]));
$fieldnm_2=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_2’]));
$fieldnm_3=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_3’]));
$fieldnm_4=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_4’]));
$fieldnm_5=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_5’]));
$fieldnm_6=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_6’]));
$fieldnm_7=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_7’]));
$fieldnm_8=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_8’]));
$fieldnm_9=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_9’]));
$fieldnm_10=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_10’]));

$contentmsg=stripslashes("
$sub

First Name *: $fieldnm_1
Last name *: $fieldnm_2
House name or number *: $fieldnm_3
Address: $fieldnm_4
Address: $fieldnm_5
Address: $fieldnm_6
Post code *: $fieldnm_7
Phone Number *: $fieldnm_8
Email *: $fieldnm_9
Comments : $fieldnm_10
");

////
$headers = "MIME-Version: 1.0
";
$headers .= "Content-type: text/html; charset=iso-8859-1
";

$from=$fromemail;

$headers .= "From: “.$from.”
";

@mail($toemail,$sub,$contentmsg,$headers);

header(“Location:$success_page_name”);
}?>[/php]

Well, first, if your code does not work, you have this code executed:

}else { “Sorry, you have provided an invalid security code”;

What is that? The else does not have any type of PHP code! ? !

Shouldn’t it be }else { echo “Sorry, you have provided an invalid security code”;

Also, I am just looking at the last code you posted. You check for a matching security entry. Then, you set up some variables to be sent by email and if not you sort-of display an error message. THEN, no matter of the results, you send an email. This makes little sense to me.

It should be something more like, check for the matching security entry. If matches, send the email, if not, display the error message. The email code should be inside the matching security success code not after both results are handled. The email is part of the success section.

Hope that helps… If not ask us further questions. Good luck.

Alter code as you said now nothing works just getting a blank page.

[php]<?php
session_start();

if( isset($_POST[‘submit’]))
{
if( $_SESSION[‘security_code’] == $_POST[‘security_code’] && !empty($_SESSION[‘security_code’] ) )
{
$fromemail=“No-Reply [email protected]”; // change here if you want
$toemail="[email protected]"; // change here if you want
$sub=“Online Feedback”; // change here if you want
$success_page_name=“success.html”;
////// do not change in following
if($_SERVER[‘REQUEST_METHOD’]==“POST”)

$fieldnm_1=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_1’]));
$fieldnm_2=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_2’]));
$fieldnm_3=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_3’]));
$fieldnm_4=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_4’]));
$fieldnm_5=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_5’]));
$fieldnm_6=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_6’]));
$fieldnm_7=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_7’]));
$fieldnm_8=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_8’]));
$fieldnm_9=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_9’]));
$fieldnm_10=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_10’]));

$contentmsg=stripslashes("
$sub

First Name *: $fieldnm_1
Last name *: $fieldnm_2
House name or number *: $fieldnm_3
Address: $fieldnm_4
Address: $fieldnm_5
Address: $fieldnm_6
Post code *: $fieldnm_7
Phone Number *: $fieldnm_8
Email *: $fieldnm_9
Comments : $fieldnm_10
"); //// $headers = "MIME-Version: 1.0 "; $headers .= "Content-type: text/html; charset=iso-8859-1 "; $from=$fromemail; $headers .= "From: ".$from." "; @mail($toemail,$sub,$contentmsg,$headers); header("Location:$success_page_name");
	unset($_SESSION['security_code']);

}else
{
// Insert your code for showing an error message here
echo ‘Sorry, you have provided an invalid security code’;
}
}
?>[/php]

Well, you didn’t actually write it as I explained, but, close… First, you have several issues with the formatting of the code. You start off by checking for a POSTED variable. Then, a few lines down you have another check for POSTing but, no grouping if the IF is true. It just hangs there.

Basically, the line ///////do not change and the following line do nothing. If it is true, then the first fieldnm grab line will work and then it continues to the next line. this does not make sense! What I did was take your code and format it using TABS, each IF is indented and each {} for each IF is indented and then, the bad line jumped out at me.

Next, you have a major issue with the email itself. You can NOT stripslashes for an HTML email. All of your table code will fail. It will strip out the slashes for tags, etc… This will make the HTML fail once the email is sent. If you feel you need to strip slashes from the content, you should do it where you create the fieldnm’s. You currently have this for the first field:
$fieldnm_1=str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_1’]));
If you need to strip them, it should be on the fields not the content, something like this:
$fieldnm_1=stripslashes(str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_1’])));
(Note the extra ( ) …)

Lastly, your email code is messed up. Your headers code is using double-quotes INSIDE double-quotes. That is a no-no in any language code. " " " " means “” “” not " ’ ’ "… So, that needs to be fixed up. I did not actually check your headers for formatting. I normally do not send emails that way, but, it looks like it should work once you alter the inside double-quotes.

One final note, once you have code that has a “HEADER” command in it, all code after that does not work. It is already gone from the page. So your UNSET command has to be BEFORE you switch pages. (It does not reach that line in your code. I moved it up a line…) And, if you use multiple HEADERS, you must separate them using CRLF’s. (In this case, “\r\n”) I added those…

So, here is a new version to test. Hope it works, if not we will have to add some debugging lines to see where it fails. Oh, also, on the page “success.html”, do you have something on that page? If that page is blank, you will see a blank page… Here’s my new version for you to review:
(Hope I located all the issues…)

[php]

<?php session_start(); if( isset($_POST['submit'])) { if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) { $fromemail="No-Reply "; // change here if you want $toemail="[email protected]"; // change here if you want $sub="Online Feedback"; // change here if you want $success_page_name="success.html"; $fieldnm_1=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_1']))); $fieldnm_2=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_2']))); $fieldnm_3=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_3']))); $fieldnm_4=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_4']))); $fieldnm_5=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_5']))); $fieldnm_6=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_6']))); $fieldnm_7=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_7']))); $fieldnm_8=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_8']))); $fieldnm_9=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_9']))); $fieldnm_10=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_10']))); $contentmsg="
$sub
First Name *: $fieldnm_1
Last name *: $fieldnm_2
House name or number *: $fieldnm_3
Address: $fieldnm_4
Address: $fieldnm_5
Address: $fieldnm_6
Post code *: $fieldnm_7
Phone Number *: $fieldnm_8
Email *: $fieldnm_9
Comments : $fieldnm_10
"; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: '$fromemail'"; @mail($toemail,$sub,$contentmsg,$headers); unset($_SESSION['security_code']); header("Location:$success_page_name"); } else { // Insert your code for showing an error message here echo 'Sorry, you have provided an invalid security code'; } } ?>

[/php]

Hi all
you get this warning message when you try to submit with the security code entered correctly.

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/kensing1/public_html/Testfolder/_sendmail.php:2) in /home/kensing1/public_html/Testfolder/_sendmail.php on line 3

And the same error message when you try to submit with the security code entered incorrectly.

You can only send your headers once per session. My guess is that it is the IF structure.
Can you post your latest code to see if you made the changes correctly?

It must be something small…

[php]

<?php session_start(); if( isset($_POST['submit'])) { if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) { $fromemail="'No-Reply '"; // change here if you want $toemail="'[email protected]'"; // change here if you want $sub="'Online Feedback'"; // change here if you want $success_page_name="'success.html'"; $fieldnm_1=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_1']))); $fieldnm_2=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_2']))); $fieldnm_3=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_3']))); $fieldnm_4=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_4']))); $fieldnm_5=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_5']))); $fieldnm_6=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_6']))); $fieldnm_7=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_7']))); $fieldnm_8=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_8']))); $fieldnm_9=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_9']))); $fieldnm_10=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_10']))); $contentmsg="
$sub
First Name *: $fieldnm_1
Last name *: $fieldnm_2
House name or number *: $fieldnm_3
Address: $fieldnm_4
Address: $fieldnm_5
Address: $fieldnm_6
Post code *: $fieldnm_7
Phone Number *: $fieldnm_8
Email *: $fieldnm_9
Comments : $fieldnm_10
"; $headers = "'MIME-Version: 1.0\r\n'"; $headers .= "'Content-type: text/html; charset=iso-8859-1\r\n'"; $headers .= "'From: $fromemail'"; @mail($toemail,$sub,$contentmsg,$headers); unset($_SESSION['security_code']); header("'Location:$success_page_name'"); } else { // Insert your code for showing an error message here echo 'Sorry, you have provided an invalid security code'; } } ?>

[/php]

Hmmmm, in my example everything look correct. In your version, you added a lot of single quotes inside of double quotes. This means that you are loading the variables with single quoted values not the real values. This may be the problem. Two issues pop up with the code. First, the ‘@’ character in front of the mail() command suppresses mailing errors. This is just fine except when you are debugging mailing scripts like this one. I suggest removing that for now and if you want it there, add it back in once the script is working correctly! Next, since you added in the single quotes around the success URL file name, it shouldn’t work as it will be looking for a page named ‘success.html’ not success.html … PHP is EXACT with filenames and caps, too.

So, removing the extra single quotes in the first part of the mail setup:

			$fromemail="No-Reply <[email protected]>"; // change here if you want
			$toemail="[email protected]";   // change here if you want
			$sub="Online Feedback";          // change here if you want
			$success_page_name="success.html";

And, removing the extra single quotes in the last part of the setup:

			$headers  = "MIME-Version: 1.0\r\n";
			$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
			$headers .= "From: $fromemail";
			mail($toemail,$sub,$contentmsg,$headers);
			unset($_SESSION['security_code']);
			header("Location:$success_page_name");

Should get it working. Try it and let us know what happens. You are very close!

Hi Latest message

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/kensing1/public_html/Testfolder/_sendmail.php:2) in /home/kensing1/public_html/Testfolder/_sendmail.php on line 3

[php]

<?php session_start(); if( isset($_POST['submit'])) { if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) { $fromemail="No-Reply "; // change here if you want $toemail="[email protected]"; // change here if you want $sub="Online Feedback"; // change here if you want $success_page_name="success.html"; $fieldnm_1=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_1']))); $fieldnm_2=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_2']))); $fieldnm_3=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_3']))); $fieldnm_4=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_4']))); $fieldnm_5=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_5']))); $fieldnm_6=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_6']))); $fieldnm_7=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_7']))); $fieldnm_8=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_8']))); $fieldnm_9=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_9']))); $fieldnm_10=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_10']))); $contentmsg="
$sub
First Name *: $fieldnm_1
Last name *: $fieldnm_2
House name or number *: $fieldnm_3
Address: $fieldnm_4
$fieldnm_5
Address: $fieldnm_6
Post code *: $fieldnm_7
Phone Number *: $fieldnm_8
Email *: $fieldnm_9
Comments : $fieldnm_10
"; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: $fromemail"; mail($toemail,$sub,$contentmsg,$headers); unset($_SESSION['security_code']); header("Location:$success_page_name"); } else { // Insert your code for showing an error message here echo 'Sorry, you have provided an invalid security code'; } } ?>

[/php]

So, your code IS working and you have an error inside your mail code. (It is trying to send out the email and it fails inside your mail code.)

I do not have any special email code on any of my servers. Quite often this is from using someone else’s PHP email handler such as “PHPmailer” library or one of the other email libraries. You do not need to use a library unless you need some special mail handling. Anyway, you have to look inside that file and show the first few lines of it to us:

/home/kensing1/public_html/Testfolder/_sendmail.php:2) in /home/kensing1/public_html/Testfolder/_sendmail.php on line 3

The file is in Testfolder and is called “_sendmail.php” Show us the first five or so lines of it…

End that sentence right there… :wink:

I suggest you follow Alex’s advice and stop suppressing errors, try this:
[php]if(mail($toemail,$sub,$contentmsg,$headers)) {
// mail success, do something
}
else {
// mail fail, do something else…
}[/php]

Just my two-pence worth,
Red :wink:

The file we are working on is the one called “_sendmail.php”

[php]

<?php session_start(); if( isset($_POST['submit'])) { if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) { $fromemail="No-Reply "; // change here if you want $toemail="[email protected]"; // change here if you want $sub="Online Feedback"; // change here if you want $success_page_name="success.html"; $fieldnm_1=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_1']))); $fieldnm_2=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_2']))); $fieldnm_3=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_3']))); $fieldnm_4=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_4']))); $fieldnm_5=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_5']))); $fieldnm_6=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_6']))); $fieldnm_7=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_7']))); $fieldnm_8=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_8']))); $fieldnm_9=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_9']))); $fieldnm_10=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_10']))); $contentmsg="
$sub
First Name *: $fieldnm_1
Last name *: $fieldnm_2
House name or number *: $fieldnm_3
Address: $fieldnm_4
$fieldnm_5
Address: $fieldnm_6
Post code *: $fieldnm_7
Phone Number *: $fieldnm_8
Email *: $fieldnm_9
Comments : $fieldnm_10
"; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: $fromemail"; mail($toemail,$sub,$contentmsg,$headers); unset($_SESSION['security_code']); header("Location:$success_page_name"); } else { // Insert your code for showing an error message here echo 'Sorry, you have provided an invalid security code'; } } ?>

[/php]

Well, I researched this issue previously for another coder who had the same troubles.
Then, a year or two later, I had the same problem and did the research further.
In both cases it was due to spaces or other text being pushed to the browser BEFORE starting the session!

So, I suspect you are only showing us a “clip” of your code. Is the code you posted the entire _sendmail.php file? Is the first two lines of that file <?PHP and session_start(); ???

I will explain why this is important…

When a server sends ANYTHING to the browser, it starts with a header which tells the browser what type of content is heading it’s way. Therefore, if you have some code laid out something like this:

Here is your info you requested... <?PHP session_start(); echo $_SESSION['some-session-variable']; ?> That ends your request... Now, this looks like a correctly formatted page. A simple "display" of some requested info that was stored in a session variable. But, if you look at it closer, the first actual item sent to the browser is the text telling the user that here is the requested info! To send text to a browser, the server first sends out a header. Actually, the header is created in the HTML/BODY area to tell the browser that it is sending a webpage.

In this example, AFTER the header is sent, the PHP section attempts to send a new header starting a PHP/Browser session. Since you can NOT send two headers unless the first is closed, you receive an error similar to the one you are receiving. Even ONE space character in the first line can cause the server to send HTML/BODY/TEXT headers to the browser.

So, the next question is: Is the posted code ALL of the code in the file _sendmail.php??? Also, is this file being used inside an INCLUDED file? If you have this file and include it into another one, it has to be the very first line so that the session is started before the parent file sends any headers…

Let us know, and I hope that helps…

Definitely all the code for _sendmail.php file, Have you taken into account the form is on a separate html page, which I posted right at the beginning of this thread.

[code]

First Name *
Last name *
House name or number *
Address
Address
Address
Post code *
Phone Number *
Email *
Interested in
Security Code
 
 
[/code]

I’ve taken all the spaces out of the code and now get blank pages when submitting with correct Captcha and incorrect Captcha

[php]<?php
session_start();
if( isset($_POST[‘submit’]))
{
if( $_SESSION[‘security_code’] == $_POST[‘security_code’] && !empty($_SESSION[‘security_code’] ) )
{
$fromemail=“No-Reply [email protected]”; // change here if you want
$toemail="[email protected]"; // change here if you want
$sub=“Online Feedback”; // change here if you want
$success_page_name=“success.html”;
$fieldnm_1=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_1’])));
$fieldnm_2=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_2’])));
$fieldnm_3=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_3’])));
$fieldnm_4=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_4’])));
$fieldnm_5=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_5’])));
$fieldnm_6=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_6’])));
$fieldnm_7=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_7’])));
$fieldnm_8=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_8’])));
$fieldnm_9=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_9’])));
$fieldnm_10=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_10’])));
$contentmsg="
$sub

First Name *: $fieldnm_1
Last name *: $fieldnm_2
House name or number *: $fieldnm_3
Address: $fieldnm_4
$fieldnm_5
Address: $fieldnm_6
Post code *: $fieldnm_7
Phone Number *: $fieldnm_8
Email *: $fieldnm_9
Comments : $fieldnm_10
"; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: $fromemail";} if(mail($toemail,$sub,$contentmsg,$headers)) { unset($_SESSION['security_code']); header("Location:$success_page_name"); } else { // Insert your code for showing an error message here echo 'Sorry, you have provided an invalid security code'; } } ?> [/php]

Paul,

One last time…

In the file you posted, it starts with one line that has a space in it. This sends the header to the browser that HTML is being sent. Then, you send a session header next. You can NOT do that.

I copied and pasted your _sendmail.php code that you posted and the very first line is a space! You CAN NOT do that as it sends a HTML header to the browser. Then, the session_start() sends a second one!

Unless you mistyped the file and added that line by mistake, but, in my editor (Dreamweaver) I copied your _sendmail.php code AS-IS and the “session_start();” line is #3 which is where the error message states is the problem. This is ALL 100% correct if you have a space in the first line of your code. This is as clear as I can be!

REMOVE the FIRST BLANK LINE so that the very first line in _sendmail.php is “<?PHP” and the “session_start();” line will become line #2 and that should fix it up.

Hope you understand that this time. Good luck…

Okay, so now, you do NOT get any errors as you removed the first blank line.

Now, you are getting blank pages. So, let’s debug that issue which is not related to the error you were getting before…

You have your logic incorrect in how you handle your emails validation.

This is how it is currently:

—check for a post
------check for security
----------if security okay, set up email
—send email not matter what happened before
------if emails sent, go to success page (Should always do that as prior checks do not matter)
------if emails fail, echo error (Should never happen unless your email address is incorrect)
end

So, you never handle any error if the security check fails.
The email is sent no matter what happens with the security check.

So, here is a new version of your code with an else added for the security.
(Not tested, but should do it. I added comments where you were missing the else for the security check)

[php]

<?php session_start(); if( isset($_POST['submit'])) { if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) { $fromemail="No-Reply "; // change here if you want $toemail="[email protected]"; // change here if you want $sub="Online Feedback"; // change here if you want $success_page_name="success.html"; $fieldnm_1=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_1']))); $fieldnm_2=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_2']))); $fieldnm_3=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_3']))); $fieldnm_4=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_4']))); $fieldnm_5=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_5']))); $fieldnm_6=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_6']))); $fieldnm_7=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_7']))); $fieldnm_8=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_8']))); $fieldnm_9=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_9']))); $fieldnm_10=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST['fieldnm_10']))); $contentmsg="
$sub
First Name *: $fieldnm_1
Last name *: $fieldnm_2
House name or number *: $fieldnm_3
Address: $fieldnm_4
$fieldnm_5
Address: $fieldnm_6
Post code *: $fieldnm_7
Phone Number *: $fieldnm_8
Email *: $fieldnm_9
Comments : $fieldnm_10
"; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: $fromemail"; // Security checked out and email set up, now try to send it... if(mail($toemail,$sub,$contentmsg,$headers)) { unset($_SESSION['security_code']); header("Location:$success_page_name"); } else { // Insert your code for showing an error message here echo 'Sorry, you have provided an invalid security code'; } } else { // Security check failed, say so... echo "Security check failed!"; } // END of security check code... } // END of isset(post)... ?>

[/php]

hi still the same blank pages for both incorrect and correct security check

[php]<?php
session_start();
if( isset($_POST[‘submit’])) {
if( $_SESSION[‘security_code’] == $_POST[‘security_code’] && !empty($_SESSION[‘security_code’] ) ) {
$fromemail=“No-Reply [email protected]”; // change here if you want
$toemail="[email protected]"; // change here if you want
$sub=“Online Feedback”; // change here if you want
$success_page_name=“success.html”;
$fieldnm_1=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_1’])));
$fieldnm_2=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_2’])));
$fieldnm_3=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_3’])));
$fieldnm_4=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_4’])));
$fieldnm_5=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_5’])));
$fieldnm_6=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_6’])));
$fieldnm_7=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_7’])));
$fieldnm_8=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_8’])));
$fieldnm_9=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_9’])));
$fieldnm_10=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_10’])));
$contentmsg="
$sub

First Name *: $fieldnm_1
Last name *: $fieldnm_2
House name or number *: $fieldnm_3
Address: $fieldnm_4
$fieldnm_5
Address: $fieldnm_6
Post code *: $fieldnm_7
Phone Number *: $fieldnm_8
Email *: $fieldnm_9
Comments : $fieldnm_10
"; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: $fromemail"; // Security checked out and email set up, now try to send it... if(mail($toemail,$sub,$contentmsg,$headers)) { unset($_SESSION['security_code']); header("Location:$success_page_name"); } else { // Insert your code for showing an error message here echo 'Sorry, you have provided an invalid security code'; } } else { // Security check failed, say so... echo "Security check failed!"; } // END of security check code... } // END of isset(post)... ?>[/php]

Hmmm… First, I forgot to rewrite your error messages. The tail end of that page looks like:
// Insert your code for showing an error message here
echo ‘Sorry, you have provided an invalid security code’;
}
} else {
// Security check failed, say so…
echo “Security check failed!”;
} // END of security check code…
} // END of isset(post)…
?>
It should be:
[php]
// Insert your code for showing an email error message here
echo ‘Email error, message was not sent!’;
}
} else {
// Security check failed, say so…
echo “Sorry, you have provided an invalid security code!”;
} // END of security check code…
} // END of isset(post)…
?>
[/php]
Sorry, flying fingers…

Now, I see nothing wrong with that in that code. So, first, did you receive the email? And, what do you have in the success page? Do you have anything echo’d in the success page?

If you did not receive the email, then, we need to debug the inputs to the IF’s. If you received the email, then we have to debug the redirecting code.

To debug the inputs to the IF’s, you just need to change the first few lines to this:
[php]

<?php session_start(); // Temporary debug displays... print_r($_POST); echo "

"; print_r($_SESSION); echo "

"; die("Debug listing complete"); // End of temporary debug code... if( isset($_POST['submit'])) { [/php] What this debug code does is accept your input, prints all the POSTed variables and all the SESSION variables that are currently in effect. They are printed in the format variable-name=>variable-value, etc. Next it ends the page so you can read the values. You can walk thru your code and do the compares manually and see where the error occurs. Or, you can post the results here for us to review. If you post it here, blank out any userid's or passwords in the results. You must be extremely close to the solution, it is just some small issue. Let us know...

Hi one for a correct code
Array ( [fieldnm_1] => Paul [fieldnm_2] => Fereday [fieldnm_3] => Romart [fieldnm_4] => [fieldnm_5] => [fieldnm_6] => [fieldnm_7] => LL11 4SR [fieldnm_8] => 0123456789 [fieldnm_9] => [email protected] [fieldnm_10] => Testing [security_code] => dy337y [Submit] => Submit )

Array ( [security_code] => dy337y )

Debug listing complete

One for incorrect code

Array ( [fieldnm_1] => Paul [fieldnm_2] => Fereday [fieldnm_3] => Romart [fieldnm_4] => [fieldnm_5] => [fieldnm_6] => [fieldnm_7] => LL11 4SR [fieldnm_8] => 0123456789 [fieldnm_9] => [email protected] [fieldnm_10] => Testing [security_code] => [Submit] => Submit )

Array ( [security_code] => gh8rwp ) But I left the security code field blank

Debug listing complete

[php]<?php
session_start();

// Temporary debug displays…
print_r($_POST);
echo “

”;
print_r($_SESSION);
echo “

”;
die(“Debug listing complete”);
// End of temporary debug code…

if( isset($_POST[‘submit’])) {
if( $_SESSION[‘security_code’] == $_POST[‘security_code’] && !empty($_SESSION[‘security_code’] ) ) {
$fromemail=“No-Reply [email protected]”; // change here if you want
$toemail="[email protected]"; // change here if you want
$sub=“Online Feedback”; // change here if you want
$success_page_name=“success.html”;
$fieldnm_1=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_1’])));
$fieldnm_2=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_2’])));
$fieldnm_3=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_3’])));
$fieldnm_4=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_4’])));
$fieldnm_5=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_5’])));
$fieldnm_6=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_6’])));
$fieldnm_7=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_7’])));
$fieldnm_8=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_8’])));
$fieldnm_9=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_9’])));
$fieldnm_10=stripslashes( str_replace ( array("\n"), array("
"),trim($_REQUEST[‘fieldnm_10’])));
$contentmsg="
$sub

First Name *: $fieldnm_1
Last name *: $fieldnm_2
House name or number *: $fieldnm_3
Address: $fieldnm_4
$fieldnm_5
Address: $fieldnm_6
Post code *: $fieldnm_7
Phone Number *: $fieldnm_8
Email *: $fieldnm_9
Comments : $fieldnm_10
"; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: $fromemail"; // Security checked out and email set up, now try to send it... if(mail($toemail,$sub,$contentmsg,$headers)) { unset($_SESSION['security_code']); header("Location:$success_page_name"); } else { // Insert your code for showing an email error message here echo 'Email error, message was not sent!'; } } else { // Security check failed, say so... echo "Sorry, you have provided an invalid security code!"; } // END of security check code... } // END of isset(post)... ?> [/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service