Wierd setcookie Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cmaza
    New Member
    • May 2007
    • 16

    Wierd setcookie Problem

    Hi,

    I've been dealing with PHP for a few years now and I've never encountered a problem quite like this.

    I've searched the net for an answer to my problem hoping somebody else may have encountered this, but without any luck, so I now resolve to pose the question myself.

    Code:
    [CODE=PHP]
    <?php
    $somedata="Lore m Ipsum";
    setcookie("some name",$somedata ,time()+43200,"/");
    ?>
    [/CODE]

    [Please use CODE tags when posting source code. Thanks! --pbmods]

    That's the everything on the relevant page. Just that. Two lines of code. No extra content or includes.
    This page is not being included from any other pages and is being accessed directly.

    Problem:

    It doesn't work, and this is the first time setcookie hasn't worked for me without any obvious conflictions with other code. No cookie is being set.

    Instead, PHP returns the following the error:

    Warning: Cannot modify header information - headers already sent by (output started at /public_html/mkl/cookie.php:1) in /public_html/mkl/cookie.php on line 3

    I've tried running the script over a multitude of browsers such as Firefox 1x, Firefox 2x, IE5, IE6, Mozilla, and it's the same result on all of them.

    The server that the script is being executed has never given me such problems before (this is the first time setcookie hasn't worked for me like this) and as far as I'm aware has not undergone any recent changes.

    The server is Apache operated and has the latest version PHP installed.

    Anyone that can offer any assistance or suggestions on how I may resolve this problem would be greatly appreciated.

    Thanks.
  • Cmaza
    New Member
    • May 2007
    • 16

    #2
    Nevermind.

    I've figured out what the problem was.

    Always seems to go that way -> Waste hours of your life away trying to sort out a seemingly impossible problem, only to ask for help then finally figure out what's wrong for yourself... ¬_¬

    Incase anyone else has the same problem - I was sending the incorrect character encoding with the page header, and I'm guessing either the server, PHP or all the browsers I tested didn't like me doing that.

    Anyhow, thanks for putting up with me.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, Cmaza. Welcome to TSDN!

      Originally posted by Cmaza
      Warning: Cannot modify header information - headers already sent by (output started at /public_html/mkl/cookie.php:1) in /public_html/mkl/cookie.php on line 3
      You're getting this error because your script is sending output to the browser either before or after it tries to set the cookie.

      In order for this to work, you'd have to remove all leading whitespace before the opening <?php tag, and any whitespace after the last ?> tag as well.

      That's the usual culprit, at any rate. Interesting that you solved it by changing the character encoding....

      Comment

      • satya61229
        New Member
        • Feb 2007
        • 24

        #4
        Originally posted by Cmaza
        Hi,

        I've been dealing with PHP for a few years now and I've never encountered a problem quite like this.

        I've searched the net for an answer to my problem hoping somebody else may have encountered this, but without any luck, so I now resolve to pose the question myself.

        Code:
        [CODE=PHP]
        <?php
        $somedata="Lore m Ipsum";
        setcookie("some name",$somedata ,time()+43200,"/");
        ?>
        [/CODE]

        [Please use CODE tags when posting source code. Thanks! --pbmods]

        That's the everything on the relevant page. Just that. Two lines of code. No extra content or includes.
        This page is not being included from any other pages and is being accessed directly.

        Problem:

        It doesn't work, and this is the first time setcookie hasn't worked for me without any obvious conflictions with other code. No cookie is being set.

        Instead, PHP returns the following the error:

        Warning: Cannot modify header information - headers already sent by (output started at /public_html/mkl/cookie.php:1) in /public_html/mkl/cookie.php on line 3

        I've tried running the script over a multitude of browsers such as Firefox 1x, Firefox 2x, IE5, IE6, Mozilla, and it's the same result on all of them.

        The server that the script is being executed has never given me such problems before (this is the first time setcookie hasn't worked for me like this) and as far as I'm aware has not undergone any recent changes.

        The server is Apache operated and has the latest version PHP installed.

        Anyone that can offer any assistance or suggestions on how I may resolve this problem would be greatly appreciated.

        Thanks.
        You got a right reply. You can use ob_start(); at top of the page also.

        get more header info here

        Comment

        Working...