0

When I use tabs to indent my HTML/PHP code in the file to make it more readable and nice formatted then I encounter strange issue of "Cannot modify header information - headers already sent" in PHP. When I undo indentation then everything seems to be OK. Any idea how's that? Here's my small snippet of code for which I am facing this strange issue:

<ul>
    <li><a href="<?php echo BASE_URL; ?>" <?php if(basename($_SERVER['PHP_SELF']) == "index.php") echo "class='active'"; ?>>Home</a></li>
    <li><a href="posts" <?php if(get_page_title() == "Posts") echo "class='active'"; ?>>Blog</a></li>
    <li><a href="gallery" <?php if(get_page_title() == "Gallery") echo "class='active'"; ?>>Gallery</a></li>
</ul>

EDIT

My problem is due to indentation which is allowed in source code for well formatting. Why this question is put on duplicate?

When I put my code in following way:

     <ul>
<li><a href="<?php echo BASE_URL; ?>" <?php if(basename($_SERVER['PHP_SELF']) == "index.php") echo "class='active'"; ?>>Home</a></li>
<li><a href="posts" <?php if(get_page_title() == "Posts") echo "class='active'"; ?>>Blog</a></li>
<li><a href="gallery" <?php if(get_page_title() == "Gallery") echo "class='active'"; ?>>Gallery</a></li>
<li><a href="contact" <?php if($title == "Contact") echo "class='active'"; ?>>Contact</a></li>
    </ul>

The it does work, but when I format above code in following style:

<ul>
    <li><a href="<?php echo BASE_URL; ?>" <?php if(basename($_SERVER['PHP_SELF']) == "index.php") echo "class='active'"; ?>>Home</a></li>
    <li><a href="posts" <?php if(get_page_title() == "Posts") echo "class='active'"; ?>>Blog</a></li>
    <li><a href="gallery" <?php if(get_page_title() == "Gallery") echo "class='active'"; ?>>Gallery</a></li>
    <li><a href="contact" <?php if($title == "Contact") echo "class='active'"; ?>>Contact</a></li>
</ul>

Then it stops working. Strange!!! isn't it?

enter image description here

enter image description here

On line number 8 in functions.php file, I have a function which has following:

header("Location: " . $url);

// Page redirection
function redirect_to($url)
{
    if(isset($url) && $url != "")
    {
        header("Location: " . $url);
        exit();
    }
}

NOTE

Note that if I remove my navigation code then error is automatically gone away.

Sachin
  • 1,646
  • 3
  • 22
  • 59
  • There must be something missing. There's nothing in that block of code that edits any type of headers – IsThisJavascript Jul 24 '17 at 12:49
  • No, that's the code which causes issue. – Sachin Jul 24 '17 at 13:00
  • Maybe after you indented some server magic happened which made the error display. If you view the page source can you still see the error? – IsThisJavascript Jul 24 '17 at 13:02
  • Actually I am redirecting contact page to itself after form submission. But when I indent code like the way I above explained then I get the error. If I don't indent then no error. No when I view page source then I can not see the error message because page was not redirected without error. – Sachin Jul 24 '17 at 13:04
  • Look into the method on how you are redirecting to itself after form submission. Best way would be to utilize the
    elements rather than sending a header(). As I said previously, there's nothing in that block of code which should trigger the error. Unless `get_page_title()` is doing some header manipulation
    – IsThisJavascript Jul 24 '17 at 13:07
  • I posted two important pics in regards to my question which may help you to find out the bug, plz see. Don't worry about get_page_title(). Its working correctly. – Sachin Jul 24 '17 at 13:11
  • The error says line 8 in functions.php. What is the function on line 8, could you post this. – IsThisJavascript Jul 24 '17 at 13:14
  • I posted about line 8 – Sachin Jul 24 '17 at 13:18
  • Well, that's you're issue. You're not allowed to send a header("Location") request after headers have been sent (you are outputting html, so headers have been sent). Either put ob_start(); at the top of your page or change your logic. – IsThisJavascript Jul 24 '17 at 13:20
  • I know about ob_start() but I don't want to use it. Main thing is that I yet don't know what html output is being printed there. I can not see it. How to resolve it without enabling output buffering? You said - `(you are outputting html, so headers have been sent` but where? – Sachin Jul 24 '17 at 13:22
  • Can you post the function that header() is being called from? – IsThisJavascript Jul 24 '17 at 13:23
  • Posted, please check it. – Sachin Jul 24 '17 at 13:26
  • You're calling `redirect_to()` after displaying HTML. Find where you are doing that and change your logic. – IsThisJavascript Jul 24 '17 at 13:37
  • Can you please give me your skype id so that I can share code with you? – Sachin Jul 24 '17 at 13:41
  • No sorry, just rework your logic. If you must echo out some html before any redirect_to calls, then store the html as a variable and echo it out later. – IsThisJavascript Jul 24 '17 at 13:52

0 Answers0