25

Here's my file. I want to make it redirect, but nothing happens. To check out what is going on, I added an echo before the header part.

It neither throws an error or redirect to index.php. What is wrong? I have turned output buffering on/off, but nothing makes it redirect. What can I do?

<?
error_reporting(E_ALL);
echo 'This is an error';

header("Location: login.php");
die();
?>

Thanks

Industrial
  • 41,400
  • 69
  • 194
  • 289

4 Answers4

52

From PHP documentation :

header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

And in your case, you are using echo before header()

Soufiane Hassou
  • 17,257
  • 2
  • 39
  • 75
2

Do you have short tags enabled? try it with the long tag <?php:

<?php
error_reporting(E_ALL);
header("Location: login.php");
die();
?>
Community
  • 1
  • 1
jah
  • 2,056
  • 1
  • 18
  • 35
1

I reminded myself that I had xDebug installed on the actual test environment and after googling it, I found this site: http://bugs.xdebug.org/view.php?id=532

So I'll downloaded the last version of xDebug and changed the php.ini accordingly for the new file and everything works out like a charm. Headers are being sent - the redirecetion is done and errors are displayed.

Thanks everybody for your help!

Industrial
  • 41,400
  • 69
  • 194
  • 289
0

Is display_errors enabled?
@Gumbo - It sure is!

well, it sure is not. because

To check out what is going on, I added an echo before the header part.

if you'd have display_errors enabled, it would display an error, at least when you turned output buffering on.

So, first of all make sure you can see error messages.
just print out an undefined variable,

error_reporting(E_ALL);
echo $undef9306;

I am sure you won't see it. So, you have to turn displaying errors on

Next, on the server side you can use headers_sent() function to see if headers were sent. On client side use HTTP sniffer to see if anything were sent

And check your file for the BOM.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Hi. I'm afraid not. Display_errors are enabled as said, but it was the xDebug that caused this... Thanks a lot for your help Col.! – Industrial Apr 26 '10 at 15:09