How the header() function works in PHP

Asked

Viewed 935 times

0

I’m using the following code:

if (!$lRet):
    // Volta para o cartão eletrônico.
    header('Location: '.$_SESSION['CFG_DIR_ROOT'].'index.php');
endif;

This way it just doesn’t work and keeps running the script, but if we include a line of code below header() it works.

if (!$lRet):
    // Volta para o cartão eletrônico.
    header('Location: '.$_SESSION['CFG_DIR_ROOT'].'index.php');
    die();
endif;

This way it works, IE, goes to index.php and does not run die(). If I replace die() [Exit() also works] with another instruction like echo or $a = 'b', etc., it also doesn’t work.

What is the magic ?


Today I will try to run on another server, this time Linux, to see if I have different functioning or some error message. Then put here the result.

  • Enable PHP error flags, you’ll probably get a message like "Headers already sent". Post here the error output message, based on it I can better guide you.

  • Thanks for your attention Fábio. Error messages are enabled. It happens that there is no error, simply when I do not put the die() line; the impression is that there is no if. It simply follows below. With die(); it follows the header() instruction but the die() instruction is never executed.

  • Good morning, don’t post answers inside the question, so use the answer button. In this case you don’t even need to post an answer, since the problem has nothing to do with linux or die or Exit and yes you didn’t turn on php errors using error_reporting(E_ALL), this answer already responds well to your problem http://answall.com/a/18812/3635

4 answers

2

The function header() basically serves to send headers HTTP.

Usually functions that change or send headers HTTP, must be called before any other data output by script.

Some of these functions are:

  • header / header_remove
  • session_start / session_regenerate_id
  • setcookie / setrawcookie

Behold:

<html>
<?php
/* This will give an error. Note the output
 * above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>

This call returns an error because something was printed before the request was made. Literally, nothing can be printed before a header have been defined, this includes the white spaces before and after the tag php, special characters, etc..

Another thing to bear in mind is that once we define a header nothing else can be executed, so the exit, die, to make sure nothing else runs after that line.


Some References:

  • 1

    Good morning @Edilson, you as a more experienced user, if you notice that the question already has answer, then prefer to signal as duplicate, this type of question has been asked a lot of times here and already has answer: http://answall.com/a/18812/3635 - See more.

  • @Guilhermenascimento the questions have different approach, but the answers resemble, in this case, the answer of this question that you referred, is also solution to this question, I must have been careless. Thanks for the warning.

  • 1

    Good morning, see the last edition before the rollback, the author himself answered (erroneously) within the question: http://answall.com/revisions/92944/3 the answer which he added there is basically what is on the link http://answall.com/a/18812/3635

  • Answered and removed, that was 3/4 this there is 4/4. Still to be duplicated, it would be good if he had the good will to remove the question.

1

Probably your request header is after some HTML, so some output has already been sent to the browser. Headers can only be sent before any HTML output.

Hence, a ob_start() will take all the output data and buffer it. The data will only be sent to the browser when the buffer is closed.

session_start(); // 1ª linha do arquivo
ob_start(); // 2ª linha do arquivo
// ...
// ...
// ...
if (!$lRet):
// Volta para o cartão eletrônico.
header('Location: '.$_SESSION['CFG_DIR_ROOT'].'index.php');
endif;
// ...
// ...
// ...
  • It would be interesting if you add, to the code, a little explanation of how it answers the question.

  • @ramaral, thanks for the comment, I will do it.

0

Try it this way:

if(!isset($lRet)){
    // Volta para o cartão eletrônico.
    header('Location: '.$_SESSION['CFG_DIR_ROOT'].'index.php');
}

Always remember to put session_start() in the first line of the code.

  • Good morning @Alissonacioli, you as a more experienced user, if you notice that the question already has answer, then prefer to signal as duplicate, this type of question.

-2


Have you tried using the ob_start at the beginning of the code to clear the buffer?

Whenever that happened to me, I used that function, that solved the problem, I hope it helps you too!

  • I agree. But the page didn’t work for me. I had to put in index.php, because it’s the beginning of my code. Because I have a static index, and I only upload the intermediary content of the page, keeping the menu at the top and the footer at the bottom. So just by ob_start and be happy the entire program.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.