Using Session array to maintain data across the pages

Array can store more items or data in a single variable but these are not available in different pages for use. Any ordinary ( or normal ) array will loose its data as page execution ends. We have to transfer the array each time the visitor moves away from one page to other. In some applications we need to retain the data as long as the visitor is active at site. Shopping cart is the best example for this. Visitor moves between different pages and adds items to the shopping cart. We have to carry the shopping cart to all the pages along with the visitor. Visitor can add or remove items from the cart from any page.

Demo shopping cart using session array

We need a session array to retain the data in different pages. Session arrays are like session variables which maintain a unique link between user's web page and the server. You can read more on session management here.

Related Tutorial
PHP Session
PHP Cookies
Let us start with declaring a session array. Before that we have to start the session by adding this line at the top of the page
Next, any where inside the page we can declare the array (if not done before ) before using. ( Start the shopping cart )
Here we have declared a session array. We can add elements to it by array_push function.
We can remove items from the array by using array_diff function.
This way we can add or remove items from out session array. We will use this in our shopping cart script.

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
Visitors Rating
Your Rating




How max limit of the Array Session and Array? Where can i test if the one variable is more than the max. limir? Thank.


$_SESSION is it self array or we have to make it array


Yes , you can loop through and display all elements of the session array check the last part.
uday kiran


if i m writing array_push($_SESSION[cart],$prod_id);
in this manner the second pushed element is overwriting the first element


No that can never happen,
array_push($_SESSION['cart'],'apple','mango','banana'); // Items added to cart
array_push($_SESSION['cart'],'Orange'); // Items added to cart

Second line ads fourth element to the array of cart, it is not removing any item.


uday kiran is right, though it is not actually overwriting.
Every time the array is declared, a new empty array is created, so array_push() is in fact pushing the first $prod_id to that empty array.
This can be avoid by declaring the array only if it was not already set:



No that is not correct. To make it clear one more page cart-add.php is added to the script. This page shows one input box and user can enter product through this to the cart. This will not overwrite any previously stored elements in the cart. It is adding a new one.

This explanation is added in demo script also. You can download a fresh copy of the script and check.

Note that we are declaring the array only once, in cart.php page. This is not to be declared in any other page. If you want to destroy this cart then you can use

Check the demo page.

Post Comment This is for short comments only. Use the forum for more discussions.

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2018 All rights reserved worldwide Privacy Policy Disclaimer