0
I’m looking to add a value to a GET session. I’m doing it this way but it doesn’t work.
function addCart($product){
$cart['cart'][$product] = 0;
$_SESSION[] = $cart;
}
0
I’m looking to add a value to a GET session. I’m doing it this way but it doesn’t work.
function addCart($product){
$cart['cart'][$product] = 0;
$_SESSION[] = $cart;
}
1
Example:
function addCart($product){
session_start();
$_SESSION['cart'][] = $product;
}
Don’t forget to use the session_start()
, can even be out of function at the beginning of the PHP document body.
References:
http://php.net/manual/en/function.session-start.php https://stackoverflow.com/questions/8259149/how-to-put-array-into-session http://www.w3schools.com/php/php_sessions.asp http://php.net/manual/en/reserved.variables.session.php
despite the need to log in, call the function session_start()
inside another function does not seem a good idea, if the function is called more than once it will cause error.
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
Doesn’t work means what? that the new item goes with a numerical input?
– rray
the variable
$cart
is not set and causes error, you need to check if it exists, and create conditions for whether or not it exists– Pedro Sanção
It didn’t work. What I wanted was to store the ids of a product in the session, go incrementing them every get.
– Ryan Felipe
Ryan, I suggest you also give a
name
for your$_SESSION[]
– Thyago ThySofT