Jump to content

PHP Header error

Joveice

I have this error, and I have no clue what makes this happen. Yes I googled it but that dident help me find the issue since line 126 in header is 

';
Warning: Cannot modify header information - headers already sent by (output started at D:\xampp\htdocs\header.php:126) in D:\xampp\htdocs\pages\m\create.php on line 64

So what is this? If you need more of the code I'll post it.

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

Usually this problem is caused by some warning or error message (if they're configured to output anywhere besides log files) sent to the browser before headers are sent.

Also, if you echo / print or just plain have some characters in the php script outside php tags , they may be sent by the php engine as characters to the browser before the php code has a chance to send the header data.

In the past, php caused problems because it considered the UTF-8 BOM (two invisible characters hidden at the beginning of text files) which are sometimes added by text editors and they're kind of redundant. Newer versions of php detect those two invisible characters and ignore them. 

 

So it says there in the warning.  At line 126 or thereabouts in header.php , something sent something to the output so on line 64 in create.php some code can't send or modify headers because output has already started.

You can prevent functions from printing errors or warnings  to screen by placing an @ in front of the function name (the errors or warnings would still be logged to files)

 

Another possible fix could be to call the function ob_clean()  right before sending the headers ... ob_clean should clear the output buffers so if the data wasn't actually sent already to the browser, whatever was supposed to go to the browser before sending headers will be erased. Documentation : http://us3.php.net/manual/en/function.ob-clean.php

 

Link to comment
Share on other sites

Link to post
Share on other sites

52 minutes ago, Joveice said:

I have this error, and I have no clue what makes this happen. Yes I googled it but that dident help me find the issue since line 126 in header is 


';

Warning: Cannot modify header information - headers already sent by (output started at D:\xampp\htdocs\header.php:126) in D:\xampp\htdocs\pages\m\create.php on line 64

So what is this? If you need more of the code I'll post it.

The Header should be sent before any content is displayed.

If you could post more of the code, we can take a better look. :)

Link to comment
Share on other sites

Link to post
Share on other sites

50 minutes ago, leonfagan71 said:

The Header should be sent before any content is displayed.

If you could post more of the code, we can take a better look. :)

<?php
      if (isset($_SESSION['uid'])) {
        echo '
        <div class="user-panel">
          <div class="pull-left image">
            <img src="/images/uploads/avatar/'.$_SESSION['picid'].'" class="img-circle" alt="User Image">
          </div>
          <div class="pull-left info">
            <p>'.$_SESSION['uid'].'</p>
            <a href="#"><i class="fa fa-gear text-success"></i> Account</a>
          </div>
        </div>';
      }
      ?>

Line 126 in header.php is the </div>'; part.

      if (getCountPlayersInTeam($conn, $username) < 5) {
        header("Location: /pages/m/create.php?error=lt");
        exit();
      } else {}

line 64 is the header line

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

21 minutes ago, Joveice said:

<?php
      if (isset($_SESSION['uid'])) {
        echo '
        <div class="user-panel">
          <div class="pull-left image">
            <img src="/images/uploads/avatar/'.$_SESSION['picid'].'" class="img-circle" alt="User Image">
          </div>
          <div class="pull-left info">
            <p>'.$_SESSION['uid'].'</p>
            <a href="#"><i class="fa fa-gear text-success"></i> Account</a>
          </div>
        </div>';
      }
      ?>

Line 126 in header.php is the </div>'; part.


      if (getCountPlayersInTeam($conn, $username) < 5) {
        header("Location: /pages/m/create.php?error=lt");
        exit();
      } else {}

line 64 is the header line

Okay, the header("Location:...") cannot be sent after content has been sent.

You must ensure that you're not echoing or printing any content before sending that header otherwise it will fail.

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, leonfagan71 said:

Okay, the header("Location:...") cannot be sent after content has been sent.

You must ensure that you're not echoing or printing any content before sending that header otherwise it will fail.

Yea then I'm even more confused, since there are no echo before that. the only echo that is before, is in it's own if statment and is at line 20, and this is the way I have done it last time and it has always worked.

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

You're trying to redirect the user to an error page, after content was already sent to user, that's the problem.

Go backwards, since that line 64 is only executed when the result of getCountPlayersInTeam($conn, $username) is less than 5, investigate what happens in that function.

 

If you're doing this locally on your computer, enable the php error reporting in php.ini and specify a error.log file and after running the script that shows the error, look at the bottom of error.log and you should see whatever warnings and errors show up.

 

My guess is $conn is not a valid database connection (maybe you forget to create a database connection further up in that page), or maybe the sql query you use in that function is invalid or you get no results, which triggers a warning or error when you later user  mysql_fetch_rows or something like that, which fails if the actual result set is 0.

 

Link to comment
Share on other sites

Link to post
Share on other sites

30 minutes ago, Joveice said:

Yea then I'm even more confused, since there are no echo before that. the only echo that is before, is in it's own if statment and is at line 20, and this is the way I have done it last time and it has always worked.

pop your code file on pastebin and I'll take a look and see what's up. It's hard to debug with small sections.

Cheers,

Leon.

Link to comment
Share on other sites

Link to post
Share on other sites

21 hours ago, leonfagan71 said:

pop your code file on pastebin and I'll take a look and see what's up. It's hard to debug with small sections.

Cheers,

Leon.

Which code should I post? header, the file I'm accessing or the function?

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Joveice said:

Which code should I post? header, the file I'm accessing or the function?

"code file"
If you'd prefer, you can send me the code file via PM.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×