SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    wont modify headers error

    hi,

    I've been trying to build a logg in system using sessions, cookies and php.
    When I try to load the page I get this error:

    Warning: Cannot modify header information - headers already sent by (output started at D:\inetpub\vhosts\cat-community.com\httpdocs\includes\functions.php:164) in D:\inetpub\vhosts\cat-community.com\httpdocs\includes\functions.php on line 125

    I have commented to show which is line 125 in the functions.php below.

    The staff page script is as follows:
    PHP Code:
    <?php require_once("includes/session.php");?>
    <?php 
    require_once("includes/connection.php");?>
    <?php 
    require_once("includes/functions.php");?>
    <?php confirm_logged_in
    ();?>
    <?php 
    include("includes/header.php");?>    
    <div id="menu"></div>   
    <div id="mainarea">
    <div id="contentind"><div id="contentbox"><h2><b><u>Staff Area</u></b></h2>
    <h4><b>Welcome To the Staff Area, <?php echo $_SESSION['username'];?> !</b></h4>
    <a href="content.php">Manage Content</a><br/>
    <a href="register.php">Add New User</a><br/>
    <a  href="logout.php">log out</a><br/>  
    </div>
    The connection script is as follows:

    PHP Code:
    <?php 
    $db 
    =  mysqli_connect("91.186.30.8""cass27""*******""catphp");
    if (!
    $db){
    die(
    "Database Failed:" .mysqli_error($db));
    }
    ?>
    The session script is as follows:

    PHP Code:
    <?php
        session_start
    ();
        
        function 
    logged_in(){
        return isset(
    $_SESSION['user_id']);
         }
        
        function 
    confirm_logged_in(){
        if (!
    logged_in()){
    redirect_to("logon.php");
    }
    }    
    ?>
    The redirect_to function in the function file is as follows:
    PHP Code:
      function redirect_to($location NULL){
     if(
    $location != NULL){
     
    header("Location:{$location}");// this is line 125
     
    exit;
     }
     } 
    There is no php in the header.

    the actual site is at www.cat-community.com/staff.php

    Can anyone see where i may be going wrong here?

    thanks for your help

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You have whitespace in one of your scripts which is being sent the browser, if you want to use headers not output must be sent prior to the call.

    It's quite hard to see which one (or more) has the rogue whitespace, just walk through your scripts and make sure you have nothing outside of your PHP tags.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Member
    Join Date
    Jul 2004
    Location
    us
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    You have whitespace in one of your scripts which is being sent the browser, if you want to use headers not output must be sent prior to the call.

    It's quite hard to see which one (or more) has the rogue whitespace, just walk through your scripts and make sure you have nothing outside of your PHP tags.
    You could also use output buffering have PHP buffer the HTTP response.
    The problem you have is that the HTTP body is being sent, thus you cannot send any more HTTP headers (as cookies are HTTP headers, and whitespace goes in the HTTP body).

    At the beginning of your file add:

    ob_start();

    That will buffer all output, including those from the included files, so you don't have to dig through each one trying to remove whitespace.

    Another possiblity is a BOM: http://en.wikipedia.org/wiki/Byte-order_mark
    it could be sent to HTTP without you seeing it. I think only windows uses BOM.

  4. #4
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SilverBulletUK said:

    You have whitespace in one of your scripts which is being sent the browser, if you want to use headers not output must be sent prior to the call.
    sorry to sound really stupid. but I am not sure what you mean by "Whitespace". I thought whitespace wasnt a problem with php?
    Are you saying there should be no whitespace after ?> and the next element?

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    output started at D:\inetpub\vhosts\cat-community.com\httpdocs\includes\functions.php:164

    line 164 is your problem.

    You can also do a web search for this error and get probably a hundred thousand results.

  6. #6
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cass27 View Post
    Are you saying there should be no whitespace after ?> and the next element?
    Yes. And line 164 of includes\functions.php is the problem. Likely that's the end of the file. If so, you can just remove the ?>
    - Robert

  7. #7
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    output started at D:\inetpub\vhosts\cat-community.com\httpdocs\includes\functions.php:164

    line 164 is your problem.

    You can also do a web search for this error and get probably a hundred thousand results.
    There was one result and guess whos it was??

    QMonkey said:

    Yes. And line 164 of includes\functions.php is the problem. Likely that's the end of the file. If so, you can just remove the ?>
    This seems to have got rid of that part of the problem. Thanks.

    However. Im really new to this output buffering stuff. I have been googling it to see if I can work it out. I have found out I can turn it on in my php.ini file.
    The php.ini file on my computer looks like this:

    output_buffering = 4096 So I take it it is on already.

    Bucabay said:

    At the beginning of your file add:

    ob_start();
    I did this on one file that was causing the problem.

    I just added:

    ob_start();

    The error I get now is:

    ob_start();
    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at D:\inetpub\vhosts\cat-community.com\httpdocs\staff.php:2) in D:\inetpub\vhosts\cat-community.com\httpdocs\includes\session.php on line 2

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at D:\inetpub\vhosts\cat-community.com\httpdocs\staff.php:2) in D:\inetpub\vhosts\cat-community.com\httpdocs\includes\session.php on line 2

    Warning: Cannot modify header information - headers already sent by (output started at D:\inetpub\vhosts\cat-community.com\httpdocs\staff.php:2) in D:\inetpub\vhosts\cat-community.com\httpdocs\includes\functions.php on line 125

    Now it seems to conflict with the session start.

    Anybody got any ideas

  8. #8
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    Is there no way out of this? Anybody got any ideas?

    thank you for your time?

  9. #9
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    All you need to do is make sure you aren't outputting anything before you send header() or session_start() or setcookie() - anything that outputs or modifies the headers. You shouldn't need output buffering if you do that right.

  10. #10
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By closing and opening PHP tags for no reason you've introduced a less than obvious problem.

    PHP Code:
    <?php require_once("includes/session.php");?> 
    <?php require_once("includes/connection.php");?> 
    <?php require_once("includes/functions.php");?> 
    <?php confirm_logged_in();?> 
    <?php include("includes/header.php");?>     
    <div id="menu"></div>    
    <div id="mainarea"> 
    <div id="contentind"><div id="contentbox"><h2><b><u>Staff Area</u></b></h2> 
    <h4><b>Welcome To the Staff Area, <?php echo $_SESSION['username'];?> !</b></h4> 
    <a href="content.php">Manage Content</a><br/> 
    <a href="register.php">Add New User</a><br/> 
    <a  href="logout.php">log out</a><br/>   
    </div>
    Change it to

    PHP Code:
    <?php require_once("includes/session.php");
    require_once(
    "includes/connection.php");
    require_once(
    "includes/functions.php");
    confirm_logged_in();
    include(
    "includes/header.php"); ?>
    <div id="menu"></div>    
    <div id="mainarea"> 
    <div id="contentind"><div id="contentbox"><h2><b><u>Staff Area</u></b></h2> 
    <h4><b>Welcome To the Staff Area, <?php echo $_SESSION['username'];?> !</b></h4> 
    <a href="content.php">Manage Content</a><br/> 
    <a href="register.php">Add New User</a><br/> 
    <a  href="logout.php">log out</a><br/>   
    </div>
    When you close the php tag and start a new line, you're doing just that, sending a new line to the browser. The moment you send anything to the browser you cannot modify the headers anymore, that is why you're getting the error.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  11. #11
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Brian Hanson said:

    When you close the php tag and start a new line, you're doing just that, sending a new line to the browser. The moment you send anything to the browser you cannot modify the headers anymore, that is why you're getting the error.
    I have tried this and it seemed to get rid of part of the problem. By commenting out some of the script I found the offending area seems to be around confirm_logged_in(). By taking out this function at least I can log in and out and go to the staff area. However if I leave it in, the error message is:

    Warning: Cannot modify header information - headers already sent by (output started at D:\inetpub\vhosts\cat-community.com\httpdocs\includes\header.php:6) in D:\inetpub\vhosts\cat-community.com\httpdocs\includes\functions.php on line 125

    I have tried deleting line 6 but the error then becomes line 7 and so on.

    Stormrider said:

    All you need to do is make sure you aren't outputting anything before you send header() or session_start() or setcookie() - anything that outputs or modifies the headers. You shouldn't need output buffering if you do that right.
    In my php ini file on apache output buffering is on. But I am putting my site up to my hosts remote server so (I only use apache for testing) does it matter what the php.ini is set to? Also looking at my files there is nothing output before session and so Im at a loss.

    The only thing the redirect_to function does within the confirm_log_in function is redirect to the "logon.php" page. Could this be causing a problem

    any ideas would be very appreciated

  12. #12
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cass27 View Post
    Is there no way out of this? Anybody got any ideas?

    thank you for your time?
    Are you using dreamweaver ? I know a dreamweaver feature could cause this, you need to turn it off so output buffering will be able to be sent.

    If still not work, check for whitespace after ?> or before <?php

    Or else just use the current solution.

  13. #13
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    runrunforest said:

    Are you using dreamweaver ? I know a dreamweaver feature could cause this, you need to turn it off so output buffering will be able to be sent.
    I do use dreamweaver. Any idea what this feature is and how I turn it off?

  14. #14
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just try once putting the function ob_start() as the very first line (before you start the session session_start()) in your session file includes/session.php.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  15. #15
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bhanson View Post
    By closing and opening PHP tags for no reason you've introduced a less than obvious problem.

    When you close the php tag and start a new line, you're doing just that, sending a new line to the browser. The moment you send anything to the browser you cannot modify the headers anymore, that is why you're getting the error.
    Actually, when closing a php tag, the new line following it is automatically removed. As long as there are no blank lines between each <?php ?> line, it shouldn't be a problem.

  16. #16
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stormrider View Post
    Actually, when closing a php tag, the new line following it is automatically removed. As long as there are no blank lines between each <?php ?> line, it shouldn't be a problem.
    It appears you're right, I haven't tested heavily in the area as I basically never close and then reopen a PHP tag immediately.

    In any case, where possible it's better to just avoid the situation, and then you don't have to worry about it.

    His code in particular I think had extra spaces in between the tags.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  17. #17
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Thanks everyone who has taken time to offer thier ideas about this mess ive been in. Output buffering was a whole new ball game. Still trying to get my head round it but I know a lot more from the posts that have been put up.

    ...and frustratingly the problem seems to have righted itself and I dont know what I did. I took all the spaces out of the function page. I think this was causing me problems. I have also added ob_start() again (it didn't work before but it appears to now.(?)

    Anyhow next time I will be much more carefull about spaces around <? and not putting in too many.

    Cheers for your help. Sitepoint rules ok?

  18. #18
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cass27 View Post

    I do use dreamweaver. Any idea what this feature is and how I turn it off?
    You've got the problem solved, good. Just in case next time you cant solve it anymore, try this.

    Open your problem file or included file in dreamweaver.

    Press ctrl + J, page property dialog box will come up

    On the left panel, choose Title/Encoding.

    Now on the right hand side, look for Include Unicode Signature (BOM).

    If it is checked, you have to uncheck it.

    Done.

  19. #19
    SitePoint Member lordsbaby2007's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am having the same problem with a php file. It started all the sudden and I am like this person it is frustrating trying to find the issues behind it. After over 1 year it happens to 6 of my sites. weird


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •