SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question What is ob_start to?

    Hi there!
    I have seen a lot of post in other communitys about something "headers already sent!" or something. I don't know what it mean, and people say they need to set

    <?php ob_start(); ?>

    in the top of the document. What is this? Can someone tell me?
    Thanks!

  2. #2
    SitePoint Evangelist dmsuperman's Avatar
    Join Date
    Feb 2005
    Location
    A box
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It stops all things from being sent to the browser via echo or whatever then when the script ends or when ob_end_flush(); is called it outputs it. It basically lets you use echo before using header(); In fact it's good to do this, it allows you to be sure you send whole files.
    <(^.^<) \(^.^\) (^.^) (/^.^)/ (>^.^)>
    Core 2 Duo E8400 clocked @ 3.375GHz, 2x2GB 800MHz DDR2 RAM
    5x SATA drives totalling 2.5TB, 7900GS KO, 6600GT

  3. #3
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It also makes a script slower.i am not saying don't use it. i my myself use it. its just you should know the advantages and disdvantages of using a certain thing before using it.
    ---------------------------
    Errors = Improved Programming.
    My Site

  4. #4
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry, but I really don't understand what header is... can someone explain?
    Thansk, by the way .

  5. #5
    Non-Member
    Join Date
    Nov 2003
    Location
    here
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Read up on HTTP headers.

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A served page has two sections - much like an html doc - a header and a body. The header gets sent first, then the body. Once the headers are sent - which happens as soon as any html content gets sent (or php output that is sent to the browser) you cannot add anything else to them unless you use ob_start to buffer the output. By using output buffering, you can adjust the headers (and other parts of your script) at a time that would otherwise not be available to you to do so.

  7. #7
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    suppose you want to use session in your script and you want to include some file in your page and you do it this way

    WRONG WAY
    PHP Code:
    <?
     $page
    'test';
     switch(
    $page)
     {
      case 
    'test':
        include(
    'test.php');
       break;
     }
     
    session_start();
    ?>
    RIGHT WAY
    PHP Code:
    <?
     $page
    'test';
     
    ob_start();
     switch(
    $page)
     {
      case 
    'test':
        include(
    'test.php');
       break;
     }
     
    $contents=ob_get_contents();
    ob_clean();
     
    session_start();
    echo 
    $contents;
    ?>
    i know not good example.

    in this case to use session_start(), you SHOULD NOT SEND ANY output to browser before this function. its called sending headers,which server some purpose (in this case it start a session).

    in first example, you include that filea and then send headers,which is not possible and it will complain about it.

    in second, itwill includeand then storethe results in $contents and then you send headers and then echo it and it will work.

    hope thi shelps
    ---------------------------
    Errors = Improved Programming.
    My Site

  8. #8
    SitePoint Evangelist dmsuperman's Avatar
    Join Date
    Feb 2005
    Location
    A box
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jaswinder_rana
    It also makes a script slower.i am not saying don't use it. i my myself use it. its just you should know the advantages and disdvantages of using a certain thing before using it.
    No, it doesn't. It just outputs the whole thing at once rather than in parts, either way it's the same load speed on the server, and overall load time for the user is the same as well. I have no idea why it's not built in to PHP already. There was a thread about this a while ago, everyone else agreed it is great, and there are no downsides to it.
    <(^.^<) \(^.^\) (^.^) (/^.^)/ (>^.^)>
    Core 2 Duo E8400 clocked @ 3.375GHz, 2x2GB 800MHz DDR2 RAM
    5x SATA drives totalling 2.5TB, 7900GS KO, 6600GT

  9. #9
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I'm starting to understand. Thank you guys.
    But... I had an error once. It was something with session_start(); headers crap etc. etc. A waring! I just put a @ in front of it. Like @session_start();.
    So... I could have used the ob_start(); instead, or something?

  10. #10
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But... I had an error once. It was something with session_start(); headers crap etc. etc. A waring! I just put a @ in front of it.
    the @ sign suppresses errors, it does not correct them

    using @ is a workaround for your problem, ob_start() is a solution


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
  •