<span><span><span class="hljs-meta"><?php</span></span><span>
<span class="hljs-comment">/*
* This example is for demonstration only, the first part is not related to the article.
* You can place any PHP initialization logic here.
*/</span>
<p></span>$start_time = microtime(true); // Record script start time</p>
<p>// Simulate some unrelated code<br>
function dummy_log($message) {<br>
file_put_contents('dummy.log', date('Y-m-d H:i:s') . ' - ' . $message . PHP_EOL, FILE_APPEND);<br>
}</p>
<p>dummy_log("Visited the article page");</p>
<p>?><br>
<hr><br>
<?php<br>
/**</p>
<ul>
<li>
<p>How to Use ob_get_contents() to Capture the Full Page Content Before PHP Redirect? Detailed Guide</p>
</li>
<li></li>
<li>
<p>In PHP development, sometimes we need to capture the full page output before executing</p>
</li>
<li>
<p>header('Location: ...') for redirection. This is useful when we want to save or analyze the content.</p>
</li>
<li>
<p>In such cases, output buffering functions like ob_get_contents() come in handy.</p>
</li>
<li></li>
<li>
<ol>
<li>
<p>Why use output buffering?</p>
</li>
</ol>
</li>
<li>
<hr>
</li>
<li>
<p>Normally, PHP outputs content directly to the browser. Once sent, PHP can no longer modify</p>
</li>
<li>
<p>the headers, which may cause a “headers already sent” error when calling header() to redirect.</p>
</li>
<li>
<p>With Output Buffering, PHP stores the output in memory first, allowing us to both read it</p>
</li>
<li>
<p>and modify it before sending it to the browser.</p>
</li>
<li></li>
<li>
<ol start="2">
<li>
<p>The role of ob_get_contents()</p>
</li>
</ol>
</li>
<li>
<hr>
</li>
<li>
<p>ob_get_contents() retrieves the content currently stored in the output buffer.</p>
</li>
<li>
<p>Note: You must first enable buffering with ob_start().</p>
</li>
<li></li>
<li>
<ol start="3">
<li>
<p>Steps to implement</p>
</li>
</ol>
</li>
<li>
<hr>
</li>
<li>
<ol>
<li>
<p>Call ob_start() at the beginning of the page to enable buffering.</p>
</li>
</ol>
</li>
<li>
<ol start="2">
<li>
<p>Write your page content; it will be stored in the buffer instead of being sent immediately.</p>
</li>
</ol>
</li>
<li>
<ol start="3">
<li>
<p>Use ob_get_contents() to get the buffered content.</p>
</li>
</ol>
</li>
<li>
<ol start="4">
<li>
<p>Save the captured content to a file, database, or process it as needed.</p>
</li>
</ol>
</li>
<li>
<ol start="5">
<li>
<p>Finally, call either ob_end_clean() or ob_end_flush():</p>
</li>
</ol>
</li>
<li>
<ul>
<li>
<p>ob_end_clean(): Clears the buffer without sending it to the browser.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>ob_end_flush(): Sends the buffer content to the browser.</p>
</li>
</ul>
</li>
<li>
<ol start="6">
<li>
<p>After clearing the buffer, execute header('Location: ...') for redirection.</p>
</li>
</ol>
</li>
<li></li>
<li>
<ol start="4">
<li>
<p>Example code</p>
</li>
</ol>
</li>
<li>
<hr>
</li>
<li>
<p>The following example shows how to capture the page content before redirecting and save it to a file:<br>
*/</p>
</li>
</ul>
<p>ob_start(); // 1. Start output buffering</p>
<p>// 2. Simulate page output<br>
echo "<h1>Welcome to our site</h1>";<br>
echo "<p>This is the main page content.</p>";</p>
<p>// 3. Retrieve buffer content<br>
$pageContent = ob_get_contents();</p>
<p>// 4. Save content to file<br>
file_put_contents('page_backup.html', $pageContent);</p>
<p>// 5. Clear the buffer<br>
ob_end_clean();</p>
<p>// 6. Perform redirect<br>
header("Location: <a rel="noopener" target="_new" class="decorated-link" href="https://www.example.com"</span></span><span">https://www.example.com"</span></span><span<svg width="20" height="20" viewBox="0 0 20 20" fill="currentColor" xmlns="http://www.w3.org/2000/svg" data-rtl-flip="" class="block h-[0.75em] w-[0.75em] stroke-current stroke-[0.75]"><path d="M14.3349 13.3301V6.60645L5.47065 15.4707C5.21095 15.7304 4.78895 15.7304 4.52925 15.4707C4.26955 15.211 4.26955 14.789 4.52925 14.5293L13.3935 5.66504H6.66011C6.29284 5.66504 5.99507 5.36727 5.99507 5C5.99507 4.63273 6.29284 4.33496 6.66011 4.33496H14.9999L15.1337 4.34863C15.4369 4.41057 15.665 4.67857 15.665 5V13.3301C15.6649 13.6973 15.3672 13.9951 14.9999 13.9951C14.6327 13.9951 14.335 13.6973 14.3349 13.3301Z"></path></svg></a>>);<br>
exit;</p>
<p>/**</p>
<ul data-is-only-node="" data-is-last-node="">
<li>
<p>5. Important notes</p>
</li>
<li>
<hr>
</li>
<li>
<ul>
<li>
<p>ob_start() must be called before any actual output; otherwise, the buffer cannot capture content.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>If there is already output before header() without buffering, an error will occur.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>The saved content only includes the PHP script output, not external resources (CSS, JS, images).</p>
</li>
</ul>
</li>
<li></li>
<li>
<p>Summary:</p>
</li>
<li>
<hr>
</li>
<li>
<p>ob_get_contents() is a practical tool to capture output before a PHP redirect.</p>
</li>
<li>
<p>Together with ob_start() and ob_end_clean(), it allows you to capture the complete content</p>
</li>
<li data-is-last-node="">
<p data-is-last-node="">without affecting HTTP headers—perfect for logging, debugging, or caching scenarios.<br>
*/<br>
?><br>