Tutorial PHP Session Array()

in #utopian-io8 years ago (edited)

Tutorial PHP Session Array()

PHP Sessions Array

The problem of storing user information while the user is accessing the web site is solved with sessions in PHP. Essentially, when the user logs into a web site and enters some information, the server assigns the user a 32 character, random, session ID which uniquely identifies the user. It stores this ID on the users computer as a cookie, along with time-out expiration information.

To start a session you call session_start() at the top of the web page file, before anything else. Since your storing the session ID in a cookie, session_start has to be called before any new line in the web page file.

When session_start() is called PHP checks to see if a session has already been started, and if it hasn't, it will assign that user a session ID and store it in a cookie. It also sets up a unique global session array on the server, identified by, you guessed it, the session ID.

PHP keeps session variables in a $_SESSION[] array. This array is available globally, which means it doesn't matter which page of the web server application you go to, the information will be available, which is what we want.

Below is the example to store information in session array.

session_start();
$_SESSION['firstname'] = $fname ;
$_SESSION['lastname'] = $lname ;
$_SESSION['usercity'] = $city ;
$_SESSION['address'] = $addr ;

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.

We will be using array commands to manage our shopping cart. We will be using three files. The very first file cart.php is for declaring the array and adding elements.

At the starting of each page we will keep session_start() command. Here is the code for cart.php file .

< ?Php
session_start();
?>
< !doctype html public "-//w3c//dtd html 3.2//en">
< html>
< head>
< title>Demo of Session array used for cart from plus2net.com< /title>
< /head>
< body>
< ?Php
$_SESSION['cart']=array(); // Declaring session array
array_push($_SESSION['cart'],'apple','mango','banana'); // Items added to cart
echo "Number of Items in the cart = ".sizeof($_SESSION['cart'])." < a href=cart-remove-all.php>Remove all< /a>< br>";
?>
< /body>
< /html>

In the above code we used array_push() to add products to our shopping cart array. We have also used sizeof to count total number of elements present in our array.

We can display all elements or products present inside the array cart by using while loop. Here is the next file cart-display.php for displaying all elements of the array.

< ?Php
session_start();
? >
< !doctype html public "-//w3c//dtd html 3.2//en">
< html>

< head>

< title > Displaying Session Cart products from plus2net.com < /title >
< /head>
< body >
< ?Php
echo "Number of Items in the cart = ".sizeof($_SESSION['cart'])." < a href=cart-remove-all.php >
Remove all< /a>< br >";
while (list ($key, $val) = each ($_SESSION['cart'])) {
echo "$key -> $val
";
}
?>
< /body>
< /html>

We will use unset command to remove elements from the cart. After removal we will display the total number of products available in the cart.cart-remove-all.php is used for unsettling from cart.

< ?Php
session_start();
?>
< !doctype html public "-//w3c//dtd html 3.2//en">
< html>

< head>
< title>Session Cart removal by plus2net.com
< /head>
< body>
< ?Php
while (list ($key, $val) = each ($_SESSION['cart'])) {
//echo "$key -> $val
";
unset($_SESSION['cart'][$key]);
}
echo "Number of Items in the cart = ".sizeof($_SESSION['cart'])." < br>";
? >
< /body>
< /html>

We can add a feature to remove elements from the cart based on user selection. For this we need to add one more page. We will say cart-remove.php.

< ?Php
session_start();
?>
< !doctype html public "-//w3c//dtd html 3.2//en">
< html>
< head>
< title>Session Cart removal on selection by user at plus2net.com
< /head>
< body>
< ?Php
$item=$_POST['item'];
while (list ($key1,$val1) = @ each ($item)) {
//echo "$key1 , $val1,
";
unset($_SESSION['cart'][$val1]);

}
echo "Number of Items in the cart = ".sizeof($_SESSION['cart'])."
";
echo "";
while (list ($key, $val) = each ($_SESSION['cart'])) {
echo " $key -> $val
";
}
echo "< input type=submit value=Remove>< /form>";
?>
< a href=cart.php>Cart adding . < a href=cart-display.php>Display Items< /a> .< a href=cart-remove.php>Remove Item< /a>
< /body>
< /html>

Example:

1.png

2.png



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Oo so thats how that works.

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Your contribution cannot be approved yet. See the Utopian Rules. Please edit your contribution to reapply for approval.
Check the Completeness of Writing HTML tag tittle and check the php tag <? Php

You may edit your post here, as shown below:

You can contact us on Discord.
[utopian-moderator]

I editted :)

Hey @brothermic I am @utopian-io. I have just upvoted you!

Achievements

  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.13
TRX 0.34
JST 0.036
BTC 109587.97
ETH 4474.98
USDT 1.00
SBD 0.84