Jump to content

UPDATE $_SESSIONS


Omzy

Recommended Posts

Here is my SESSIONS array:

 

$_SESSION[$_POST['item_number']]=array($_POST['item_number'], $_POST['item_name'], $_POST['options'], $_POST['amount']);

 

Somewhere in this array I want to include a $quantity variable - the way the site is designed there is currently no quantity field on the product pages so I want to be able to set this on the checkout page as follows:

 

1) If the $_POST['item_number'] is POSTed for the first time, $quantity is set to 1.

 

2) If the same $_POST['item_number'] is POSTed again, the $quantity of that session item is increased

 

3) If the same $_POST['item_number'] is POSTed again but the $options variable is different then a new session item is added to $_SESSION

Link to comment
https://forums.phpfreaks.com/topic/176900-update-_sessions/
Share on other sites

It looks like it's not really item_number that defines an item, but "item_number + options".  So you might use a combination of both as a key, for example:

 

$key = $_POST['item_number'] . "|" . $_POST['options'];
$_SESSION['items'][$key] = array($_POST['item_number'], $_POST['item_name'], $_POST['options'], $_POST['amount']);

 

I've also added ['items'] into the session variable - the reason is that there may be a security issue by setting these items at the top level of $_SESSION.  If someone sends an unexpected value for item_number they might be able to overwrite other entries in $_SESSION.  If you know this won't be a problem then you can remove the ['items'].

Link to comment
https://forums.phpfreaks.com/topic/176900-update-_sessions/#findComment-932727
Share on other sites

Well the problem with item_number is it doesn't uniquely identify an item.  So the question is do you store your items as "widget|red" and "widget|green" or "widget" - "red" and "widget" - "green".

 

The first option is using a combined key of item number and options.

 

The second option would fit a 2 level array like this:

 

$_SESSION[$_POST['item_number']][$_POST['options']] = ...;

 

From a technical perspective, there's nothing wrong with using a long string as an array key.

 

 

Link to comment
https://forums.phpfreaks.com/topic/176900-update-_sessions/#findComment-932749
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.