setcookie fails to set cookie!

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

    setcookie fails to set cookie!

    Fourth attempt.. it fails now in login, I check by printing
    $_COOKIE['nordicnet_regi stration'] and there is no value there! Guys, what
    on earth do I do about this????

    Here is the code that sets the cookie:

    if ($hasLoggedIn && ($row = mysql_fetch_row ($query))) {
    setcookie('nord icnet_registrat ion', $row['nnet_user_regi strationnumber'],
    0, '/');
    @mysql_free_res ult($query);
    mysql_close($db Conn) or die('Could not close db');
    $html .= $font . '<font color=000099>Ta kk for logging inn. For forsatte
    ' .
    "<a
    href=http://$serverName/index.php?conte nt=membersites/Palogget%20S1.p hp " .
    ' target=_top>kli kk her</a></font></font>';
    } else if (sizeof($_POST) > 0) {
    $errorMsg .= $font . '<font color=cc0000><l i>Det finns en problem med
    db</li></font></font><p>';
    $hasLoggedIn = 0;
    }


    Because the PHP script is in a frame, instead of using header() I print out
    some text with a link <a href=mylink.php target=_top> for the user to click
    and to go to another page. By then the cookie should be registered, yet
    it's not there!

    I'm at a complete loss.

    Phil


  • Janwillem Borleffs

    #2
    Re: setcookie fails to set cookie!


    "Phil Powell" <soazine@erols. com> schreef in bericht
    news:Ikq6b.1266 16$xf.18927@lak eread04...[color=blue]
    > Fourth attempt.. it fails now in login, I check by printing
    > $_COOKIE['nordicnet_regi stration'] and there is no value there! Guys,[/color]
    what[color=blue]
    > on earth do I do about this????
    >
    > Here is the code that sets the cookie:
    >
    > if ($hasLoggedIn && ($row = mysql_fetch_row ($query))) {
    > setcookie('nord icnet_registrat ion',[/color]
    $row['nnet_user_regi strationnumber'],[color=blue]
    > 0, '/');[/color]

    Makes sence, because you are giving the cookie a life time of 0 seconds. If
    you want to create a session cookie with the path argument (so, without the
    expire argument), try using the header() function instead:

    header("Set-Cookie:
    nordicnet_regis tration={$row['nnet_user_regi strationnumber} ; path=/");

    Be aware that cookie values should be url encoded when they contain special
    characters like whitespaces.


    JW



    Comment

    • Phil Powell

      #3
      Re: setcookie fails to set cookie!

      Ok I've had it! PHP is losing its superiority status at this point! I am
      beginning to HATE this language!!!!!!! !

      I did what you suggested and it worked.. but why is it that in logginn.php
      your header() solution worked, but it FAILED it registrering.ph p to do the
      EXACT SAME THING! However, THIS worked in registrering.ph p but FAILED in
      logginn.php:

      setcookie('nord icnet_registrat ion', $registrationNu mber, 0, '/');

      Both have the exact same logic: set cookie -> display text with link -> go
      to next page

      Phil
      "Janwillem Borleffs" <jwb@jwbfoto.de mon.nl> wrote in message
      news:3f5a3656$0 $28894$1b62eedf @news.euronet.n l...[color=blue]
      >
      > "Phil Powell" <soazine@erols. com> schreef in bericht
      > news:Ikq6b.1266 16$xf.18927@lak eread04...[color=green]
      > > Fourth attempt.. it fails now in login, I check by printing
      > > $_COOKIE['nordicnet_regi stration'] and there is no value there! Guys,[/color]
      > what[color=green]
      > > on earth do I do about this????
      > >
      > > Here is the code that sets the cookie:
      > >
      > > if ($hasLoggedIn && ($row = mysql_fetch_row ($query))) {
      > > setcookie('nord icnet_registrat ion',[/color]
      > $row['nnet_user_regi strationnumber'],[color=green]
      > > 0, '/');[/color]
      >
      > Makes sence, because you are giving the cookie a life time of 0 seconds.[/color]
      If[color=blue]
      > you want to create a session cookie with the path argument (so, without[/color]
      the[color=blue]
      > expire argument), try using the header() function instead:
      >
      > header("Set-Cookie:
      > nordicnet_regis tration={$row['nnet_user_regi strationnumber} ; path=/");
      >
      > Be aware that cookie values should be url encoded when they contain[/color]
      special[color=blue]
      > characters like whitespaces.
      >
      >
      > JW
      >
      >
      >[/color]


      Comment

      • Pedro

        #4
        Re: setcookie fails to set cookie!

        Phil Powell wrote:[color=blue]
        >Fourth attempt.. it fails now in login, I check by printing
        >$_COOKIE['nordicnet_regi stration'] and there is no value there! Guys, what
        >on earth do I do about this????
        >
        >Here is the code that sets the cookie:
        >
        >if ($hasLoggedIn && ($row = mysql_fetch_row ($query))) {
        > setcookie('nord icnet_registrat ion', $row['nnet_user_regi strationnumber'],
        >0, '/');[/color]
        [...][color=blue]
        >By then the cookie should be registered, yet it's not there!
        >
        >I'm at a complete loss.[/color]

        mysql_fetch_row () does not return the column names as an index!

        try mysql_fetch_ass oc()
        or use numeric indexes for the colunms:
        setcookie('nord icnet_registrat ion', $row[0], 0, '/');


        HTH

        --
        "Yes, I'm positive."
        "Are you sure?"
        "Help, somebody has stolen one of my electrons!"
        Two atoms are talking:

        Comment

        • Janwillem Borleffs

          #5
          Re: setcookie fails to set cookie!


          "Phil Powell" <soazine@erols. com> schreef in bericht
          news:AIq6b.1266 97$xf.39169@lak eread04...[color=blue]
          > Ok I've had it! PHP is losing its superiority status at this point! I am
          > beginning to HATE this language!!!!!!! !
          >[/color]

          PHP, nor any other language can teach the programmer the proper and logical
          way of thinking. When you know that the third argument of a function that
          creates a cookie sets its life time, you must realize that you should pass
          it a value larger then zero to create it.
          [color=blue]
          > I did what you suggested and it worked.. but why is it that in logginn.php
          > your header() solution worked, but it FAILED it registrering.ph p to do the
          > EXACT SAME THING! However, THIS worked in registrering.ph p but FAILED in
          > logginn.php:
          >
          > setcookie('nord icnet_registrat ion', $registrationNu mber, 0, '/');
          >[/color]

          Then you are probably facing a conflict with cookie names which overwrite
          each other. Try other names to test this, eg:

          header("Set-Cookie: nordicnet_regis tration=1; path=/");

          in registering.php and

          header("Set-Cookie: nordicnet_regis tration2=1; path=/");

          in login.php.

          Also, don't forget that once a cookie has been created, it will only be
          available after a reload of the page.

          So:

          <?
          header("Set-Cookie: nordicnet_regis tration=1; path=/");
          echo $_COOKIE['nordictnet_reg istration'];
          ?>

          Displays nothing.


          JW



          Comment

          • Phil Powell

            #6
            Re: setcookie fails to set cookie!

            OK this is how I did my test:

            1) I go to registrering.ph p and register fully, thereby setting the
            nordicnet_regis tration cookie. It's all set, everything is fine (uses
            setcookie() with 0 time).

            2) I close my browser

            3) I reboot computer (just for safety sake)

            4) I reopen my browser and go to logginn.php

            5) I log in, however, setcookie() this time does NOT work, the cookie is not
            set because AFTER I click the link to go to another page the
            $_COOKIE['nordicnet_regi stration'] value is empty, thus, no cookie. HOWEVER,
            header() DOES work in logginn.php!

            6) I change the code in registrering.ph p to do header() instead of
            setcookie() (remember: setcookie() works, header() does NOT work in
            registrering.ph p)

            7) I close the browser

            8) I reboot computer

            9) I reopen browser

            10) I go to registrering.ph p Now THIS TIME I have header() and NOT
            setcookie() instead of the reverse. I register under a new name and email..
            Registration is complete, the cookie SHOULD be set. I get a link to click.
            I click the link, go to my next page.. AND NO COOKIE!

            So.. this is what happens:

            registrering.ph p:

            Register -> setcookie('stuf f', $stuff, 0, '/') WORKS -> click link -> there
            is the cookie!

            Register -> header() FAILS -> click link -> NO COOKIE

            logginn.php:

            Login -> setcookie('stuf f', $stuff, 0, '/') FAILS -> click link -> NO COOKIE

            Login -> header() WORKS -> click link -> there is the cookie!

            Phil
            "Janwillem Borleffs" <jwb@jwbfoto.de mon.nl> wrote in message
            news:3f5a3e0e$0 $28899$1b62eedf @news.euronet.n l...[color=blue]
            >
            > "Phil Powell" <soazine@erols. com> schreef in bericht
            > news:AIq6b.1266 97$xf.39169@lak eread04...[color=green]
            > > Ok I've had it! PHP is losing its superiority status at this point! I[/color][/color]
            am[color=blue][color=green]
            > > beginning to HATE this language!!!!!!! !
            > >[/color]
            >
            > PHP, nor any other language can teach the programmer the proper and[/color]
            logical[color=blue]
            > way of thinking. When you know that the third argument of a function that
            > creates a cookie sets its life time, you must realize that you should pass
            > it a value larger then zero to create it.
            >[color=green]
            > > I did what you suggested and it worked.. but why is it that in[/color][/color]
            logginn.php[color=blue][color=green]
            > > your header() solution worked, but it FAILED it registrering.ph p to do[/color][/color]
            the[color=blue][color=green]
            > > EXACT SAME THING! However, THIS worked in registrering.ph p but FAILED in
            > > logginn.php:
            > >
            > > setcookie('nord icnet_registrat ion', $registrationNu mber, 0, '/');
            > >[/color]
            >
            > Then you are probably facing a conflict with cookie names which overwrite
            > each other. Try other names to test this, eg:
            >
            > header("Set-Cookie: nordicnet_regis tration=1; path=/");
            >
            > in registering.php and
            >
            > header("Set-Cookie: nordicnet_regis tration2=1; path=/");
            >
            > in login.php.
            >
            > Also, don't forget that once a cookie has been created, it will only be
            > available after a reload of the page.
            >
            > So:
            >
            > <?
            > header("Set-Cookie: nordicnet_regis tration=1; path=/");
            > echo $_COOKIE['nordictnet_reg istration'];
            > ?>
            >
            > Displays nothing.
            >
            >
            > JW
            >
            >
            >[/color]


            Comment

            • Janwillem Borleffs

              #7
              Re: setcookie fails to set cookie!


              "Pedro" <hexkid@hotpop. com> schreef in bericht
              news:hfeklvsn3d 87u9a6rhne1gvvk 0m3riruv1@4ax.c om...[color=blue]
              >
              > mysql_fetch_row () does not return the column names as an index!
              >
              > try mysql_fetch_ass oc()
              > or use numeric indexes for the colunms:
              > setcookie('nord icnet_registrat ion', $row[0], 0, '/');
              >[/color]

              Yes, I think this indeed caused the problem. It seems that when the cookie
              function is applied with 0 as the expire time, it does the same as the
              header call I suggested.


              JW




              Comment

              • Janwillem Borleffs

                #8
                Re: setcookie fails to set cookie!


                "Phil Powell" <soazine@erols. com> schreef in bericht
                news:drr6b.1268 57$xf.1324@lake read04...[color=blue]
                > OK this is how I did my test:
                >[/color]

                I think that I have taken you to the wrong track. Leave the setcookie()
                calls as they are and take a look at Pedro's suggestion.


                JW



                Comment

                • Andy Hassall

                  #9
                  Re: setcookie fails to set cookie!

                  On Sat, 6 Sep 2003 22:05:32 +0200, "Janwillem Borleffs" <jwb@jwbfoto.de mon.nl>
                  wrote:
                  [color=blue]
                  >PHP, nor any other language can teach the programmer the proper and logical
                  >way of thinking. When you know that the third argument of a function that
                  >creates a cookie sets its life time, you must realize that you should pass
                  >it a value larger then zero to create it.[/color]

                  It is stated in the manual that specifying zero for this argument means to
                  skip it, i.e. not set an expiry time, for the case when you want to use the
                  other parameters after 'expire'.

                  "
                  All the arguments except the name argument are optional. You may also replace
                  an argument with an empty string ("") in order to skip that argument. Because
                  the expire and secure arguments are integers, they cannot be skipped with an
                  empty string, use a zero (0) instead. The following table explains each
                  parameter of the setcookie() function, be sure to read the Netscape cookie
                  specification for specifics on how each setcookie() parameter works and RFC
                  2965 for additional information on how HTTP cookies work.
                  "

                  "
                  expire The time the cookie expires. This is a unix timestamp so is in number of
                  seconds since the epoch. In otherwords, you'll most likely set this with the
                  time() function plus the number of seconds before you want it to expire. Or you
                  might use mktime().

                  time()+60*60*24 *30 will set the cookie to expire in 30 days. If not set, the
                  cookie will expire at the end of the session (when the browser closes).
                  "

                  So zero does mean 'session cookie'.

                  --
                  Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
                  Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

                  Comment

                  • Janwillem Borleffs

                    #10
                    Re: setcookie fails to set cookie!


                    "Andy Hassall" <andy@andyh.co. uk> schreef in bericht
                    news:ctgklv0ork 3923g83fuo21npc 10qieofgk@4ax.c om...[color=blue]
                    >
                    > So zero does mean 'session cookie'.
                    >[/color]

                    Yes, now I know...

                    JW



                    Comment

                    • Phil Powell

                      #11
                      Re: setcookie fails to set cookie!

                      Ok I changed my code to reflect the associative array $row:

                      // IF hasLoggedIn IS STILL TRUE WE FOUND THEIR LOGIN INFORMATION - CLOSE UP,
                      SET COOKIE AND REDIRECT
                      if ($hasLoggedIn && ($row = mysql_fetch_ass oc($query))) {
                      setcookie('nord icnet_registrat ion',
                      "$row['nnet_user_regi strationnumber']", 0, '/');
                      //header("Set-Cookie:nordicne t_registration= {" .
                      $row['nnet_user_regi strationnumber'] . "}; path=/");
                      @mysql_free_res ult($query);
                      mysql_close($db Conn) or die('Could not close db');
                      $html .= $font . '<font color=000099>Ta kk for logging inn. For forsatte
                      ' .
                      "<a href=/membersites/Palogget%20S1.p hp target=_top>kli kk
                      her</a></font></font>";
                      } else if (sizeof($_POST) > 0) {
                      $errorMsg .= $font . '<font color=cc0000><l i>Det finns en problem med
                      db</li></font></font><p>';
                      $hasLoggedIn = 0;
                      }

                      Guess what? IT STILL FAILED! Unless I specfically use header() instead of
                      setcookie() it never works within logginn.php (but if I use setcookie()
                      instead of header() in registrering.ph p with the exact same program logic,
                      it works, the opposite fails!)

                      Phil
                      "Pedro" <hexkid@hotpop. com> wrote in message
                      news:hfeklvsn3d 87u9a6rhne1gvvk 0m3riruv1@4ax.c om...[color=blue]
                      > Phil Powell wrote:[color=green]
                      > >Fourth attempt.. it fails now in login, I check by printing
                      > >$_COOKIE['nordicnet_regi stration'] and there is no value there! Guys,[/color][/color]
                      what[color=blue][color=green]
                      > >on earth do I do about this????
                      > >
                      > >Here is the code that sets the cookie:
                      > >
                      > >if ($hasLoggedIn && ($row = mysql_fetch_row ($query))) {
                      > > setcookie('nord icnet_registrat ion',[/color][/color]
                      $row['nnet_user_regi strationnumber'],[color=blue][color=green]
                      > >0, '/');[/color]
                      > [...][color=green]
                      > >By then the cookie should be registered, yet it's not there!
                      > >
                      > >I'm at a complete loss.[/color]
                      >
                      > mysql_fetch_row () does not return the column names as an index!
                      >
                      > try mysql_fetch_ass oc()
                      > or use numeric indexes for the colunms:
                      > setcookie('nord icnet_registrat ion', $row[0], 0, '/');
                      >
                      >
                      > HTH
                      >
                      > --
                      > "Yes, I'm positive."
                      > "Are you sure?"
                      > "Help, somebody has stolen one of my electrons!"
                      > Two atoms are talking:[/color]


                      Comment

                      • Phil Powell

                        #12
                        Re: setcookie fails to set cookie!

                        Ok last time.. I changed to $row[0] and everything is fine with setcookie()
                        now..

                        So apparently while header() could set an undefined cookie value,
                        setcookie() will not do so. Is there documentation on this behavior?

                        Phil

                        "Phil Powell" <soazine@erols. com> wrote in message
                        news:ZBr6b.1269 00$xf.85727@lak eread04...[color=blue]
                        > Ok I changed my code to reflect the associative array $row:
                        >
                        > // IF hasLoggedIn IS STILL TRUE WE FOUND THEIR LOGIN INFORMATION - CLOSE[/color]
                        UP,[color=blue]
                        > SET COOKIE AND REDIRECT
                        > if ($hasLoggedIn && ($row = mysql_fetch_ass oc($query))) {
                        > setcookie('nord icnet_registrat ion',
                        > "$row['nnet_user_regi strationnumber']", 0, '/');
                        > //header("Set-Cookie:nordicne t_registration= {" .
                        > $row['nnet_user_regi strationnumber'] . "}; path=/");
                        > @mysql_free_res ult($query);
                        > mysql_close($db Conn) or die('Could not close db');
                        > $html .= $font . '<font color=000099>Ta kk for logging inn. For[/color]
                        forsatte[color=blue]
                        > ' .
                        > "<a href=/membersites/Palogget%20S1.p hp target=_top>kli kk
                        > her</a></font></font>";
                        > } else if (sizeof($_POST) > 0) {
                        > $errorMsg .= $font . '<font color=cc0000><l i>Det finns en problem med
                        > db</li></font></font><p>';
                        > $hasLoggedIn = 0;
                        > }
                        >
                        > Guess what? IT STILL FAILED! Unless I specfically use header() instead of
                        > setcookie() it never works within logginn.php (but if I use setcookie()
                        > instead of header() in registrering.ph p with the exact same program logic,
                        > it works, the opposite fails!)
                        >
                        > Phil
                        > "Pedro" <hexkid@hotpop. com> wrote in message
                        > news:hfeklvsn3d 87u9a6rhne1gvvk 0m3riruv1@4ax.c om...[color=green]
                        > > Phil Powell wrote:[color=darkred]
                        > > >Fourth attempt.. it fails now in login, I check by printing
                        > > >$_COOKIE['nordicnet_regi stration'] and there is no value there! Guys,[/color][/color]
                        > what[color=green][color=darkred]
                        > > >on earth do I do about this????
                        > > >
                        > > >Here is the code that sets the cookie:
                        > > >
                        > > >if ($hasLoggedIn && ($row = mysql_fetch_row ($query))) {
                        > > > setcookie('nord icnet_registrat ion',[/color][/color]
                        > $row['nnet_user_regi strationnumber'],[color=green][color=darkred]
                        > > >0, '/');[/color]
                        > > [...][color=darkred]
                        > > >By then the cookie should be registered, yet it's not there!
                        > > >
                        > > >I'm at a complete loss.[/color]
                        > >
                        > > mysql_fetch_row () does not return the column names as an index!
                        > >
                        > > try mysql_fetch_ass oc()
                        > > or use numeric indexes for the colunms:
                        > > setcookie('nord icnet_registrat ion', $row[0], 0, '/');
                        > >
                        > >
                        > > HTH
                        > >
                        > > --
                        > > "Yes, I'm positive."
                        > > "Are you sure?"
                        > > "Help, somebody has stolen one of my electrons!"
                        > > Two atoms are talking:[/color]
                        >
                        >[/color]


                        Comment

                        • Pedro

                          #13
                          Re: setcookie fails to set cookie!

                          Phil Powell wrote:[color=blue]
                          >Ok I changed my code to reflect the associative array $row:
                          >
                          >// IF hasLoggedIn IS STILL TRUE WE FOUND THEIR LOGIN INFORMATION - CLOSE UP,
                          >SET COOKIE AND REDIRECT
                          > if ($hasLoggedIn && ($row = mysql_fetch_ass oc($query))) {
                          > setcookie('nord icnet_registrat ion',
                          >"$row['nnet_user_regi strationnumber']", 0, '/');[/color]

                          ?????

                          "$row['xyz']" is not the same as
                          $row['xyz'] and not the same as
                          "$row[xyz]"

                          as far as I can tell only the last two work :)

                          --
                          "Yes, I'm positive."
                          "Are you sure?"
                          "Help, somebody has stolen one of my electrons!"
                          Two atoms are talking:

                          Comment

                          • Pedro

                            #14
                            Re: setcookie fails to set cookie!

                            Phil Powell wrote:[color=blue]
                            >Ok last time.. I changed to $row[0] and everything is fine with setcookie()
                            >now..
                            >
                            >So apparently while header() could set an undefined cookie value,
                            >setcookie() will not do so. Is there documentation on this behavior?[/color]

                            I even tried to make sense of RFC2965 [1] ... but RFCs, definitely,
                            are not my friends.

                            [1] http://www.faqs.org/rfcs/rfc2965


                            --
                            "Yes, I'm positive."
                            "Are you sure?"
                            "Help, somebody has stolen one of my electrons!"
                            Two atoms are talking:

                            Comment

                            • Janwillem Borleffs

                              #15
                              Re: setcookie fails to set cookie!


                              "Phil Powell" <soazine@erols. com> schreef in bericht
                              news:HHr6b.1269 21$xf.3928@lake read04...[color=blue]
                              > Ok last time.. I changed to $row[0] and everything is fine with[/color]
                              setcookie()[color=blue]
                              > now..
                              >
                              > So apparently while header() could set an undefined cookie value,
                              > setcookie() will not do so. Is there documentation on this behavior?
                              >[/color]

                              I performed some tests:

                              When you call
                              setcookie("test ","",0,"/");

                              The cookie header looks like this:
                              Set-Cookie: test=deleted; expires=Fri, 06-Sep-02 21:08:27 GMT; path=/

                              Which deletes the cookie immediately.

                              But when you create the cookie through the header function with:
                              header("Set-Cookie: test=; path=/");

                              The cookie header looks like:
                              Set-Cookie: test=; path=/

                              So the cookie is indeed created with an empty value. I think this is an
                              undocumented feature of the setcookie() function (group, please correct me
                              if I'm wrong).


                              JW



                              Comment

                              Working...