0% found this document useful (0 votes)
61 views13 pages

Ch.9a Session

The document discusses session management in PHP. It provides examples of starting a session with session_start(), registering session variables, accessing and destroying session variables, and passing the session ID in the query string. Key functions covered include session_start(), session_register(), session_encode(), session_destroy(), and session_unset().

Uploaded by

wfscmgbh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views13 pages

Ch.9a Session

The document discusses session management in PHP. It provides examples of starting a session with session_start(), registering session variables, accessing and destroying session variables, and passing the session ID in the query string. Key functions covered include session_start(), session_register(), session_encode(), session_destroy(), and session_unset().

Uploaded by

wfscmgbh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Session Management

By Tony Chun Kuen WONG


15/03/2010

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’];
_
}
?>

Connecting to specific user


Session support in PHP consists of a way to
preserve certain data across subsequent
accesses. This enables you to build more
customized applications and increase the
appeal of your web site.

A visitor accessing your web site is assigned


an unique idid, the so-called
so called session id
id. This is
either stored in a cookie on the user side or is
propagated
p p g in the URL.
The session support allows you to register
arbitraryy numbers of variables to be
preserved across requests. When a visitor
accesses y your site, PHP will check
automatically (if session.auto_start is set to 1)
or on yyour request
q ((explicitly
p y through
g
session_start() or implicitly through
session_register())
_ g ()) whether a specific
p
session id has been sent with the request. If
this is the case, the prior saved environment
is recreated.

Starting a session with session_start


session start ( )
http://isem03.hkbu.edu.hk/~isemuser20/session/s
ession02.php
Listing (session02.php)
<?php
session_start();
?>
<html>
<head>
<title> Session 02 example by Tony WONG - Start or
resume a session</title>
</head>
<body>
<?php
print "<p>Welcome, your session ID is
".session_id()."</p>\n\n";
?>
</body>
</html>
W ki with
Working ith S
Session
i V Variables
i bl

session_register ( ) - Register one or more variables with


the current session.

session_register( ) variable number of arguments, any of


which can be either a string holding the variable name
or an array consisting of such variable names or other
arrays. For each encountered variable name,
session_register( ) registers the global variable named by
it with the current session.

This function returns true when the variable is


successfullyy registered
g with the session.

Register variables – session03.php


http://isem03.hkbu.edu.hk/~isemuser20/session/session03.php
<?php
session_start();
?>
<html>
<head>
<title> Session 03 example by Tony WONG -
registering variables with a session</title>
</h d>
</head>
<body>
<?php
session register(‘product1’);
session_register(‘product1’);
session_register(‘product2’);
$_SESSION[‘product1’] = "Tone dream car";
$ SESSION[‘product2’]
$_SESSION[ product2 ] = "Nissian
Nissian Bluebird
Bluebird";
;
print session_encode();
print “<br>The products have been registered";
?>
</body>
</html>
Session_encode ( )
session_encode ( ) - Encodes the
current session data as a string

session_encode() returns a string with


th contents
the t t off the
th currentt session
i
encoded within.

Accessing registered variables – session04.php


session04 php
http://isem03.hkbu.edu.hk/~isemuser20/session/session04.php
<?php
session_start();
?>
<html>
<head>
<title> Session 04 example by Tony WONG -
accessing registered variables</title>
</head>
<body>
<?php
print "Your chosen products are :\n\n";
print
"<ul><li>”.$_SESSION[‘product1’].”\n<li>”.$_SES
"<ul><li>” $ SESSION[‘product1’] ”\n<li>” $ SES
SION[‘product2’].”\n</ul>\n";
?>
</b d >
</body>
</html>
Session variables are store in a writable
directory in the isem03.hkbu.edu.hk

Record multiple products – session05.php


http://isem03.hkbu.edu.hk/~isemuser20/session/session05.php

<?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

session_destroy() destroys all of the data


associated with the current session
session.
This function returns true on success and
f l on failure
false f il t destroy
to d t the
th session
i ddata.
t

The session_unset( ) function free's all


session variables currently
y registered.
g

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

What happen if a browser don’t accept


cookies;;

Then you need


Th d tto pass th
the session
i ID b
by
yourself by

<a href = “nextpage


nextpage.html?<?
html?<? Print SID; ?>Next page</a>

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.

The session name references the session id


in cookies and URLs
URLs. It should contain only
alphanumeric characters; it should be short
and descriptive
p ((i.e. for users with enabled
cookie warnings). The session name is reset
to the default value stored in session name at
request startup time. Thus, you need to call
session_name( ) for every request (and
b f
before session_start(
i t t( ) or
session_register( ) are called).
session_unregister(
i i t ( ) unregisters
i t
(forgets) the global variable named
name from the current session.

This function returns true when the


variable is successfully unregistered
from the session.

session_is_registered(‘products’
i i i t d(‘ d t ’)

returns true if there is a variable with the


name products
prod cts registered in the ccurrent
rrent
session.
session_decode(
i d d ()

decodes the session data in data,


setting variables
ariables stored in the session
session.

Last example

session08.php
Examples using session
User information
Shopping Cart

You might also like