SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Jul 2010
    Location
    Mumbai, Maharashtra, India
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Not getting error for headers

    Hi,
    I'm new to PHP. Currently I'm learning on output buffering. Everyone says that there would an error if there is an output before header() function call. This is same with session() function. Currently I'm not getting this error (Cannot modify header information...) while practicing on localhost. I'm not sure what is problem. I've not changed my 'php.ini'. I'm using PHP 5.4.3 and Appache 2.2.22. I want to use buffer functions to buffer my output before I can send it to browser. For practice I want to see this error and then remove this error using output buffering.

    Can anyone please let me know how I would get this header errors? The example that I've used is available in php.net. That is:
    PHP Code:
    <html>
    <?php
    /* This will give an error. Note the output
     * above, which is before the header() call */
    header('Location: http://www.google.com/');
    ?>
    Thanks,

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,388
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Sounds like you have output buffering turned on in your php.ini file (http://www.php.net/manual/en/outcont...tput-buffering).

  3. #3
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,806
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by sanjayabc1234 View Post
    Hi,
    I'm new to PHP. Currently I'm learning on output buffering. Everyone says that there would an error if there is an output before header() function call. This is same with session() function. Currently I'm not getting this error (Cannot modify header information...) while practicing on localhost. I'm not sure what is problem. I've not changed my 'php.ini'. I'm using PHP 5.4.3 and Appache 2.2.22. I want to use buffer functions to buffer my output before I can send it to browser. For practice I want to see this error and then remove this error using output buffering.

    Can anyone please let me know how I would get this header errors? The example that I've used is available in php.net. That is:
    PHP Code:
    <html>
    <?php
    /* This will give an error. Note the output
     * above, which is before the header() call */
    header('Location: http://www.google.com/');
    ?>
    Thanks,
    That is weird.

    I checked the PHP Manual and also forced error_reporting(-1); and ini_set('display_errors',1); and the elusive Cannot modify header information... never appeared ?

    I reckon this is a job for @Salathe ; or another PHP Guru.

  4. #4
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I've solved the mystery! What you get is a warning:

    PHP Warning: Cannot modify header information - headers already sent by (output started at...
    warnings don't kill the script and if you are using a production server that does not display warnings you don't see it. I got the quoted message from my php error log.
    Denny Schlesinger
    web services

  5. #5
    SitePoint Member
    Join Date
    Jul 2010
    Location
    Mumbai, Maharashtra, India
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    Sounds like you have output buffering turned on in your php.ini file (http://www.php.net/manual/en/outcont...tput-buffering).
    Hi,
    Output buffering is off in php.ini.

  6. #6
    SitePoint Member
    Join Date
    Jul 2010
    Location
    Mumbai, Maharashtra, India
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by captainccs View Post
    I've solved the mystery! What you get is a warning:



    warnings don't kill the script and if you are using a production server that does not display warnings you don't see it. I got the quoted message from my php error log.


    Hi,
    I've checked php error log page. There is no such warning or error. I'm using default php.ini.

  7. #7
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by John_Betong View Post
    That is weird.

    I checked the PHP Manual and also forced error_reporting(-1); and ini_set('display_errors',1); and the elusive Cannot modify header information... never appeared ?

    I reckon this is a job for @Salathe ; or another PHP Guru.
    You probably have output_buffering turned on, with the <html> text not being long enough to cause the buffer to be flushed. Also try with a different header (e.g. Content-Type: text/plain) since the warning might be being printed but not seen due to the redirect.

    Quote Originally Posted by sanjayabc1234 View Post
    Hi,
    I've checked php error log page. There is no such warning or error. I'm using default php.ini.
    Could you show us a phpinfo() page? The term "default php.ini" doesn't really help anyone, there are many defaults.
    Salathe
    Software Developer and PHP Manual Author.

  8. #8
    SitePoint Member
    Join Date
    Jul 2010
    Location
    Mumbai, Maharashtra, India
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face

    Quote Originally Posted by Salathe View Post
    You probably have output_buffering turned on, with the <html> text not being long enough to cause the buffer to be flushed. Also try with a different header (e.g. Content-Type: text/plain) since the warning might be being printed but not seen due to the redirect.



    Could you show us a phpinfo() page? The term "default php.ini" doesn't really help anyone, there are many defaults.
    Hi,
    I'm sorry to everyone. I saw phpinfo page now and found that output_buffering is turned on. I turned it off and checked my code. It is working fine. Initially I was looking at php.ini file. I got confused with two declarations in php.ini for output_buffering. At first place it is turned off and at after few lines it is turned on. Those declarations are:

    //at one location lines are

    ; output_buffering
    ; Default Value: Off
    ; Development Value: 4096
    ; Production Value: 4096

    // at another location in php.ini the lines are

    ; Output buffering is a mechanism for controlling how much output data
    ; (excluding headers and cookies) PHP should keep internally before pushing that
    ; data to the client. If your application's output exceeds this setting, PHP
    ; will send that data in chunks of roughly the size you specify.
    ; Turning on this setting and managing its maximum buffer size can yield some
    ; interesting side-effects depending on your application and web server.
    ; You may be able to send headers and cookies after you've already sent output
    ; through print or echo. You also may see performance benefits if your server is
    ; emitting less packets due to buffered output versus PHP streaming the output
    ; as it gets it. On production servers, 4096 bytes is a good setting for performance
    ; reasons.
    ; Note: Output buffering can also be controlled via Output Buffering Control
    ; functions.
    ; Possible Values:
    ; On = Enabled and buffer is unlimited. (Use with caution)
    ; Off = Disabled
    ; Integer = Enables the buffer and sets its maximum size in bytes.
    ; Note: This directive is hardcoded to Off for the CLI SAPI
    ; Default Value: Off
    ; Development Value: 4096
    ; Production Value: 4096
    ; http://php.net/output-buffering
    output_buffering = On

    After looking at phpinfo I came to know output_buffering is turned on. Issue is resolved now. Sorry for bothering on this. I think in earlier version it was turned off by default and in current versions (PHP 5.4.3) it is turned on by default.

    Thanks,

  9. #9
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sanjayabc1234 View Post
    After looking at phpinfo I came to know output_buffering is turned on. Issue is resolved now. Sorry for bothering on this. I think in earlier version it was turned off by default and in current versions (PHP 5.4.3) it is turned on by default.
    No need to apologise, it is a common enough issue (having different INI settings that what you think are being used).

    Off Topic:

    An aside about "default" values.

    The "default" value (used when no INI file overrides the setting) has always been Off. This is what we mean in the manual when we say "default" value. Up until PHP 5.3.0 when we used to ship "dist" and "recommended" sample php.ini files, the values were Off and 4096 respectively. As of PHP 5.3.0, we ship "development" and "production" sample php.ini files, both having value of 4096. Of course, those are only sample files and the actual php.ini that gets installed (if any) also depends on your method of installation; e.g. many Linux distributions use their own customised php.ini when installing PHP.
    Salathe
    Software Developer and PHP Manual Author.


Tags for this Thread

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
  •