Ch.9a Session
Transcript of Ch.9a Session
-
8/9/2019 Ch.9a Session
1/13
Session Management
By Tony Chun Kuen WONG
Example 1
.
http://isem03.hkbu.edu.hk/~isemuser20/s.
-
8/9/2019 Ch.9a Session
2/13
Connecting to specific user
Session support in PHP consists of a way to
accesses. This enables you to build more
appeal of your web site.
A visitor accessing your web site is assigned- , .
either stored in a cookie on the user side or isro a ated in the URL.
-
8/9/2019 Ch.9a Session
3/13
The session support allows you to registerarbitrary numbers of variables to bepreserved across requests. When a visitor
accesses your site, PHP will checkautomatically (if session.auto_start is set to 1)or on your request (explicitly throughsession_start() or implicitly through
session_register()) whether a s ecificsession id has been sent with the request. If
this is the case, the prior saved environmentis recreated.
_
http://isem03.hkbu.edu.hk/~isemuser20/session/session02.php
Listing (session02.php)
Session 02 example by Tony WONG - Start or
resume a session
-
8/9/2019 Ch.9a Session
4/13
or ng w ess on ar a es
session_register ( ) - Register one or more variables with
the current session.
session_register( ) variable number of arguments, any of
or an array consisting of such variable names or otherarrays. For each encountered variable name,session_register( ) registers the global variable named byit with the current session.
This function returns true when the variable issuccessfull re istered with the session.
Register variables session03.phphttp://isem03.hkbu.edu.hk/~isemuser20/session/session03.php
Session 03 example by Tony WONG -
registering variables with a session
< ea >
-
8/9/2019 Ch.9a Session
5/13
Session_encode ( )
session_encode ( ) - Encodes thecurrent session data as a string
session_encode() returns a string withthe contents of the current session
encoded within.
.http://isem03.hkbu.edu.hk/~isemuser20/session/session04.php
session_start();?>
Session 04 example by Tony WONG -
print "Your chosen products are :\n\n";printu ._ pro uc . n ._
SION[product2].\n\n";?>
o y
-
8/9/2019 Ch.9a Session
6/13
Session variables are store in a writabledirectory in the isem03.hkbu.edu.hk
Record multiple products session05.phphttp://isem03.hkbu.edu.hk/~isemuser20/session/session05.php
< >
Session 05 example by Tony WONG - registeran array variable o y
Product Choice Page
-
8/9/2019 Ch.9a Session
7/13
Session05.php continue
Nissian Bluebird
Honda Civic
Toyota corolla Benz
Landrover
< select>
orm
=" ".
Accessin Session variables session06. h Session 06 example by Tony WONG - accessingsession variables
A Content PageBack to product choice page
-
8/9/2019 Ch.9a Session
8/13
es roy ng sess ons an nse ng var a es
session_destroy() destroys all of the data.
This function returns true on success anda se on a ure to estroy t e sess on ata.
The session_unset( ) function free's all
session variables currentl re istered.
m Session07.php by Tony WONG - destroying andunsetting variables
Demo destroy and unset variables
-
8/9/2019 Ch.9a Session
9/13
print "session start again
";sess on_reg s er new es ;$_SESSION[newtest] = 10;print "
";"_ . before unset and destroy
";print session_encode()." session variable"
print "
";session_unset();session destro_print $_SESSION[newtest]." value of variableafter after unset and destroy
";rint session encode ." session variable_reocrded";print "
";print "end of testing
";?>ass ng ess on s n e query s r ng
What happen if a browser dont acceptcookies;
en you nee to pass t e sess on yyourself by
.
.
-
8/9/2019 Ch.9a Session
10/13
er sess on unc ons
_ re urns e name o ecurrent session. If name is specified, the
value.
The session name references the session id.
alphanumeric characters; it should be shortand descri tive i.e. for users with enabledcookie warnings). The session name is resetto the default value stored in session name at
request startup time. Thus, you need to callsession_name( ) for every request (ande ore sess on_s ar or
session_register( ) are called).
-
8/9/2019 Ch.9a Session
11/13
sess on_unreg ster unreg sters(forgets) the global variable namednamefrom the current session.
This function returns true when thevariable is successfully unregisteredfrom the session.
sess on_ s_reg stere pro ucts
returns true if there is a variable with the
name pro uc sreg s ere n e currensession.
-
8/9/2019 Ch.9a Session
12/13
sess on_ eco e
decodes the session data in data,se ng var a es s ore n e sess on.
Last example
session08.php
-
8/9/2019 Ch.9a Session
13/13
Examples using session
User informationShopping Cart