Hi,
   i want to create cookies in my shopping cart site. I want when i click on add to cart button of
   this product. the information of the product save in cookies and destroy when user close his browser.
   I want a script. When i create cookies it's work only for one time after reload or refresh the page
   the nulled cookies generate automatically and my product information replace with null information.
    here are my code.

    <?php
    error_reporting(E_ALL ^ E_NOTICE);  
    include("connection.php");

    setcookie('pro_id', $_REQUEST['pro_id']);
    setcookie('pro_id', $_REQUEST['pro_id'], time()+3600);
    setcookie('title', $_REQUEST['title']);
    $pro_id = $_COOKIE['pro_id'];
    $title = $_COOKIE['title'];

                $Special_Product_Sql = "SELECT * FROM product WHERE status = 1 && featured = 1";
                $Special_Product_Exe = mysql_query($Special_Product_Sql);

                while($Product_Fetch = mysql_fetch_array($Special_Product_Exe)) {

                $path = 'product/'. $Product_Fetch['product_img'];
            ?>

    <form method="get" action="<?php echo $_SERVER['PHP_SELF']?>" onsubmit="storeValues(form)" name="form">
        <input type="hidden" value="<?php echo $Product_Fetch['pro_id']; ?>" name="pro_id">
        <input type="hidden" value="<?php echo $Product_Fetch['title']; ?>" name="title">
        <input type="hidden" value="<?php echo $Product_Fetch['description']; ?>" name="field3">
        <input type="hidden" value="<?php echo $Product_Fetch['cat_id']; ?>" name="field4">
        <input type="submit" title="Add to Cart" value="Add to Cart" class="addtocart_button_module">
    </form>

    <?php }; ?>
Member Avatar for diafol

setting cookies on localhost can sometimes be an issue:

setcookie('lang', 'cy', false, "/", false);

I have a routine that sniffs whether the server is production or localhost and applies the last three parameters accordingly. The above is for localhost. An obvious change for a dev environment would be to place an expiry in seconds for the third parameter.

Oh, BTW - you can't retrieve a cookie that you set on the same page. You can't read it until page refresh or similar (maybe ajax?)

thanks diafol can you explain full to correct my code.

Member Avatar for diafol

Killing cookies on leaving the site is difficult as they have an expiry time. You could use javascript to kill cookies with the onbreforeunload() function. Your best bet would be to use sessions ($_SESSION) to be able to carry data from one page to the next.

But there are few confusing after user close his browser how to destroy session automatically with delete product that user already add by clicking on cart button becuase i don't want to save these information in database.

Member Avatar for diafol

I don't see how clearing sessions should affect your db. If an user leaves your site and then comes back to find his basket empty, he'd me pretty cheesed off. The data in the DB can be cleared periodically after a set time with a cron job if needed.

you are right but sometime onliy user visit on our site and click on our product with alot of time without any purpose. There is creating affect on our site becuase our database take more and more space. Therefore i want to use this method. Any ways thanks for suggestion.

Member Avatar for diafol

you are right but sometime onliy user visit on our site and click on our product with alot of time without any purpose

Unfortunately, you won't know when this is without purpose.
Cookies are not secure and can be manipulated to anything you want. They are not meant to store 'loads' of data.
DB (IMO) is the fastest and most reliable method of propogating data from one page to the next. The data can be moved or 'flagged' to a permanent state when the user registers/logs in. It all depends on how you are storing your data. The session will expire at some point (store a 'last page impression'), so the temporary data in the DB will be orphaned and can be removed via cron job.

I would suggest you using sessions instead of cookies since they hold more space but lasts longer, but if you don't need any longer than a single webbrowser session then there is no need to use cookies. cookies can hold from when you close your browser session to as far as to year 3000!

Diafol thanks for discussion. Now i adopted your method in my website.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.