www.webdeveloper.com
Results 1 to 12 of 12

Thread: Cookie Help

  1. #1
    Join Date
    Sep 2005
    Location
    Germany
    Posts
    55

    Cookie Help

    When setting Cookies for instance
    PHP Code:
    setcookie("UsersName""Insane"time()+3600); 
    i always get the error:
    Warning: Cannot modify header information - headers already sent by (output started at d:\apache\php\temp2.php:3) in d:\apache\php\temp2.php on line 4
    -insane

  2. #2
    Join Date
    Sep 2004
    Posts
    398
    You have to set cookies before any output (echoes, prints, whitespace, normal html, etc.) or you can put
    PHP Code:
    <?php
    ob_start
    ();
    ?>
    at the very start of your script before any output.

  3. #3
    Join Date
    Sep 2005
    Location
    Germany
    Posts
    55
    2 problems.
    first of all. where should i put it? examples pls
    second of all. the error appears even if i don't have any output on the page.

    -Insane

  4. #4
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    His suggestion would be used before anything else in the script. (first line without any leading white space. To be honest though it is a band aid fix and you should write your code so there is no output including white space in included files before headers have been sent.

  5. #5
    Join Date
    Sep 2005
    Location
    Germany
    Posts
    55
    Well i sorta tried to understand the error message
    Warning: Cannot modify header information - headers already sent by (output started at d:\apache\php\temp2.php:3) in d:\apache\php\temp2.php on line 5
    and i the problem is that it can't even set the cookie, not that php can't read it.
    i cut/pasted the exact code from a tutorial site and it still didn't work. i'm using FireFox v1.0.4
    pls help

    -Insane

    Full Code:
    PHP Code:
    <html>
    <body>
    <?php
    ob_start
    ();
    setcookie("UsersName""Insane"time()+3600);
    ?>
    </body>
    </html>

  6. #6
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    Code:
    <?php
    ob_start();
    setcookie("UsersName", "Insane", time()+3600);
    ?>
    <html>
    <body>
    <?php
    printf($_COOKIE['UsersName']);
    ?>
    </body>
    </html>
    That's what they're telling you to do.
    Last edited by chazzy; 10-15-2005 at 02:43 PM. Reason: typoooooooo

  7. #7
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Quote Originally Posted by chazzy
    Code:
    <?php
    ob_start();
    setcookie("UsersName", "Insane", time()+3600);
    ?>
    <html>
    <body>
    <?php
    printf($_COOKIE['UsersName']);
    ?>
    </body>
    </html>
    That's what they're telling you to do.
    Two things:
    1) ob_start is not necessary,
    2) You cannot send and then read a cookie on the same request.

  8. #8
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    Quote Originally Posted by bokeh
    Two things:
    1) ob_start is not necessary,
    2) You cannot send and then read a cookie on the same request.
    I just included the print line so he knew how to reference his cookie.

    and I just basically copied and pasted his code so he saw how it moved.

  9. #9
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Quote Originally Posted by chazzy
    I just included the print line so he knew how to reference his cookie.

    and I just basically copied and pasted his code so he saw how it moved.
    I'm not saying your code is wrong... I am just clarifing those two issues!

  10. #10
    Join Date
    Oct 2005
    Posts
    53
    has anyone told him to make sure there is no white space before or after ?> <?php tags yet?

  11. #11
    Join Date
    Sep 2005
    Location
    Germany
    Posts
    55
    i't worked when i put the cookieset in front of the <html> tags.
    thx for your help guys

    -Insane

  12. #12
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    Quote Originally Posted by Scleppel
    You have to set cookies before any output (echoes, prints, whitespace, normal html, etc.) or you can put
    PHP Code:
    <?php
    ob_start
    ();
    ?>
    at the very start of your script before any output.
    as far as I'm concerned, this is Scleppel telling him to put it before anything in his page. i guess it should have been pointed out what this means exactly.

    glad you got it working.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles