Ch.9a Session
Ch.9a Session
Example 1
Session01 php
Session01.php
http://isem03.hkbu.edu.hk/~isemuser20/s
ession/session01 php
ession/session01.php
<?php
session_start();
session_register(‘count’);
if (!isset($_SESSION[
(!isset($ SESSION[‘count’]))
count ]))
{
$_SESSION[‘count’]=0;
echo "<li>Counter initialized, please
reload this page to see it increment";
}
else
{
echo "<li>waking up session”.$_COOKIE[‘PHPSESSID’];
$_SESSION[‘count’]++;}
echo "<li>The counter is now “.$_SESSION[‘count’];
_
}
?>
<?php
session_start();
?>
<html>
<head>
<title> Session 05 example by Tony WONG - register
an array variable </title>
</head>
<body>
<h1>Product Choice Page</h1>
<?php
_ _
if (isset ($_POST[‘form_products’]))
{
$_SESSION[‘products’] = $_POST[‘form_products’];
print "<p>Your products have been
registered !</p>";
!</p> ;
}
?><p>
Session05.php continue
<form method = "post">
_
<select name = "form_products[]" multiple size
= 3>
<option> Nissian Bluebird
<option> Honda Civic
<option> Toyota corolla
<option> Benz
<option> Landrover
</ l t>
</select>
</p><p>
<input type = "submit" value = "choose">
</form>
</p>
</form>
<A HREF="session06
HREF= session06.php
php">A
>A content page</A>
</body>
</html>
Accessing
<? h
<?php
g Session variables – session06.php
p p
session_start();
print session_encode();
?>
<html>
h l
<head>
<title> Session 06 example by Tony WONG - accessing
session variables </title>
</head>
/
<body>
<h1>A Content Page</h1>
<?php
if (isset ($_SESSION[‘products’]))
{
print "<b>Your cart : </b><ol>\n";
foreach ($_
($ SESSION[‘products’]
[ p ] as $p => $
$models)
)
print "<li>$models";
print "</ol>";
}
?>
<A HREF="session05.php">Back to product choice page</A>
</body>
</html>
Destroying sessions and Unsetting variables
session07.php
<?php
session_start();
?>
<ht l>
<html>
<head>
<title> Session07.php by Tony WONG - destroying and
unsetting variables </title>
</head>
<body>
<h1>Demo destroy and unset variables</h1>
<?php
session_register (‘test’);
$_SESSION[‘test’] = 5;
print $_SESSION[‘test’]." value of variable before
destroy<br>";
print session_encode()." session variable reocred";
print "<br><br>";
session_destroy ( );
print $_SESSION[‘test’]."
$ value of varialbe after destroy
<br> <br>";
print session_encode()." session variable reocrded";
print "<br><br>";
p ;
print "session start again <br>";
session_register
i i t (‘
(‘newtest’);
t t’)
$_SESSION[‘newtest’] = 10;
print "<br>";
print $_SESSION[‘newtest’]."
$ SESSION[‘newtest’] " value of variable
before unset and destroy<br>";
print session_encode()." session variable
reocrded ;
reocrded";
print "<br><br>";
session_unset();
session destroy ( );
session_destroy
print $_SESSION[‘newtest’]." value of variable
after after unset and destroy <br> <br>";
print session_encode()."
p () session variable
reocrded";
print "<br><br>";
print "end of testingg <br>";
?>
</body>
</html>
P
Passing
i S Session
i IdIds iin th
the query string
ti
Example : ncsession05.php
ncsession05 php
Oth session
Other i functions
f ti
session_name(
session name( ) returns the name of the
current session. If name is specified, the
name of the current session is changed to its
value.
session_is_registered(‘products’
i i i t d(‘ d t ’)
Last example
session08.php
Examples using session
User information
Shopping Cart