• PHP Help PHP Coding
  • delete cookie, setcooke("quantity") or setcookie("quantity", "")?

this is a statment i got from the book "professional php programming"

when it is trying to delete a cookie, it used

setcookie("quantity", "")

my question is

should it be

setcookie("quantity") instead of setcookie("quantity", "")?

setcookie("quantity", "") only reset the quantity value to empty string, but the cookie is not deleted yet, right?

so the better practice is setcookie("quantity"), right? any reason to use setcookie("quantity", "") not setcookie("quantity") to delete cookie?

thanks!

    Could the one who has the good cookie knowledge explain this to me?

    Thanks!

      when i delete a cookie, i use:

      setcookie( 'security', '', time() - 3600, '/' );

      reset value to nothing and expiry to the past. kills cookies dead.

        a month later

        I am learning PHP and MySQL. I've taken a cart example (phpcart) and modified for my purposes. Everything is working good with the exception of being able to kill the cookie used with the shopping cart.

        I am a including the folowing page with the cart page:
        <? php
        function GetCartId()
        {
        if(isset($COOKIE["cartId"]))
        {
        return $
        COOKIE["cartId"];
        }
        else
        {
        session_start();
        setcookie("cartId", session_id(), time() + ((3600 24) 30));
        return session_id();
        }}
        ?>;

        This is the code segment used after I accomplish everything I need to do and want to delete the cookie and return them to the home page:

        $cartid = GetCartId();
        setcookie("$cartid", " ", time() - ((3600 24) 30));
        unset($_COOKIE["$cartid"]);
        header ("location:./index.php");
        Any suggestioins would be appreciated.

        beerman

          setcookie("cartId");

          will delet the cookie cartId.

          note "cartId" not "$cartid"

            Thanks for your quick response! I gave it a try, but the cookie remains.

            Do I need to destroy the PHPSESSID as well? Is this a browser specific issue?

            Thanks again.

            beerman

              In your next page

              echo($cardId);

              to see if it returns value or empty string. if you called setcookie("cardId"), it should already delete the cookie, you will see whether the cookie still exists in your NEXT page, not in your current page. You should see empty string due to cookie deleted by the previous page.

              delete cookie will NOT change the value of $cartid in your CURRENT PAGE.

                I mean

                In your next page

                echo($cartId);

                  I guess I'm not following.

                  The customer comes to the products page and adds to the cart; they either go back to the products page to select additional products or checkout. At the checkout page, they complete the form and place order. After place order handles the cart items and emails confirmation, it redirects to the index page.

                  If they go from the index page, back to the products page, their cart is still there – and I would like it not to be there, or be there as a new cart (new cookie or session).

                  Does that make any sense?

                    Write a Reply...