Testing the presence of a given cookie

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

    Testing the presence of a given cookie


    Hi,

    How do I test if a user has a given cookie? O:-)

    Thanks


  • windandwaves

    #2
    Re: Testing the presence of a given cookie

    Fernando Rodríguez wrote:[color=blue]
    > Hi,
    >
    > How do I test if a user has a given cookie? O:-)
    >
    > Thanks[/color]

    Here is how I do it:

    //set the cookie:
    setcookie("Cook ieTest", "t");

    //test of the cookie exists (can only be done in the next page / load)
    if($_COOKIE["CookieTest "] == "t") {
    echo "cookie exists";
    }


    HTH

    - Nicolaas


    Comment

    • Fernando Rodríguez

      #3
      Re: Testing the presence of a given cookie

      Hello windandwaves,
      [color=blue][color=green]
      >> How do I test if a user has a given cookie? O:-)
      >>
      >> Thanks
      >>[/color]
      > Here is how I do it:
      >
      > //set the cookie:
      > setcookie("Cook ieTest", "t");
      > //test of the cookie exists (can only be done in the next page / load)
      > if($_COOKIE["CookieTest "] == "t") {
      > echo "cookie exists";
      > }[/color]


      Thanks it works. However, if the cookie doesn't exist, I get a warning on
      the rendered page similar to this one:
      Notice: Undefined index: aTestCootie in c:\proyectos\we bsites\test\act ion.php
      on line 13

      That's great for debugging, but how do Iavoid this from showing up in production
      code?

      Thanks


      Comment

      • Jerry Stuckle

        #4
        Re: Testing the presence of a given cookie

        Fernando Rodríguez wrote:[color=blue]
        > Hello windandwaves,
        >[color=green][color=darkred]
        >>> How do I test if a user has a given cookie? O:-)
        >>>
        >>> Thanks
        >>>[/color]
        >> Here is how I do it:
        >>
        >> //set the cookie:
        >> setcookie("Cook ieTest", "t");
        >> //test of the cookie exists (can only be done in the next page / load)
        >> if($_COOKIE["CookieTest "] == "t") {
        >> echo "cookie exists";
        >> }[/color]
        >
        >
        >
        > Thanks it works. However, if the cookie doesn't exist, I get a warning
        > on the rendered page similar to this one:
        > Notice: Undefined index: aTestCootie in
        > c:\proyectos\we bsites\test\act ion.php on line 13
        >
        > That's great for debugging, but how do Iavoid this from showing up in
        > production code?
        >
        > Thanks
        >
        >[/color]

        if (isset($_COOKIE['CookieTest'] && $_COOKIE['CookieTest'] == 't') ...

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

        Comment

        Working...