#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Unhappy Have php automatically add content-length?


    hi

    I work on a custom webserver(long story) that doesn't add the content-length header to dynamic content by itself.

    Is there anyway to get php4 to do this automatically, via a php.ini setting, or available source code patch?

    I know I can do it manually in my scripts, but I would like it to be transparent.

    Worse case I can modify the php or webserver source myself, but am trying to ensure nothing exists for this yet.

    thank you kindly

    Tim
  2. #2
  3. Self-proclaimed Googlebot
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2005
    Location
    Edmonton, Alberta, Canada, Earth, Solar System, Milky Way Galaxy, Universe
    Posts
    652
    Rep Power
    42
    If you're reading static files like images you can read the file size before you read the file and send the header.

    Otherwise for normal PHP output, I'm pretty sure you'll need to use ob_start() at the top of your scripts and use

    PHP Code:
    header('Content-Length: ' ob_get_length()); 
    at the end of all your output.
    My Website has a few things that I haven't seen elsewhere, like a free chat with translation built in.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2008
    Posts
    7
    Rep Power
    0
    Originally Posted by ffoeg
    If you're reading static files like images you can read the file size before you read the file and send the header.

    Otherwise for normal PHP output, I'm pretty sure you'll need to use ob_start() at the top of your scripts and use

    PHP Code:
    header('Content-Length: ' ob_get_length()); 
    at the end of all your output.
    thanks for the reply, this actually won't be static content for the most part.

    And I wish to not have to modify my php scripts, I wish to modify the php source code to add the header for me.

    I'm running php as a fast cgi, so I imagine it's probably not too difficult to patch to have it add Content-Length before communicating back to the webserver, just trying to avoid extra work if someone's already done this.

    Also I wasn't sure if I could do this via php.ini I haven't seen anyway to.

    thank you
  6. #4
  7. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,904
    Rep Power
    9646
    Patch... the source? Have fun with that.


    However, you can do this, and it doesn't really require modifying any code:
    Turn on output buffering in php.ini. Then create a small PHP file, like
    PHP Code:
    <?php

    $length 
    ob_get_length();
    header("Content-Length: $length");

    ?>
    Then use the auto_append_file setting to automatically run this file at the end of every PHP script.

    Should work, I think.

    Comments on this post

    • kicken agrees : That's what I'd try.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2008
    Posts
    7
    Rep Power
    0
    thanks yes that would do the trick cleanly, though it requires output buffering, that's fine.

    However this doesn't work when exit() is called alas. I'm trying to override exit via a pre loaded php, been unsuccessful so far, but posted another thread for that.

    thanks for this !

IMN logo majestic logo threadwatch logo seochat tools logo