Pergunta

Our website has been up for a couple of years and something has changed that's really strange. I've tried talking to the hosting but nothing gets resolved.

The issue is that the everything before the first<?php tag in the first included file gets output directly before going into the OB

For example:

<?php
ob_start();
?>
<h1>Welcome</h1>
<?php 
include('contentbox1.php'); 
include('contentbox2.php'); 
include('contentbox3.php'); 
?>
<b>Bye</b>
<?php
$content = ob_get_contents();
ob_end_clean();
echo("Included{".$content."}");
?>

What gets output is this: (Notice that <h1>Welcome</h1> is not with the rest of the content that was output inside the buffer)

<h1>Welcome</h1>
Included{
<div id="contentbox1"></div>
<div id="contentbox2"></div>
<div id="contentbox3"></div>
<b>Bye</b>
}

What the correct output should be (and is on my local machine):

Included{
<h1>Welcome</h1>
<div id="contentbox1"></div>
<div id="contentbox2"></div>
<div id="contentbox3"></div>
<b>Bye</b>
}

It's causing major issues to our site and I can't figure out why. It works perfectly on my local machine, just not the hosting that we've been with for years.

SOLUTION: It turns out that while I was reuploading the test.php, the boxes of content and many other files on my website had gone funny and were possibly corrupted. Once I re-uploaded my entire site (even though no code changes had occured), my site which was running slowly is now super fast and all issues are gone. I've had my files corrupted with other hosts too so keep this in mind if you have weird stuff too in the future.

Foi útil?

Solução

Your code is correctly executed because this is how ob_get_clean works:

ob_get_clean()

ob_get_clean — Get current buffer contents and delete current output buffer

Returns the contents of the output buffer and end output buffering. If output buffering isn't active then FALSE is returned.

Use ob_get_contents();

ob_start();
include('test.php');
$content = ob_get_contents();
ob_end_clean();
echo("Included{".$content."}");

Update:

<?php
ob_start();
?>
<h1>Welcome</h1>
<?php 
include('contentbox1.php'); 
include('contentbox2.php'); 
include('contentbox3.php'); 
?>
<b>Bye</b>
<?php
$content = ob_get_contents();
ob_end_clean();
echo("Included{".$content."}");
?>

Outras dicas

Try the following:

add this to the beginning of test.php

<?php include("empty_file.php"); ?>

content of empty_file.php:

<?php
echo "";
?>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top