PHP - Reading and Writing Session Data

Introduction

You store all your session data as keys and values in the $_SESSION[] super global array.

So you might store the user's first name using:

$_SESSION[" firstName" ] ="Tom" ;

You could then display the user's first name - whether in the same page request or during a later request - as follows:

echo($_SESSION[" firstName" ]);

You can store any type of data in sessions, including arrays and objects:

$userDetails = array("firstName" =>  "Tom" ,"lastName" =>  "Smith" ,"age" => 34);
$_SESSION[" userDetails" ] = $userDetails;

Example

In this example, you use sessions to build a very simple shopping cart for an online store.

Save the following code as shopping_cart.php and run the script in your Web browser.

<?php
session_start();

class Product {
  private $productId;
  private $productName;
  private $price;

  public function __construct($productId, $productName, $price) {
    $this->productId = $productId;
    $this->productName = $productName;
    $this->price = $price;
  }

  public function getId() {
    return $this->productId;
  }

  public function getName() {
    return $this->productName;
  }

  public function getPrice() {
    return $this->price;
  }

}

$products = array(
  1 =>  new Product(1,"Phone" , 19.99),
  2 =>  new Product(2,"PC" , 29.99),
  3 =>  new Product(3,"Keyboard" , 39.99)
);

if (!isset($_SESSION[" cart" ])) $_SESSION[" cart" ] = array();

if (isset($_GET[" action" ]) and $_GET[" action" ] =="addItem" ) {
  addItem();
} elseif (isset($_GET[" action" ]) and $_GET[" action" ] =="removeItem" ) {
  removeItem();
} else {
  displayCart();
}

function addItem() {
  global $products;
  if (isset($_GET[" productId" ]) and $_GET[" productId" ] >= 1 and $_GET[" productId" ]  <= 3) {
    $productId = (int) $_GET[" productId" ];

    if (!isset($_SESSION[" cart" ][$productId])) {
      $_SESSION[" cart" ][$productId] = $products[$productId];
    }
  }

  session_write_close();
  header(" Location: shopping_cart.php" );
}

function removeItem() {
  global $products;
  if (isset($_GET[" productId" ]) and $_GET[" productId" ] >= 1 and $_GET[" productId" ]  <= 3) {
    $productId = (int) $_GET[" productId" ];

    if (isset($_SESSION[" cart" ][$productId])) {
      unset($_SESSION[" cart" ][$productId]);
    }
  }

  session_write_close();
  header(" Location: shopping_cart.php" );
}

function displayCart() {
  global $products;
?>
 <html>
   <head>
     <title> A shopping cart using sessions </title>
     <link rel="stylesheet" type="text/css" href="common.css" />
   </head>
   <body>
     <h1> Your shopping cart </h1>
     <dl>
    <?php
    $totalPrice = 0;
    foreach ($_SESSION[" cart" ] as $product) {
      $totalPrice += $product->getPrice();
    ?>
       <dt>  <?php echo $product->getName() ?>  </dt>

       <dd> $ <?php echo number_format($product->getPrice(), 2) ?>
                <a href="shopping_cart.php?action=removeItem & productId= <?php echo $product->getId() ?>"> Remove </a>  </dd>
          <?php } ?>
                <dt> Cart Total: </dt>
                <dd>  <strong> $ <?php echo number_format($totalPrice, 2) ?>  </strong>  </dd>
              </dl>

              <h1> Product list </h1>

              <dl>
          <?php foreach ($products as $product) { ?>
                <dt>  <?php echo $product->getName() ?>  </dt>
                <dd> $ <?php echo number_format($product->getPrice(), 2) ?>
                <a href="shopping_cart.php?action=addItem & amp;productId= <?php echo
         $product->getId() ?>"> Add Item </a>  </dd>
          <?php } ?>
              </dl>

          <?php
         }
?>

   </body>
</html>

Related Topic