On your product page you initialize the session, make sure you have session_start[]
above your html tag.
Then you need to give the session tags a value, like $_SESSION["cart"] = "";
You also need something to add everything in 1 array so you can check the array later on your shopping cart page.
use array_push for that, create an array when first creating the session. Then use array_push to add items to the cart.
Outputs: Red, Green, Blue, Yellow
Use this to add products to the array. On your shopping cart page you just simply count how many items are in the array. Make a loop to get them all out and you're done!
[PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8]
$_SESSION — Session variables
Description
An associative array containing session variables available to the current script. See the Session functions documentation for more information on how this is used.
Notes
Note:
This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods.
Tugrul ¶
7 years ago
Creating New Session
==========================
Getting Session
==========================
Updating Session
==========================
Deleting Session
==========================
2nd page
---------------------------------------------------------------
[Valid]
1st page
2nd page
---------------------------------------------------------------
[Valid]
1st page
2nd page
---------------------------------------------------------------
Example from PHP.net manual on Session variables
Source: //php.net/manual/en/language.types.array.php
Miller ¶
9 years ago
I wrote a little page for controlling/manipulating the session. Obviously, never use this on a production server, but I use it on my localhost to assist me in checking and changing session values on the fly.
Again, it makes use of eval[] and exposes the session, so never use this on a web server.