Community Forums 

Main Content

Quick help, need to set an expiration time on cookies

    Jul 23 2010 00:17:27

    #1

    Nate

    Join date : 2010-05-20      Posts : 28

    I'm doing the "Setting up a Summary Cart on Your Site with PHP" that's linked in Mal's help section (http://projects.jesseheap.com/all-projects/setting-up-a-summary-cart-on-your-site-with-php)

    I've got everything working but says you should set the summary cart's cookie to expire to match Mal's cart expiration which is 1 hour I think?

    If I'm not mistaken the code is: setcookie(time()+3600);
    i just don't know where it should go or if that's the right code?

    < ? php
    $resetCart="no";
    if (isset($_GET['r'])) {
    setcookie ("qty", 0);
    setcookie ("tot", 0);
    $resetCart="yes";
    }
    elseif (isset($_POST['qty']))
    {
    setcookie("qty", $_POST['qty']);
    setcookie("tot", $_POST['tot']);
    }
    ? >



    Jul 23 2010 05:20:34

    #2

    GT

    Join date : 2008-09-18      Posts : 3243

    Having trouble with a form or need one building?
    Fast turnaround, but I do charge :)

    Mal's Forms / Scripts and the syntax are here.




    Jul 23 2010 05:29:56

    #3

    Nate

    Join date : 2010-05-20      Posts : 28

    Yea I tried his way but thought the other method explained it a little better, it was easier for me. I think I've got mine working, just want to see if someone can confirm this looks ok:

    < ? php
    $resetCart="no";
    if (isset($_GET['r'])) {
    setcookie ("qty", 0, time()+3600);
    setcookie ("tot", 0, time()+3600);
    $resetCart="yes";
    }
    elseif (isset($_POST['qty']))
    {
    setcookie("qty", $_POST['qty'], time()+3600);
    setcookie("tot", $_POST['tot'], time()+3600);
    }
    ? >



    Jul 24 2010 08:09:22

    #4

    Nate

    Join date : 2010-05-20      Posts : 28

    GT said I use this method: http://www.tne.co.uk/mals/viewcart.php

    He has time()-86400 on the first 2 cookies, what is that doing exactly? Should I set mine like that or have them all set for +3600?



    Jul 24 2010 15:13:17

    #5

    anon

    Join date : 2008-09-18      Posts : 488

    Setting them to time()-86400 expires (deletes) the cookies. Any time in the past should expire them, but -86400 removes all doubt.

    Taking a closer look at your code, it seems incomplete. You need to set some php variables to display the cart contents. You can't do it from the cookies you set because you don't have access to them until the next time the page is loaded. Using the code from GT's site addresses that.
    (P.S. I'm the one who provided the code to GT)



    Jul 24 2010 15:34:15

    #6

    Nate

    Join date : 2010-05-20      Posts : 28

    Here is the code I'm using for the show cart contents so if you can look it over I'd appreciate it. I'm using Wordpress & my site is www.rockitpro.com, so I've got code in my header.php and my sidebar.php files.

    I put this code in my HEADER.PHP:
    <?php
    $resetCart="no";
    if (isset($_GET['r'])) {
    setcookie ("qty", 0, time()+3600);
    setcookie ("tot", 0, time()+3600);
    $resetCart="yes";}
    elseif (isset($_POST['qty']))
    {
    setcookie("qty", $_POST['qty'], time()+3600);
    setcookie("tot", $_POST['tot'], time()+3600);}
    ?>


    I put this code in my SIDEBAR.PHP:
    <img src="http://www.rockitpro.com/smallcart.jpg"; align="left" height="16" width="20" alt="View Cart" />
    <h1 style="font-size:75%;color:#009aff">&nbsp;Items:
    (<?php
    //Gather variables for display
    if (isset($_POST['qty'])) { $qty = number_format($_POST['qty'],0); $tot = "$" . $_POST['tot']; }
    elseif (isset($_COOKIE['qty']) && $resetCart!="yes") {
    $qty = number_format($_COOKIE['qty'],0); $tot = "$" . $_COOKIE['tot']; }
    else
    {
    $tot="$" . 0;
    $qty=0;
    }
    echo $qty; ?>)
    - <?php echo $tot; ?></h1>