Registering an Array Variable with a Session : Session Variables « Cookie Session « PHP






Registering an Array Variable with a Session

 
<?php
session_start();

if ( empty( $_SESSION['products'] ) ) {
  $_SESSION['products']=array();
}

if ( is_array( $_REQUEST['form_products'] ) ) {
  $_SESSION['products'] = array_unique(
    array_merge( $_SESSION['products'],
          $_REQUEST['form_products'] )
  );
}
?>
<html>
<head>
<title>Registering an Array Element with a Session</title>
</head>
<body>
<div>
<form action="<?php print $_SERVER['PHP_SELF']?>" method="post">
<p>
<select name="form_products[]" multiple="multiple" size="3">
<option>A</option>
<option>B</option>
<option>C</option>
<option>D</option>
<option>E</option>
</select>
</p>
<p>
<input type="submit" value="choose" />
</p>
</form>
<a href="index.php">A content page</a>
</div>
</body>
</html>
  
  








Related examples in the same category

1.A session variable can be destroyed with a call to session_unregister().
2.Accessing Session Variables
3.Adding Session Data
4.Checking Session Data
5.Reading Session Data
6.Registering Variables with a Session
7.Storing Complex Data Types in Sessions
8.Storing Complex Data Types to session
9.Storing Simple Data Types in Sessions