SQL PHP HTML ASP JavaScript articles and free scripts to download
 

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.

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.

$_SESSION[cart]=array();

Here we have declared a session array. We can add elements to it by array_push function.

array_push($_SESSION[cart],$prod_id);

We can remove items from the array by using array_diff function.

$_SESSION[cart]=array_diff($_SESSION[cart],$prod_id);

This way we can add or remove items from out session array. We will use this in our shopping cart script.




restroika29-05-2010
How max limit of the Array Session and Array? Where can i test if the one variable is more than the max. limir? Thank.
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked


Join Our Email List
Email:  
For Email Newsletters you can trust
Array Functions
PHP Sections