Strange error while trying to set a cookie

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fernando Rodríguez

    #1

    Strange error while trying to set a cookie


    Hi,

    Whenever I run a script page, I get this error:
    Warning: Cannot modify header information - headers already sent by (output
    started at c:\proyectos\we bsites\test\ind ex.php:9) in c:\proyectos\we bsites\test\ind ex.php
    on line 11

    My code is:

    <?
    $cookieValue = 'abra cadabra, pedo de cabra';
    setcookie('aTes tCookie', $cookieValue, time() + 3600);
    ?>

    Any ideas? O:-)

    Thanks


  • ZeldorBlat

    #2
    Re: Strange error while trying to set a cookie


    Fernando Rodríguez wrote:[color=blue]
    > Hi,
    >
    > Whenever I run a script page, I get this error:
    > Warning: Cannot modify header information - headers already sent by (output
    > started at c:\proyectos\we bsites\test\ind ex.php:9) in c:\proyectos\we bsites\test\ind ex.php
    > on line 11
    >
    > My code is:
    >
    > <?
    > $cookieValue = 'abra cadabra, pedo de cabra';
    > setcookie('aTes tCookie', $cookieValue, time() + 3600);
    > ?>
    >
    > Any ideas? O:-)
    >
    > Thanks[/color]

    You get that error message because the headers have already been sent
    and output has started. Specifically, you sent output on line 9 of
    index.php. If you want to set HTTP headers (and setting a cookie
    requires sending a header) you need to do it *before* you send any
    other output to the browser -- even a single whitespace counts.

    So, you have two options here:

    1. Re-order the script so that you call setcookie() before you send any
    output.
    2. Use ob_start() and ob_flush() to buffer the output (see
    <http://www.php.net/manual/en/ref.outcontrol. php> for more information).

    Comment

    • Pedro Graca

      #3
      Re: Strange error while trying to set a cookie

      Fernando Rodríguez wrote:[color=blue]
      > Whenever I run a script page, I get this error:
      > Warning: Cannot modify header information - headers already sent by (output
      > started at c:\proyectos\we bsites\test\ind ex.php:9) in c:\proyectos\we bsites\test\ind ex.php[/color]
      _______________ _______________ _______________ ___^___________ ___ ...[color=blue]
      > on line 11[/color]
      ________^^[color=blue]
      >
      > My code is:[/color]

      1 <?
      2 $cookieValue = 'abra cadabra, pedo de cabra';
      3 setcookie('aTes tCookie', $cookieValue, time() + 3600);
      4 ?>
      [color=blue]
      > Any ideas? O:-)[/color]

      Please post the rest of your code

      I guess line 3 in the example you posted is actually line 11 in your
      *real* script, making line 1 in the example your *real* line 9.

      The script you posted had absolutely no reason (*) to issue that
      warning.

      (*) save auto_prepend_fi le

      --
      If you're posting through Google read <http://cfaj.freeshell. org/google>

      Comment

      • Alvaro G. Vicario

        #4
        Re: Strange error while trying to set a cookie

        *** Pedro Graca escribió/wrote (16 Jan 2006 21:17:11 GMT):[color=blue]
        > The script you posted had absolutely no reason (*) to issue that
        > warning.
        >
        > (*) save auto_prepend_fi le[/color]

        Or blank spaces before the first delimiter.


        --
        -+ Álvaro G. Vicario - Burgos, Spain
        ++ http://bits.demogracia.com es mi sitio para programadores web
        +- http://www.demogracia.com es mi web de humor libre de cloro
        --

        Comment

        • Fernando Rodríguez

          #5
          Re: Strange error while trying to set a cookie

          Hello Pedro,
          [color=blue]
          > Please post the rest of your code[/color]


          OK, here's the full code:

          -----------------------------------
          <html>
          <head>
          <title>Untitl ed Document</title>
          <?
          $cookieValue = 'abra cadabra, pedo de cabra';
          setcookie('aTes tCookie', $cookieValue, time() + 3600);
          ?>
          <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
          </head>

          <body>



          <ol>
          <li> <a href="http://es2.php.net/FAQ.php">PHP FAQ</a></li>
          <li> <a href="http://es.php.net/manual/en/tutorial.firstp age.php">PHP Tutorial</a></li>
          <li> <a href="http://es.php.net/manual/en/index.php">PHP Manual</a></li>
          </ol>


          <form action="action. php" method="post">
          <p>Your name: <input type="text" name="name" /></p>
          <p>Your age: <input type="text" name="age" /></p>
          <p><input type="submit" /></p>
          </form>

          </body>
          </html>
          ------------------------------------

          This causes the follwoing output:

          Warning: Cannot modify header information - headers already sent by (output
          started at c:\proyectos\we bsites\test\ind ex.php:4) in c:\proyectos\we bsites\test\ind ex.php
          on line 6


          Comment

          • Jerry Stuckle

            #6
            Re: Strange error while trying to set a cookie

            Fernando Rodríguez wrote:[color=blue]
            > Hello Pedro,
            >[color=green]
            >> Please post the rest of your code[/color]
            >
            >
            >
            > OK, here's the full code:
            >
            > -----------------------------------
            > <html>
            > <head>
            > <title>Untitl ed Document</title>
            > <? $cookieValue = 'abra cadabra, pedo de cabra';
            > setcookie('aTes tCookie', $cookieValue, time() + 3600); ?>
            > <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            > </head>
            >
            > <body>
            >
            >
            >
            > <ol>
            > <li> <a href="http://es2.php.net/FAQ.php">PHP FAQ</a></li>
            > <li> <a
            > href="http://es.php.net/manual/en/tutorial.firstp age.php">PHP
            > Tutorial</a></li>
            > <li> <a href="http://es.php.net/manual/en/index.php">PHP
            > Manual</a></li>
            > </ol>
            >
            >
            > <form action="action. php" method="post">
            > <p>Your name: <input type="text" name="name" /></p>
            > <p>Your age: <input type="text" name="age" /></p>
            > <p><input type="submit" /></p>
            > </form>
            >
            > </body>
            > </html>
            > ------------------------------------
            >
            > This causes the follwoing output:
            >
            > Warning: Cannot modify header information - headers already sent by
            > (output started at c:\proyectos\we bsites\test\ind ex.php:4) in
            > c:\proyectos\we bsites\test\ind ex.php on line 6
            >
            >[/color]


            And it's correct. The headers are output as soon as ANY text is sent to
            the browser - in this case the headers were sent at the first line of
            your page:

            <html>

            Put the setcookie code immediately at the top of your file, and ensure
            there is no white space (or anything else) before it.

            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            • Pedro Graca

              #7
              Re: Strange error while trying to set a cookie

              Fernando Rodríguez wrote:[color=blue]
              > OK, here's the full code:
              >
              > -----------------------------------
              > <html>
              > <head>
              > <title>Untitl ed Document</title>
              > <?
              > $cookieValue = 'abra cadabra, pedo de cabra';
              > setcookie('aTes tCookie', $cookieValue, time() + 3600);
              > ?>
              > <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">[/color]
              <snip>[color=blue]
              > ------------------------------------
              >
              > This causes the follwoing output:
              >
              > Warning: Cannot modify header information - headers already sent by (output
              > started at c:\proyectos\we bsites\test\ind ex.php:4) in c:\proyectos\we bsites\test\ind ex.php
              > on line 6[/color]

              try

              -----------------------------------
              1 <?php
              2 $cookieValue = 'abra cadabra, pedo de cabra';
              3 setcookie('aTes tCookie', $cookieValue, time() + 3600);
              4 ?>
              5 <html>
              ....


              I started the code with "<?php" because, instead of "<?", it will work
              on all servers irrespective of their configuration. Your PHP start tag
              needs a specific configuration (short_open_tag s IIRC) to work.

              --
              If you're posting through Google read <http://cfaj.freeshell. org/google>

              Comment

              Working...