divinedesigns1 Posted March 1, 2016 Share Posted March 1, 2016 how can i make this code work with cookie? I would like it to last longer with the cookies, the session only last for a short period of time if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { // RUN IF THE CART IS EMPTY OR NOT SET $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1)); setcookie("id", $_SESSION["cart_array"], strtotime( '+1 day' ), "/", "", "", TRUE); setcookie("quantity", "1", strtotime( '+1 day minute' ), "/", "", "", TRUE); } any tips, hints or advice are acceptable as always. HELP PLEASE Link to comment Share on other sites More sharing options...
justsomeguy Posted March 1, 2016 Share Posted March 1, 2016 Check the return value of setcookie, and make sure you have all error messages enabled. Otherwise, you need to be more specific about what exactly the problem is. And what is "1 day minute"? Note also that both of the last parameters to setcookie are booleans. Link to comment Share on other sites More sharing options...
divinedesigns1 Posted March 1, 2016 Author Share Posted March 1, 2016 now im having these errors where the setcookie is, Warning : Cookie names cannot contain any of the following '=,; \t\r\n\013\014' in /home/content/14/8709614/html/ddsnet/functions.php on line 222 Warning : Cannot modify header information - headers already sent by (output started at /home/content/14/8709614/html/ddsnet/cart.php:39) in /home/content/14/8709614/html/ddsnet/functions.php im looking it up at the moment, but it makes no sense since thats where the setcookies are foreach($_SESSION['cart_array'] as $each_item){ $item = $each_item['id']; @$item_price = $each_item['price']; // get the item information $st_sql = mysqli_query($con, "SELECT * FROM products WHERE id='$item' LIMIT 0, 1") or die('Error: ' . mysqli_error($con)); while($row = mysqli_fetch_array($st_sql)){ $pname = $row['pname']; $price = $row['Proprice']; $usedprice = $row['usedPrice']; $image = $row['id'] . '.' . $row['ext']; $detail = $row['idetail']; $_SESSION['item_name'] = $pname; $_SESSION['item_price'] = $price; $_SESSION['item_usedprice'] = $usedprice; $_SESSION['item_detail'] = $detail; setcookie($pname, $pname, time() + (86400 * 7)); setcookie($price, $price, time() + (86400 * 7)); setcookie($usedprice, $usedprice, time() + (86400 * 7)); setcookie($detail, $detail, time() + (86400 * 7)); } im lost on this one, hint tips advice will do Link to comment Share on other sites More sharing options...
Ingolme Posted March 2, 2016 Share Posted March 2, 2016 It sounds like $pname, $price, $usedprice or $details have characters that are not permitted in cookie names. Make the name of the cookie something recognizable, you probably shouldn't use dynamic values for it. The "Headers already sent" error was just caused by the first error message. Link to comment Share on other sites More sharing options...
divinedesigns1 Posted March 2, 2016 Author Share Posted March 2, 2016 It sounds like $pname, $price, $usedprice or $details have characters that are not permitted in cookie names. Make the name of the cookie something recognizable, you probably shouldn't use dynamic values for it. The "Headers already sent" error was just caused by the first error message then ill just leave it as a session, because thats the only issue that is stopping it from working Link to comment Share on other sites More sharing options...
Ingolme Posted March 3, 2016 Share Posted March 3, 2016 Why don't you give a fixed name to the cookies? What exactly is your code supposed to do? A cookie is like a variable, you don't dynamically generate variables, they have specific names from the start. Try this instead: setcookie('pname', $pname, time() + (86400 * 7)); Link to comment Share on other sites More sharing options...
divinedesigns1 Posted March 9, 2016 Author Share Posted March 9, 2016 Why don't you give a fixed name to the cookies? What exactly is your code supposed to do? A cookie is like a variable, you don't dynamically generate variables, they have specific names from the start. Try this instead: setcookie('pname', $pname, time() + (86400 * 7)); you know i did that first lol then i change it to variables, ill try that and see what happens Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now