Trying to redirect to a new page, and it is failing.
06-30-2010, 01:20 PM
|
Trying to redirect to a new page, and it is failing.
|
Posts: 14
Name: Lee Wentzel
Location: Michigan, USA
|
I scoured this site as well as W3Cschool.com and am doing what I believe should work but it doesn't. Any suggestions would be appreciated. The error is pointing to the head line.
Code:
<?php
$contact = $_POST['contPref'];
$address = $_POST['eMail'];
$subject = 'EMail from the website';
$fname = $_POST['fName'];
$lname = $_POST['lName'];
$street1 = $_POST['street1'];
$street2 = $_POST['street2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email = $_POST['emailadd'];
$comments = $_POST['comments'];
$mailsend = mail($address, $subject, "Name: $fname $lname
Contact: $contact
Address: $street1
$street2
$city , $state $zip
Phone: $phone
Email: $email
Comments: $comments");
echo $mailsend;
header("Location:" . 'thankyou.htm');
?>
The error message I am getting is:
Quote:
|
Cannot modify header information - headers already sent by (output started at /home/content/89/6324089/html/mailme.php:26) in /home/content/89/6324089/html/mailme.php on line 28
|
__________________
It's never "just a game" when you're winning.
~ George Carlin ~
Last edited by leebert45; 06-30-2010 at 01:25 PM..
Reason: Adding error message
|
|
|
|
06-30-2010, 01:28 PM
|
Re: Trying to redirect to a new page, and it is failing.
|
Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
|
headers have to be sent before any output is.
__________________
Chris. ->> Please login or register to view this content. Registration is FREE <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
|
|
|
|
06-30-2010, 01:32 PM
|
Re: Trying to redirect to a new page, and it is failing.
|
Posts: 14
Name: Lee Wentzel
Location: Michigan, USA
|
Yup, sort of figured that out. I deleted the "echo" line just above the header and it worked flawlessly. Thanks.
__________________
It's never "just a game" when you're winning.
~ George Carlin ~
|
|
|
|
09-06-2010, 11:59 PM
|
Re: Trying to redirect to a new page, and it is failing.
|
Posts: 34
|
i think you should re frame the coding of the article or there are some errors which can be modified with the help of the experts like the site developers
|
|
|
|
09-08-2010, 09:58 AM
|
Re: Trying to redirect to a new page, and it is failing.
|
Posts: 408
Name: mushget
|
The fix is, obviously, to remove that whitespace from the file. Read the error message carefully. It says "output started at ..." followed by a file name and a line number. That is the file (and line) that you need to edit. Ignore the second file name - that is only a file that included the file that has the whitespace. The first file is the one you have to edit, not the second one.
|
|
|
|
09-08-2010, 01:19 PM
|
Re: Trying to redirect to a new page, and it is failing.
|
Posts: 335
Name: Jerry
|
Quote:
Originally Posted by mushget
The fix is, obviously, to remove that whitespace from the file. Read the error message carefully. It says "output started at ..." followed by a file name and a line number. That is the file (and line) that you need to edit. Ignore the second file name - that is only a file that included the file that has the whitespace. The first file is the one you have to edit, not the second one.
|
sorry my friend but you are incorrect... you should check the previous postings prior as well... the problem has been solved...
header outputs cannot be made after other outputs has been made... the echo prior to the header is affecting it that's why..
|
|
|
|
09-11-2010, 02:35 AM
|
Re: Trying to redirect to a new page, and it is failing.
|
Posts: 40
Name: Fernades jacob
|
Add the following line on the first line of the page that you are redirecting ob_start()
ie
ob_start()
This should be the first line of the script, that is after
<?php
ob_start();
$contact = $_POST['contPref'];
$address = $_POST['eMail'];
$subject = 'EMail from the website';
$fname = $_POST['fName'];
$lname = $_POST['lName'];
$street1 = $_POST['street1'];
$street2 = $_POST['street2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email = $_POST['emailadd'];
$comments = $_POST['comments'];
$mailsend = mail($address, $subject, "Name: $fname $lname
Contact: $contact
Address: $street1
$street2
$city , $state $zip
Phone: $phone
Email: $email
Comments: $comments");
echo $mailsend;
header("Location:" . 'thankyou.htm');
?>
|
|
|
|
09-11-2010, 05:36 PM
|
Re: Trying to redirect to a new page, and it is failing.
|
Posts: 805
Name: Will Craig
Location: Cheltenham, Gloucestershire, UK
|
fernades, It's not a good idea to use an output buffer as a quick fix, why echo anything if you are just redirecting the user to another page?
All you are doing is adding extra load onto the server. Besides you forgot to flush the buffers.
|
|
|
|
09-12-2010, 01:23 PM
|
Re: Trying to redirect to a new page, and it is failing.
|
Posts: 99
|
I use redirections in my code so much that I find the headers already sent error too obtrusive. So I just use javascript :
PHP Code:
function redirect($page) { echo "<script type='text/javascript'>window.location = '$page';</script>"; die(); }
By no means the best solution but at least it can be used anywhere in the script.
|
|
|
|
09-12-2010, 01:49 PM
|
Re: Trying to redirect to a new page, and it is failing.
|
Posts: 843
Name: Mike
Location: United Kingdom
|
Quote:
Originally Posted by Marik
I use redirections in my code so much that I find the headers already sent error too obtrusive. So I just use javascript
|
The problem with javascript is users can disable/not support it (Think screen readers or mobile devices), so that puts you back into square one.
I'm also not a big fan of ob_start(), mostly because the page load times drop dramatically because the page will only be sent once it's finished processing instead as it's outputted. Here is my solution:
PHP Code:
// [...]
if(mail($address, $subject, "[..]")){ // mail() only returns TRUE or FALSE. header('location: thankyou.htm'); die(); } die('There was an error sending mail'); // Remember to stop your script running after sending the header, some browsers/bots (Such as googlebot) ignore headers.
Never forgot the die() bit, otherwise you may be attacked by a Spider of Doom.
__________________
My Blog/Site: Please login or register to view this content. Registration is FREE
Last edited by rogem002; 09-12-2010 at 01:58 PM..
|
|
|
|
09-12-2010, 05:38 PM
|
Re: Trying to redirect to a new page, and it is failing.
|
Posts: 7
Name: Iman Ghasrfakhri
|
In here removing the echo is the best way, I use meta redirect in the cases that I cannot prevent outputs before header-redirect, it is just like that:
HTML Code:
<meta http-equiv="refresh" content="0; URL='URL-To-An-Othe-Page'" />
You can also change the '0' value to a number to redirect after few seconds (it is time to wait before transfer the user to new page)
__________________
Please login or register to view this content. Registration is FREE
, the first Persian freelancing website.
Quote:
|
Who can, do; who can't, teach!
|
Perfect Support, reasonable prices just in Please login or register to view this content. Registration is FREE
. Shared, Dedicated, Cloud Hosting.
|
|
|
|
09-19-2010, 12:14 PM
|
Re: Trying to redirect to a new page, and it is failing.
|
Posts: 805
Name: Will Craig
Location: Cheltenham, Gloucestershire, UK
|
You should never rely on client side browsers behaving as expected. I am intrigued about this spider issue, but fail to see how the die statement above prevents this.
Surely the solution is to use SESSION cookies to detect a logged in user not browser cookies? I only use die statements for errors which would only occur if the user was using the website in an unexpected way, i.e. a hacker.
Aren't PHP redirects handled by the server? Why would you kill the page because the email was successfully sent?
Last edited by mad_willsy; 09-19-2010 at 12:15 PM..
|
|
|
|
|
« Reply to Trying to redirect to a new page, and it is failing.
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|