SitePoint Sponsor

User Tag List

Results 1 to 20 of 20
  1. #1
    SitePoint Member
    Join Date
    Aug 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cannot modify header information

    Hi guys,

    Im having trouble with the conn.php file "session_start();" and keep having this:

    "Warning: Cannot modify header information - headers already sent by (output started at /home/humor/public_html/joke/conn.php:1)"

    It keep telling me that headers already sent but i have no idea where is wrong.

    Hope you could help me on these problem.

    Thanks
    Cecilia
    925silverjewelry - Jewelry Accessories and more...
    FREE Gift Boxes

  2. #2
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm having the very same problem.

    Now others say to look for extra spaces after the ?>

    Try that, thats not what my problem is but it might be yours.

    Also try
    ob_start();
    right before the include ("xxx.php");

    Take a look at this http://us3.php.net/header

    For me nothing seems to work I can even do this
    PHP Code:
    <html>
    <head>
    <?php 
    ob_start
    ();
    include (
    "test2.php");
    ob_end_flush();
    ?>
    </head>
    <body> </body>
    </html>
    PHP Code:
    <?php header("location:test.php"); ?>
    And it still says it.
    Last edited by Serbaside; Dec 3, 2005 at 23:39.

  3. #3
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Serbaside
    PHP Code:
    <html>
    <head>
    <?php 
    ob_start
    ();
    include (
    "test2.php");
    ob_end_flush();
    ?>
    </head>
    <body> </body>
    </html>
    Sigh... Try reading the error message... It says output started... Don't you think this is output: <html><head>... Either write your script in the correct order or start buffer before sending output.

    Personally I find what you are doing bizarre. Why are you already building a page if there is a possibility of a redirection. Seems like a waste of effort to me.

  4. #4
    SitePoint Member
    Join Date
    Aug 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Serbaside,

    Thank you very much. The ob_start(); work!
    925silverjewelry - Jewelry Accessories and more...
    FREE Gift Boxes

  5. #5
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    This whole ob_start() solution to this common problem really winds me up. Something is wrong with your code structure/logic if you want to display something to the user and then immediately redirect them before they can even see the output. So many people now think ob_start() is the answer to the problem when in fact it isn't, it's just hiding the problem. If you intend to redirect someone then don't output anything (as the error message states). Sorry for the rant but this ob_start() thing really grinds my gears as Peter Griffin would say.

  6. #6
    SitePoint Member
    Join Date
    Aug 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, i don't get what you mean..could you please advise?

    Pardon me that ive no knowledge on php stuffs.
    925silverjewelry - Jewelry Accessories and more...
    FREE Gift Boxes

  7. #7
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is very simple. Write your script in the correct linear order. Redirects first (followed by exit), then other headers, then the script body. If you need ob_start your script is not written in the proper linear order.

  8. #8
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bokehman
    Sigh... Try reading the error message... It says output started... Don't you think this is output: <html><head>... Either write your script in the correct order or start buffer before sending output.

    Personally I find what you are doing bizarre. Why are you already building a page if there is a possibility of a redirection. Seems like a waste of effort to me.
    Is this what you are saying?

    PHP Code:
    <?php include ("test2.php"); ?>
    <html>
    <head>
    </head>
    <body>
    </body>
    </html>
    PHP Code:
    <?php 
    header
    ("location:test.php"); 
    exit();
    ?>
    Because that still doesn't work. A alert message pops up and the page never loads. If you would like to see a link is below.

    http://www.namelesscreations.com/new/test.php

    Some how it gets caught in a infinite loop

    I am including the file so that I can grab certain variables from it.

  9. #9
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    test.php is including test2.php which in turn redirects back to test.php ... hence the infinite loop.

  10. #10
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So what structure do I need to use to prevent this and the header error?

  11. #11
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are intending to redirect someone using a header("Location ... then there's no point generating any content for them as they won't get to see it. If you want users to see content and then be redirected a meta-refresh is usually more appropriate.

    A common structure for say a form that submits back to itself is:
    Code:
    if the form has been submitted:
        process the form here
        if all is well then redirect to maybe a confirmation/success page
    the form and ALL output (html etc) goes here
    There is no output (html, whitespace before the <?php tag) at all at the top of the page (or anywhere before there for that matter), just form processing code.
    I'm not sure why you're including a file that does a header redirect .. maybe you could explain a bit more?

    Also remember to use exit; after a header redirect to make sure processing stops, for example:

    header("Location: wherever.php");
    exit;

  12. #12
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or, you could simple start with <?php $title = "Title"; include('page.php'); ?>

    and than do the rest after.....

  13. #13
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by markl999
    a meta-refresh is usually more appropriate.
    There is an http header for that too.
    PHP Code:
    header("Refresh: 5; http://www.domain.com"); 

  14. #14
    SitePoint Member
    Join Date
    Aug 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bokehman
    It is very simple. Write your script in the correct linear order. Redirects first (followed by exit), then other headers, then the script body. If you need ob_start your script is not written in the proper linear order.
    My scripts are given to me then I just need to upload all files to my hosting. Im not in IT line so i totally don't understand about php.
    925silverjewelry - Jewelry Accessories and more...
    FREE Gift Boxes

  15. #15
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    In home!
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I have a question about this... what if you have a fixed template for your site. So, you call ALL your content in the middle of that like this:

    index.php
    Code:
    <html>
    <head>
    </head>
    <body>
    --function to call the properly content, depends of the URL-- index.php?show=foo
    </body>
    </html>
    So, the content that you are calling in the middle of the template will do the hard work, because its the place where everything will be displayed. So... if one of your web sections has a code like this:

    PHP Code:
    //go back to home buddy
    if($_SERVER['HTTP_REFERER']!='something'){
      
    header('Location: index.php');

    How would you do that without ob_start()?. That's a big dilema.
    Takitei
    FAQtion.com - Get answers to your development FAQs now!

  16. #16
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Takitei
    How would you do that without ob_start()?. That's a big dilema.
    The big dilema is should I scrap the lot and start again doing it properly.

    The truth is you should process your logic section first and then produce your output accordingly. If you look at HTML and CSS you will see a lot has been done to separate layout from content. You should write your php in the same fashion, separating logic from presentation.

    The template need not be the way you are doing it. You should infact call the template last.

  17. #17
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    In home!
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bokehman
    You should infact call the template last.
    What do you mean?... Explain me please... any example?... I would really like to know which is the best way to work with my template.
    Takitei
    FAQtion.com - Get answers to your development FAQs now!

  18. #18
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    In home!
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok. I was thinking about this, and the template should work like this maybe:

    Code:
    --function to call the properly content, depends of the URL-- 
    <html>
    <head>
    </head>
    <body>
    --echo here the content generated or called from the db--
    </body>
    </html>
    But... I was thinking about a very peculiar case. Imagine that you have to start a session, check the session values and take a choice about it.

    PHP Code:
    session_start();
    if(
    $_SESSION['name']!='danny'){
      
    //here we go
      
    header('Location: cannotsendheaderinformation.php');

    What should I do in that case?
    Takitei
    FAQtion.com - Get answers to your development FAQs now!

  19. #19
    SitePoint Zealot
    Join Date
    Aug 2005
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As long as the session value check and redirect come before any output, you should be okay..

  20. #20
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    In home!
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got it. Thanks!
    I think I need to sleep.
    Takitei
    FAQtion.com - Get answers to your development FAQs now!


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
  •