CMartins
CMartins

Reputation: 3293

Is it possible to prevent headers to send again?

imagine this:

<?php
include_once('galeria.php');
?>

the galeria.php file is a complete html/php file. Is it possible to include the file and prevent headers to send again? I just don't want to use iframe or change my include files.

Upvotes: 0

Views: 129

Answers (3)

Captain Payalytic
Captain Payalytic

Reputation: 1071

You have confused everyone by using the term "headers". Headers is what is sent by the web server to tell the browser what to do with the payload. You are talking about the HEAD and I suspect HTML and BODY tags.

Well any sensible person would simply split the files up into section, but for some strange reason (you haven't told us why) you state that you do not wish to do this. In that case you can read in the galeria.php file and use either PHP's DOM class methods (preferred) or some REGEX (less good) to extract the body payload from the galeria.php file.

Can you tell us why you do not want to design this sensibly?

Upvotes: 2

Alex Shesterov
Alex Shesterov

Reputation: 27525

Headers are sent only once - right before the first bytes are sent to the browser. The bytes can be sent to the browser by (for example) echo(), print(), or by literal text/html outside the <?php ?> sections.

If your file which is including the galeria.php does not need to send any headers explicitely, then you have nothing to worry about.

Otherwise - if your file which is including the galeria.php needs to send headers explicitely, you may buffer the output of galeria.php as follows:

  • First, call ob_start() (redirects all the output to a temporary buffer),
  • Then include galeria.php (output gets sent to the buffer, not to browser. Headers are not sent yet),
  • Then send the headers as you need via header(),
  • Call ob_end_flush() to send the buffer contents to the browser (and all the default headers with it).

See http://www.php.net/manual/en/function.ob-start.php and http://php.net/manual/en/book.outcontrol.php

Upvotes: 1

Dipesh Parmar
Dipesh Parmar

Reputation: 27364

include_once

The include_once statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once.

Official Document

Upvotes: 1

Related Questions