0
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 }; ?>
3
Contributors
9
Replies
12
Views
5 Years
Discussion Span
Last Post by waqar3
0

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?)

Edited by diafol

0

thanks diafol can you explain full to correct my code.

Edited by waqar3

0

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.

0

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.

0

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.

0

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.

0

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.

0

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!

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.