PHP header location-redirect doesn't work - why?

96,608

Solution 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!

Solution 2

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()

Solution 3

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

<?php
error_reporting(E_ALL);
header("Location: login.php");
die();
?>
Share:
96,608

Related videos on Youtube

Industrial
Author by

Industrial

I just want to lie on the beach and eat hot dogs. That’s all I’ve ever wanted. Really.

Updated on July 09, 2022

Comments

  • Industrial
    Industrial almost 2 years

    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

    • Gumbo
      Gumbo about 14 years
      Is display_errors enabled?
    • timdev
      timdev about 14 years
      Does the code you've provided at least output 'This is an error'?
    • Industrial
      Industrial about 14 years
      No it didn't, but it all had to do with Xdebug breaking up the headers...
  • Industrial
    Industrial about 14 years
    I know that and thats why I have made an echo before the header()-call. It makes an error at other servers and should do this. Why doesn't anything happen?
  • Industrial
    Industrial about 14 years
    Yep, I do have short tags set enabled, and everything else works fine, except the header stuff...
  • Industrial
    Industrial about 14 years
    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.!
  • Nehal J Wani
    Nehal J Wani over 11 years
    An alternative to change headers would be to use: <?php echo "<script> window.location.replace('login.php') </script>" ?>
  • Ethan Brimhall
    Ethan Brimhall about 6 years
    Nehal J Wani ^^^ That comment did it for me thanks