sending multiple headers (session_start() and setcookie() )

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben

    sending multiple headers (session_start() and setcookie() )

    Hi all,

    In my .php file, I'm using both session_start() and setcookie() before
    <html> tag. It gives me following warning message:

    Warning: Cannot modify header information - headers already sent by
    (output started at D:\Apache Group\Apache2\h tdocs\YC\songs. php:4) in
    D:\Apache Group\Apache2\h tdocs\YC\ycphpf unc.php on line 148

    My .php file looks like this:

    <?php session_start() ; ?>

    <?php
    ob_start();
    include 'ycphpfunc.php' ;
    $login = new login_class;
    if ($_POST[logusername] == "" || $_POST[logpassword] == "") {}
    else {
    $login->check_login($_ POST['logusername'], $_POST['logpassword'],
    $_POST['remember']);
    }
    ob_end_flush();
    ?>
    <html>
    ........
    ........
    </html>

    The call to check_login()ca lls setcookie() function through another
    function inside my "ycphpfunc. php" file.

    Can someone show me a way to include both session_start() and
    setcookie() before <html>?

    Thanks for your time!
    Ben
  • Chris Hope

    #2
    Re: sending multiple headers (session_start( ) and setcookie() )

    Ben wrote:
    [color=blue]
    > In my .php file, I'm using both session_start() and setcookie() before
    > <html> tag. It gives me following warning message:
    >
    > Warning: Cannot modify header information - headers already sent by
    > (output started at D:\Apache Group\Apache2\h tdocs\YC\songs. php:4) in
    > D:\Apache Group\Apache2\h tdocs\YC\ycphpf unc.php on line 148
    >
    > My .php file looks like this:
    >
    > <?php session_start() ; ?>
    > [/color]

    It looks to me like there's white space (a line break) right here
    inbetween the closing ?> and opening <?php I'm not sure why you've
    coded it like this though. Why not just have one code block? See my
    example below.
    [color=blue]
    > <?php
    > ob_start();
    > include 'ycphpfunc.php' ;
    > $login = new login_class;
    > if ($_POST[logusername] == "" || $_POST[logpassword] == "") {}
    > else {
    > $login->check_login($_ POST['logusername'], $_POST['logpassword'],
    > $_POST['remember']);
    > }
    > ob_end_flush();
    > ?>
    > <html>
    > .......
    > .......
    > </html>
    >
    > The call to check_login()ca lls setcookie() function through another
    > function inside my "ycphpfunc. php" file.
    >
    > Can someone show me a way to include both session_start() and
    > setcookie() before <html>?[/color]

    Change it to be like this:

    <?php
    session_start() ;
            ob_start();
    ...


    or like this:

    <?php
            ob_start();
    session_start() ;
    ...

    --
    Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

    Comment

    • Roy W. Andersen

      #3
      Re: sending multiple headers (session_start( ) and setcookie() )

      Ben wrote:
      [snip][color=blue]
      > Warning: Cannot modify header information - headers already sent by
      > (output started at D:\Apache Group\Apache2\h tdocs\YC\songs. php:4) in
      > D:\Apache Group\Apache2\h tdocs\YC\ycphpf unc.php on line 148[/color]
      [snip][color=blue]
      > include 'ycphpfunc.php' ;
      > $login = new login_class;
      > if ($_POST[logusername] == "" || $_POST[logpassword] == "") {}
      > else {
      > $login->check_login($_ POST['logusername'], $_POST['logpassword'],[/color]
      [snip]

      According to your error-message, the output is started at line 148 in
      your ycphpfunc.php file, which is included before you call your
      check_login method. The error isn't generated by session_start, but by
      setcookie().

      What does line 148 in ycphpfunc.php do? ;)


      Roy W. Andersen
      --
      ra at broadpark dot no / http://roy.netgoth.org/

      "Hey! What kind of party is this? There's no booze
      and only one hooker!" - Bender, Futurama

      Comment

      • crescent_au@yahoo.com

        #4
        Re: sending multiple headers (session_start( ) and setcookie() )


        Roy W. Andersen wrote:[color=blue]
        > Ben wrote:
        > [snip][color=green]
        > > Warning: Cannot modify header information - headers already sent by
        > > (output started at D:\Apache Group\Apache2\h tdocs\YC\songs. php:4)[/color][/color]
        in[color=blue][color=green]
        > > D:\Apache Group\Apache2\h tdocs\YC\ycphpf unc.php on line 148[/color]
        > [snip][color=green]
        > > include 'ycphpfunc.php' ;
        > > $login = new login_class;
        > > if ($_POST[logusername] == "" || $_POST[logpassword] == "") {}
        > > else {
        > > $login->check_login($_ POST['logusername'], $_POST['logpassword'],[/color]
        > [snip]
        >
        > According to your error-message, the output is started at line 148 in[/color]
        [color=blue]
        > your ycphpfunc.php file, which is included before you call your
        > check_login method. The error isn't generated by session_start, but[/color]
        by[color=blue]
        > setcookie().
        >[/color]
        just a reminder, the login_class class is also included inside
        ycphpfunc.php file.
        [color=blue]
        > What does line 148 in ycphpfunc.php do? ;)
        >[/color]
        line 148 in ycphpfunc.php has the following line:
        setcookie('reme mber',$cookie, time()+31104000 );
        [color=blue]
        >
        > Roy W. Andersen
        > --
        > ra at broadpark dot no / http://roy.netgoth.org/
        >
        > "Hey! What kind of party is this? There's no booze
        > and only one hooker!" - Bender, Futurama[/color]

        Comment

        • R. Rajesh Jeba Anbiah

          #5
          Re: sending multiple headers (session_start( ) and setcookie() )

          Ben wrote:[color=blue]
          > Hi all,
          >
          > In my .php file, I'm using both session_start() and setcookie()[/color]
          before[color=blue]
          > <html> tag. It gives me following warning message:
          >
          > Warning: Cannot modify header information - headers already sent by
          > (output started at D:\Apache Group\Apache2\h tdocs\YC\songs. php:4) in
          > D:\Apache Group\Apache2\h tdocs\YC\ycphpf unc.php on line 148
          >
          > My .php file looks like this:
          >
          > <?php session_start() ; ?>
          >
          > <?php
          > ob_start();
          > include 'ycphpfunc.php' ;
          > $login = new login_class;
          > if ($_POST[logusername] == "" || $_POST[logpassword] == "") {}
          > else {
          > $login->check_login($_ POST['logusername'], $_POST['logpassword'],
          > $_POST['remember']);
          > }
          > ob_end_flush();
          > ?>[/color]

          1. Put everything under ob_start()
          2. Send all headers (cookies/session/etc) before outputting

          --
          <?php echo 'Just another PHP saint'; ?>
          Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

          Comment

          • Mark

            #6
            Re: sending multiple headers (session_start( ) and setcookie() )

            Ben wrote:
            [color=blue]
            >
            > <?php session_start() ; ?>
            >
            > <?php
            > ob_start();[/color]

            That blank line is killing you. In fact, any carriage return characters
            not INSIDE <?php and ?> characters will cause output to be sent by PHP
            unless you are using output buffering (ob_start()).

            That means included/required files too !!!!

            It can be quite upsetting to track all those down, so just make sure that
            ob_start() is absolulutely the first thing that happens in ANY php script
            file ...


            marc.




            --
            I am not an ANGRY man. Remove the rage from my email to reply.

            Comment

            Working...