Forum Moderators: coopster

Message Too Old, No Replies

Header location problem

php script not working properly

         

Perkin5

2:20 pm on Jun 17, 2010 (gmt 0)

10+ Year Member



I have a header location problem despite following all the advice I can find online. The mailer script sends the email(s) but I get a 'headers already sent' error relating to Line 29 which is {header("Location: $thanksURL");} and no redirect to the confirmation page. If I replace the header location code with an instruction to print a confirmatory message instead, it works, so there must be something about the header location code that the server doesn't like.

Here is the code:

<?php
ob_start();
$to = "msheath@btinternet.com" ;
$from = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$headers = "From: $from";
$subject = "Request for Library Document";
$thanksURL = "http://www.postalhistory.org.uk/newsite/php/thankyou.php"; //confirmation page
$fields = array();
$fields{"name"} = "Name";
$fields{"address"} = "Address";
$fields{"email"} = "Email";
$fields{"tel"} = "Telephone No";
$fields{"author1"} = "First Author";
$fields{"title1"} = "First Title";
$fields{"author2"} = "Second Author";
$fields{"title2"} = "Second Title";
$body = "I would like to borrow the undermentioned book(s) from the PHS Library:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
$headers2 = "From: The Librarian, Postal History Society";
$subject2 = "Thank you for contacting the Postal History Society";
$autoreply = "Thank you for your request. Somebody will get back to you as soon as possible, usually within 48 hours.";
if($from == '') {print "You have not entered an email, please go back and try again";}
else {
if($name == '') {print "You have not entered a name, please go back and try again";}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{header("Location: $thanksURL");}
else
{print "We encountered an error sending your mail, please notify webmaster@YourCompany.com"; }
}
}
ob_end_flush()
?>

Go to [postalhistory.org.uk...] to try it out for yourself.

Can anyone suggest what is wrong?

Mike

Readie

6:14 pm on Jun 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If anything - anything is being printed, or echoed, or basically sent to the browser (that includes anything outside of the <php ?> tags - including white space) the header() will not work.

rocknbil

6:26 pm on Jun 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




if($from == '') {print "You have not entered an email, please go back and try again";}
else {
if($name == '') {print "You have not entered a name, please go back and try again";}


Are these printing first? I'm guessing they are. These errors should be concatenated and stored in variables, or an exit; added after each line inside the "if" blocks.

<OT>Interesting that you posted this. Had a client ask me to mod this exact same script **just yesterday.** I nearly flipped a lid seeing it on their server, and completely revamped it, it's horribly vulnerable to injection and is a poor interface, anything that says "go back" is a level -10 in usability.</OT>

Anyway . . . add exit; after your errors, see if it fixes it.