1

I created a PHP ticketing system. When uploading my script to the web I got the following error when redirecting to the second page:

Warning: Cannot modify header information - headers already sent by... output started at line 29.

I didn't have this error when developing on my local pc with wampserver.

The ticketing PHP consist of two pages:

  1. First Page: HTML form, PHP validates input after clicking on submit. If everything is OK ==> redirect to second page.
  2. Second page: selecting the tickets.

I realize I get this error because I output HTML before redirecting to another page. How can I resolve this? (because I have to output the HTML)

Kind regards

Tchoupi
  • 14,560
  • 5
  • 37
  • 71
Cloze
  • 11
  • 1
  • All kinds of solutions by using output buffering, _but_ it will be beneficial to your code in all kinds of ways if you stick with the 'process first' rule (which also means all kinds of headers & redirects) _before_ outputting _anything_. `ob_start()` is a crutch you can use, but for this, it's bad code smell. In short: don't write your applications to output HTML when it they're not done processing yet, what would happen if you decide you want to output something else, like xml, pdf, json, etc.? – Wrikken Jan 21 '14 at 20:09
  • Why would you want to output HTML before redirecting? – Tchoupi Jan 21 '14 at 20:10
  • It sounds like you're outputting stuff before the session. Can you provide your code? – Enijar Jan 21 '14 at 20:10
  • As a side note, you probably didn't get the warning message on your local server because of different settings for display_errors in your php.ini. You should also disable that on production. – Michael Helwig Jan 21 '14 at 20:11
  • It could be a number of things. A space before `` it's not needed. – Funk Forty Niner Jan 21 '14 at 20:12
  • I hope the second page does validation too... – Sumurai8 Jan 21 '14 at 20:13
  • Thanks everyone for answering my question. After reading all the comments, I realize that their is something structurally wrong my code. The javascript hack and meta hack works with my script, just tested it. However I want my script to be programmatically correct, so I will need to rewrite my code. Also as mentioned I have to revalidate on the second page. I guess the solution would be to keep step 1 and step 2 on the same page and hide/make visible step 2 with a div? Or do you guys have suggestions for a correct multiple step registration process? – Cloze Jan 22 '14 at 21:32

3 Answers3

0

That message has frustrated me many times also. This solution should get around it:

echo '<meta HTTP-EQUIV="REFRESH" content="0; url=newurl.php">';

Where the number zero in content="0; represents the number of seconds to pause before redirecting.

I suppose it goes without saying that you must replace newurl.php with the desired page name... just saying...

Since the above has its detractors, although I have used it myself with no observable negative results, here are some alternatives:

Redirects - alternative to "<meta http-equiv='refresh' />"?

The article linked in Sam Dufel's comment above is amazing:

How to fix "Headers already sent" error in PHP

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • This is just a hack to fix the problem. The problem should actually be fixed (i.e. don't output to browser before you attempt the redirect). Fix the actual problem don't add a hack to mask the problem. – Mike Brant Jan 21 '14 at 20:17
  • 1
    A hack is a fix someone else doesn't like. – cssyphus Jan 21 '14 at 20:21
  • In this case a hack is a hack. This one is even worse than normal as there would be no indication via typical server headers that a redirection is required, nor would there be any guarantee that the browser would obey the meta refresh. – Mike Brant Jan 21 '14 at 20:23
  • You are quite passionate about this Mike. Given your SO rep, I am sure you have your reasons. Although many continue to use this method without negative consequences, I look forward to your answer to this question. – cssyphus Jan 21 '14 at 20:45
  • I wasn't planning on answering this question, as it really is a duplicate to questions already asked a large number of times on SO. If I were to answer, I would give answer similar to what vooD gave, though I would specifically mention that the error message tells him exactly what line of code is the offending line of could when it mentions "output started at line 29" – Mike Brant Jan 21 '14 at 20:52
  • Thanks everyone for answering my question. After reading all the comments, I realize that their is something structurally wrong my code. The javascript hack and meta hack works with my script, just tested it. However I want my script to be programmatically correct, so I will need to rewrite my code. Also as mentioned I have to revalidate on the second page. I guess the solution would be to keep step 1 and step 2 on the same page and hide/make visible step 2 with a div? Or do you guys have suggestions for a correct multiple step registration process? – Cloze Jan 22 '14 at 21:20
0

Make sure you don't have any output before calling header() or session_start()

  • Make sure you don't have spaces anywhere before <?php in all php files
  • Remove ?> at the end of each php file
  • Change error_reporting to E_ALL and display_errors to On in php.ini on your development machine then run your application again and see if there are any errors. Fix them.
vooD
  • 2,881
  • 2
  • 25
  • 34
-1

Running the risk of getting martyred with this one but you can have javascript redirect you instead:

<?php
// instead of header('Location: someURL.php');
die('<script>window.location = \'someURL.php\';</script>');
?>

Otherwise, don't output ANYTHING before the header() call. Follow the rules and happy coding =)

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77