Results 1 to 9 of 9

Thread: Headers already sent

  1. #1
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Headers already sent

    Hi all !

    I am a new kid on the PHP Block. I am using dreamweaver CS3 with WAMP.

    While performing as decribed in one of the tutorial, I am getting the following error.

    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\wamp\www\nationalEx\admin\login.php:1) in C:\wamp\www\nationalEx\admin\login.php on line 0

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\wamp\www\nationalEx\admin\login.php:1) in C:\wamp\www\nationalEx\admin\login.php on line 0

    Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\nationalEx\admin\login.php:1) in C:\wamp\www\nationalEx\admin\login.php on line 66

    Kindly advise me where & what to change.

    Thanks in advance.

    Tralsi

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    the line "session_start()" MUST be placed before any information is sent to the browser - it's best to put it in *immediately*, like so:
    PHP Code:
    <?php
    session_start
    ();
    //rest of your code...

  3. #3
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Unrelated question, and I usually don't use WYSIWYGs, but how do you like Dreamweaver CS3? Was it worth the $$?
    - Josh

  4. #4
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks Traq for your valuable suggestion !

    I am able to manage the first two errors while my page loads.

    unfortunately I still gets the following error after supplying username & password & while clicking on the submit button

    Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\nationalEx\admin\login.php:4) in C:\wamp\www\nationalEx\admin\login.php on line 70


    PHP Code

    code from line no 1 to 10 & from line no 67 to 75 is pasted for your reference.
    actually nothing has been changed to original code attached except the first 3 lines as suggested


    <?php
    session_start();
    ?>
    <?php virtual('/nationalEx/Connections/ConnNationalEx.php'); ?>
    <?php
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
    {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
    .
    .
    .
    .
    // code from line no 67 to 75

    if (isset($_SESSION['PrevUrl']) && false) {
    $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
    }
    header("Location: " . $MM_redirectLoginSuccess );
    }
    else {
    header("Location: ". $MM_redirectLoginFailed );
    }
    }
    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">



    I am hopeful that you will able to guide me in solving this one also.

    Thanks a lot again for your help.

    Tralsi

  5. #5
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Your IF statement is set so that you have a header(); called no matter what. This line is probably the problem:
    PHP Code:
    <?php virtual('/nationalEx/Connections/ConnNationalEx.php'); ?>
    Somewhere in that page you're calling headers already [whether being hypertext or headers() or sessions or cookies or whatever].

    Post the code from that page, or fix it so that headers are NOT called first from that page.

    HTH
    - Josh

  6. #6
    Join Date
    Aug 2009
    Posts
    74
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default

    try like this


    <?php
    ob_start();
    session_start();
    virtual('/nationalEx/Connections/ConnNationalEx.php');

    and continue your code by removing ur php open tag on 5th line.

    and i think it is better to use include() or require() than virtual
    Last edited by hemi; 09-12-2009 at 12:00 PM.

  7. #7
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Agreed. You really don't need to use virtual();. && like I said, be sure headers are terminated.
    - Josh

  8. #8
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for your reply JSHor & Hemi !

    I tried the script change suggested by Hemi i.e.

    <?php
    ob_start();
    session_start();
    virtual('/nationalEx/Connections/ConnNationalEx.php');


    even this didn't fix my problem so I am sending my code for ConnNationalEx.php as suggested by JShor

    Code:
    <?php
    # FileName="Connection_php_mysql.htm"
    # Type="MYSQL"
    # HTTP="true"
    $hostname_ConnNationalEx = "Localhost";
    $database_ConnNationalEx = "nationalex";
    $username_ConnNationalEx = "nationalEx";
    $password_ConnNationalEx = "nationalEx";
    $ConnNationalEx = mysql_pconnect($hostname_ConnNationalEx, $username_ConnNationalEx, $password_ConnNationalEx); or trigger_error(mysql_error(),E_USER_ERROR); 
    ?>

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    It can be very confusing once you get two (or more) pages embedded in each other. The real problem is when you end up having several page each with a header section and content section. This is actually similar to having two php pages embedded in each other each with <head> and <body> sections.
    In either case, the best way to approach it is to either write some complex php with confusing logic in order to embed part of one into the other then the rest of it later, or you could just go with four pages that all work together (header1.php, header2.php, content1.php, content2.php), and if you just put those together in the right order then it should be fine.
    But if you end up with something this complex then it is probably just time to rewrite the entire site so you are only sending headers once. If you can't, though, then, yes, it's time for something this complex.
    Since this is so important for how your site will function, it's best to design around this rather than adding it on at the end (which seems easier when you start building a site piece-by-piece rather than structure down to content).
    It will just take some practice and you'll find workarounds as you're aware of these issues.

    The best way to think about this is to understand PHP as an html-generator. So you have your PHP code with stuff going on in it to change settings, etc., then after it's all ready to start putting out html you can do it and the environment is setup. For that reason, there are a number of things you should do at the start (especially header-specific code, but generally just any non-output php [setup stuff]), then after that put any content.

    As a general rule your pages should look something like this, at least as much as possible:
    PHP Code:
    <?php
    //lots of php code here setting up the page
    ?>
    <html>
    ....
    </html>
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •