SlideShare una empresa de Scribd logo
1 de 20
Sessions

      session_start() function
session_register(‘variable’) function
       $_SESSION[‘variable’]
Before you begin, check php.ini
; The file storage module creates files using mode 600 by default.
; You can change that by using
;
; session.save_path = "N;MODE;/path"
;
; where MODE is the octal representation of the mode. Note that this
; does not overwrite the process's umask.
; http://php.net/session.save-path
session.save_path = "c:/wamp/tmp“

For Windows, modify session.save_path so that a session
file can be written to a directory that exists. In the case of
WAMP, it already exists.
Check with Windows Explorer
For cs346.cs.uwosh.edu




/var/lib/php5 already exists and is empty
huen@CS346:/var/lib/php5$ sudo pwd
/var/lib/php5
huen@CS346:/var/lib/php5$ sudo ls -l
total 0
huen@CS346:/var/lib/php5$
What is a session?
• Time perspective:
• A session is the length of time during which a user
  visits a site
• Programming perspective:
   – A session is a big blob that can hold all sorts of variables
     and values
   – The blob has an identification string e.g.
     sess_fc48gdbuckuj9oe3ef74i00b01
   – The identification string is sent to the user when a session
     is initiated in a cookie called PHPSESSID (accessible via
     $_COOKIE[PHPSESSID]
   – On the server side, a matching temporary file is created
     with the same name (sess_fc48gdbuckuj9oe3ef74i00b01)
Session Variables
• Session variables and the corresponding values are
  stored in the temporary session file
• Database is not used – no connection nor query
  needed
• You may access the session variables through the
  $_SESSION superglobal
• Example: suppose session file contains:
   –   Count|s:7:”17”;
   –   Valid|s:7:”yes”;
   –   Count and Valid are session variables
   –   Count and Valid must first be added to $_SESSION
   –   Extract with $_SESSION[Count] and $_SESSION[Valid]
Operations to retrieve a session variable e.g.
              $_SESSION[Count]
• PHP engine gets the value of
  $_COOKIE[PHPSESSID] from the user cookie
• PHP engine finds a matching temporary
  session file
• Inside the session file, the PHP engine looks
  for Count variable and then extracts its value
  i.e. 17
• $_SESSION[Count] get the value 17
To start a session:
• Just call session_start() function
• PHP does the rest:
  – Sends the cookie to the user
  – Creates the temporary session file
• See m17/17-1session.php
Registering and Modifying Session Variables

•   See 17-2countme.php
<?php
session_start();
session_register('count');
/*
    register a variable called count
    After registering, as long as this session exists, a variable called
    $_SESSION[count]
    will be available. Initially it has no value.
 */
$_SESSION['count']++;
$msg ="<p>You've been here $_SESSION[count] times. Thanks!</p>";
?>
Accessing 17-2countme.php




Refresh the page 6 more times:
Session temporary file




Session temporary file contains count|i:7;
Getting more complicated . . .
• Managing user preferences with Sessions
  – Start a session
  – Ask the user for preferred font family and base
    font size
  – Display the preferences in subsequent pages
  – Allow the user to change the preferences and
    reset the values
  – See 17-3session01.php
Starting a session and Registering Defaults
//PHP file for the defaults/preferences + html to request
    preferences
<?php
//start a session
session_start();
/* Because the user may come back to reset, so we must
    check if previous values exist.
    If previous values of font-family and font_size are not
    defined, assign default values
    else extract the values from $_SESSION superglobal
*/
// Preparing for the CSS attributes
//check for stored values and register defaults
if ((!$_SESSION['font_family']) || (!$_SESSION['font_size'])) {

           $font_family = "sans-serif";
           $font_size = "10";

           $_SESSION['font_family'] = $font_family;

           $_SESSION['font_size'] = $font_size;

} else {

           //extract from $_SESSION superglobal if exist
           $font_family = $_SESSION['font_family'];
           $font_size = $_SESSION['font_size'];
}

?>
Requesting and setting
            Preferences
• HTML portion, see 17-3session01.php
  – Display the default/previous preferences set by
    CSS styles
  – Requests for new preferences in a form
  – Transmit the new preferences to
    17-3session02.php
• 17-3session02.php
  – Set the new preferences in $_SESSION
Initially preferences not defined, defaults used
Selecting new preferences
Wingdings and font_size 12
Selecting Courier and size 14 pt
Courier and size 14pt

Más contenido relacionado

La actualidad más candente

PHP and Databases
PHP and DatabasesPHP and Databases
PHP and DatabasesThings Lab
 
エロサイト管理者の憂鬱3 - Hokkaiodo.pm#4 -
エロサイト管理者の憂鬱3 - Hokkaiodo.pm#4 -エロサイト管理者の憂鬱3 - Hokkaiodo.pm#4 -
エロサイト管理者の憂鬱3 - Hokkaiodo.pm#4 -Yusuke Wada
 
Php - Getting good with session
Php - Getting good with sessionPhp - Getting good with session
Php - Getting good with sessionFirdaus Adib
 
Twib in Yokoahma.pm 2010/3/5
Twib in Yokoahma.pm 2010/3/5Twib in Yokoahma.pm 2010/3/5
Twib in Yokoahma.pm 2010/3/5Yusuke Wada
 
Image upload in php MySql
Image upload in php MySqlImage upload in php MySql
Image upload in php MySqlIshaq Shinwari
 
PHP Programming: Intro
PHP Programming: IntroPHP Programming: Intro
PHP Programming: IntroThings Lab
 
Class 6 - PHP Web Programming
Class 6 - PHP Web ProgrammingClass 6 - PHP Web Programming
Class 6 - PHP Web ProgrammingAhmed Swilam
 
Php session 3 Important topics
Php session 3 Important topicsPhp session 3 Important topics
Php session 3 Important topicsSpy Seat
 
HackU PHP and Node.js
HackU PHP and Node.jsHackU PHP and Node.js
HackU PHP and Node.jssouridatta
 
GettingStartedWithPHP
GettingStartedWithPHPGettingStartedWithPHP
GettingStartedWithPHPNat Weerawan
 
System performance tuning
System performance tuningSystem performance tuning
System performance tuningMenandro Oba
 
PHP Basics and Demo HackU
PHP Basics and Demo HackUPHP Basics and Demo HackU
PHP Basics and Demo HackUAnshu Prateek
 

La actualidad más candente (20)

PHP and Databases
PHP and DatabasesPHP and Databases
PHP and Databases
 
エロサイト管理者の憂鬱3 - Hokkaiodo.pm#4 -
エロサイト管理者の憂鬱3 - Hokkaiodo.pm#4 -エロサイト管理者の憂鬱3 - Hokkaiodo.pm#4 -
エロサイト管理者の憂鬱3 - Hokkaiodo.pm#4 -
 
Php - Getting good with session
Php - Getting good with sessionPhp - Getting good with session
Php - Getting good with session
 
Tax management-system
Tax management-systemTax management-system
Tax management-system
 
Twib in Yokoahma.pm 2010/3/5
Twib in Yokoahma.pm 2010/3/5Twib in Yokoahma.pm 2010/3/5
Twib in Yokoahma.pm 2010/3/5
 
Image upload in php MySql
Image upload in php MySqlImage upload in php MySql
Image upload in php MySql
 
PHP Programming: Intro
PHP Programming: IntroPHP Programming: Intro
PHP Programming: Intro
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Class 6 - PHP Web Programming
Class 6 - PHP Web ProgrammingClass 6 - PHP Web Programming
Class 6 - PHP Web Programming
 
Php session 3 Important topics
Php session 3 Important topicsPhp session 3 Important topics
Php session 3 Important topics
 
PHP for hacks
PHP for hacksPHP for hacks
PHP for hacks
 
Php talk
Php talkPhp talk
Php talk
 
File Upload
File UploadFile Upload
File Upload
 
HackU PHP and Node.js
HackU PHP and Node.jsHackU PHP and Node.js
HackU PHP and Node.js
 
Add loop shortcode
Add loop shortcodeAdd loop shortcode
Add loop shortcode
 
GettingStartedWithPHP
GettingStartedWithPHPGettingStartedWithPHP
GettingStartedWithPHP
 
Phphacku iitd
Phphacku iitdPhphacku iitd
Phphacku iitd
 
extending-php
extending-phpextending-php
extending-php
 
System performance tuning
System performance tuningSystem performance tuning
System performance tuning
 
PHP Basics and Demo HackU
PHP Basics and Demo HackUPHP Basics and Demo HackU
PHP Basics and Demo HackU
 

Destacado

Get Social with StarBuzz Social Web Community
Get Social with StarBuzz Social Web Community Get Social with StarBuzz Social Web Community
Get Social with StarBuzz Social Web Community StarBuzz Weekly
 
Sinh vienit.net --bao-cao-design_patterns
Sinh vienit.net --bao-cao-design_patternsSinh vienit.net --bao-cao-design_patterns
Sinh vienit.net --bao-cao-design_patternshaduyen757
 
Design Patterns Course
Design Patterns CourseDesign Patterns Course
Design Patterns CourseAhmed Soliman
 
Phani Kumar - Decorator Pattern
Phani Kumar - Decorator PatternPhani Kumar - Decorator Pattern
Phani Kumar - Decorator Patternmelbournepatterns
 
Presenter and Decorator in Rails
Presenter and Decorator in RailsPresenter and Decorator in Rails
Presenter and Decorator in RailsThaichor Seng
 
Decorator design pattern (A Gift Wrapper)
Decorator design pattern (A Gift Wrapper)Decorator design pattern (A Gift Wrapper)
Decorator design pattern (A Gift Wrapper)Sameer Rathoud
 
Design Pattern lecture 3
Design Pattern lecture 3Design Pattern lecture 3
Design Pattern lecture 3Julie Iskander
 
Decorator Design Pattern
Decorator Design PatternDecorator Design Pattern
Decorator Design PatternAdeel Riaz
 
Design Patterns - 01 Introduction and Decorator Pattern
Design Patterns - 01 Introduction and Decorator PatternDesign Patterns - 01 Introduction and Decorator Pattern
Design Patterns - 01 Introduction and Decorator Patterneprafulla
 
Observer and Decorator Pattern
Observer and Decorator PatternObserver and Decorator Pattern
Observer and Decorator PatternJonathan Simon
 

Destacado (18)

12 cache questions
12 cache questions12 cache questions
12 cache questions
 
8 polymorphism
8 polymorphism8 polymorphism
8 polymorphism
 
Get Social with StarBuzz Social Web Community
Get Social with StarBuzz Social Web Community Get Social with StarBuzz Social Web Community
Get Social with StarBuzz Social Web Community
 
Sinh vienit.net --bao-cao-design_patterns
Sinh vienit.net --bao-cao-design_patternsSinh vienit.net --bao-cao-design_patterns
Sinh vienit.net --bao-cao-design_patterns
 
16 cookies
16 cookies16 cookies
16 cookies
 
Design Patterns Course
Design Patterns CourseDesign Patterns Course
Design Patterns Course
 
Decorator
DecoratorDecorator
Decorator
 
Phani Kumar - Decorator Pattern
Phani Kumar - Decorator PatternPhani Kumar - Decorator Pattern
Phani Kumar - Decorator Pattern
 
Presenter and Decorator in Rails
Presenter and Decorator in RailsPresenter and Decorator in Rails
Presenter and Decorator in Rails
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
15 decorator pattern
15 decorator pattern15 decorator pattern
15 decorator pattern
 
Decorator design pattern (A Gift Wrapper)
Decorator design pattern (A Gift Wrapper)Decorator design pattern (A Gift Wrapper)
Decorator design pattern (A Gift Wrapper)
 
Decorator design pattern
Decorator design patternDecorator design pattern
Decorator design pattern
 
12 memory hierarchy
12 memory hierarchy12 memory hierarchy
12 memory hierarchy
 
Design Pattern lecture 3
Design Pattern lecture 3Design Pattern lecture 3
Design Pattern lecture 3
 
Decorator Design Pattern
Decorator Design PatternDecorator Design Pattern
Decorator Design Pattern
 
Design Patterns - 01 Introduction and Decorator Pattern
Design Patterns - 01 Introduction and Decorator PatternDesign Patterns - 01 Introduction and Decorator Pattern
Design Patterns - 01 Introduction and Decorator Pattern
 
Observer and Decorator Pattern
Observer and Decorator PatternObserver and Decorator Pattern
Observer and Decorator Pattern
 

Similar a 17 sessions

season management in php (WT)
season management in php (WT)season management in php (WT)
season management in php (WT)kunjan shah
 
PHP-Cookies-Sessions.pdf
PHP-Cookies-Sessions.pdfPHP-Cookies-Sessions.pdf
PHP-Cookies-Sessions.pdfHumphreyOwuor1
 
Lecture8 php page control by okello erick
Lecture8 php page control by okello erickLecture8 php page control by okello erick
Lecture8 php page control by okello erickokelloerick
 
PHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptxPHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptxShitalGhotekar
 
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.pptLecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.pptSreejithVP7
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessionsUdaAs PaNchi
 
Session Server - Maintaing State between several Servers
Session Server - Maintaing State between several ServersSession Server - Maintaing State between several Servers
Session Server - Maintaing State between several ServersStephan Schmidt
 
7. Sessions.pptx
7. Sessions.pptx7. Sessions.pptx
7. Sessions.pptxmawordTz
 
Creating a Simple PHP and MySQL-Based Login System
Creating a Simple PHP and MySQL-Based Login SystemCreating a Simple PHP and MySQL-Based Login System
Creating a Simple PHP and MySQL-Based Login SystemAzharul Haque Shohan
 
Amp and higher computing science
Amp and higher computing scienceAmp and higher computing science
Amp and higher computing scienceCharlie Love
 
Introducation to php for beginners
Introducation to php for beginners Introducation to php for beginners
Introducation to php for beginners musrath mohammad
 
Beyond symfony 1.2 (Symfony Camp 2008)
Beyond symfony 1.2 (Symfony Camp 2008)Beyond symfony 1.2 (Symfony Camp 2008)
Beyond symfony 1.2 (Symfony Camp 2008)Fabien Potencier
 

Similar a 17 sessions (20)

season management in php (WT)
season management in php (WT)season management in php (WT)
season management in php (WT)
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php session
Php sessionPhp session
Php session
 
PHP-Cookies-Sessions.pdf
PHP-Cookies-Sessions.pdfPHP-Cookies-Sessions.pdf
PHP-Cookies-Sessions.pdf
 
Lecture8 php page control by okello erick
Lecture8 php page control by okello erickLecture8 php page control by okello erick
Lecture8 php page control by okello erick
 
PHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptxPHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptx
 
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.pptLecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
 
4.4 PHP Session
4.4 PHP Session4.4 PHP Session
4.4 PHP Session
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
 
Session Server - Maintaing State between several Servers
Session Server - Maintaing State between several ServersSession Server - Maintaing State between several Servers
Session Server - Maintaing State between several Servers
 
Sessions in php
Sessions in php Sessions in php
Sessions in php
 
Mojolicious
MojoliciousMojolicious
Mojolicious
 
Php with my sql
Php with my sqlPhp with my sql
Php with my sql
 
CakePHP workshop
CakePHP workshopCakePHP workshop
CakePHP workshop
 
7. Sessions.pptx
7. Sessions.pptx7. Sessions.pptx
7. Sessions.pptx
 
Creating a Simple PHP and MySQL-Based Login System
Creating a Simple PHP and MySQL-Based Login SystemCreating a Simple PHP and MySQL-Based Login System
Creating a Simple PHP and MySQL-Based Login System
 
Amp and higher computing science
Amp and higher computing scienceAmp and higher computing science
Amp and higher computing science
 
Introducation to php for beginners
Introducation to php for beginners Introducation to php for beginners
Introducation to php for beginners
 
Beyond symfony 1.2 (Symfony Camp 2008)
Beyond symfony 1.2 (Symfony Camp 2008)Beyond symfony 1.2 (Symfony Camp 2008)
Beyond symfony 1.2 (Symfony Camp 2008)
 
Php tutorial
Php tutorialPhp tutorial
Php tutorial
 

17 sessions

  • 1. Sessions session_start() function session_register(‘variable’) function $_SESSION[‘variable’]
  • 2. Before you begin, check php.ini ; The file storage module creates files using mode 600 by default. ; You can change that by using ; ; session.save_path = "N;MODE;/path" ; ; where MODE is the octal representation of the mode. Note that this ; does not overwrite the process's umask. ; http://php.net/session.save-path session.save_path = "c:/wamp/tmp“ For Windows, modify session.save_path so that a session file can be written to a directory that exists. In the case of WAMP, it already exists.
  • 4. For cs346.cs.uwosh.edu /var/lib/php5 already exists and is empty huen@CS346:/var/lib/php5$ sudo pwd /var/lib/php5 huen@CS346:/var/lib/php5$ sudo ls -l total 0 huen@CS346:/var/lib/php5$
  • 5. What is a session? • Time perspective: • A session is the length of time during which a user visits a site • Programming perspective: – A session is a big blob that can hold all sorts of variables and values – The blob has an identification string e.g. sess_fc48gdbuckuj9oe3ef74i00b01 – The identification string is sent to the user when a session is initiated in a cookie called PHPSESSID (accessible via $_COOKIE[PHPSESSID] – On the server side, a matching temporary file is created with the same name (sess_fc48gdbuckuj9oe3ef74i00b01)
  • 6. Session Variables • Session variables and the corresponding values are stored in the temporary session file • Database is not used – no connection nor query needed • You may access the session variables through the $_SESSION superglobal • Example: suppose session file contains: – Count|s:7:”17”; – Valid|s:7:”yes”; – Count and Valid are session variables – Count and Valid must first be added to $_SESSION – Extract with $_SESSION[Count] and $_SESSION[Valid]
  • 7. Operations to retrieve a session variable e.g. $_SESSION[Count] • PHP engine gets the value of $_COOKIE[PHPSESSID] from the user cookie • PHP engine finds a matching temporary session file • Inside the session file, the PHP engine looks for Count variable and then extracts its value i.e. 17 • $_SESSION[Count] get the value 17
  • 8. To start a session: • Just call session_start() function • PHP does the rest: – Sends the cookie to the user – Creates the temporary session file • See m17/17-1session.php
  • 9. Registering and Modifying Session Variables • See 17-2countme.php <?php session_start(); session_register('count'); /* register a variable called count After registering, as long as this session exists, a variable called $_SESSION[count] will be available. Initially it has no value. */ $_SESSION['count']++; $msg ="<p>You've been here $_SESSION[count] times. Thanks!</p>"; ?>
  • 11. Session temporary file Session temporary file contains count|i:7;
  • 12. Getting more complicated . . . • Managing user preferences with Sessions – Start a session – Ask the user for preferred font family and base font size – Display the preferences in subsequent pages – Allow the user to change the preferences and reset the values – See 17-3session01.php
  • 13. Starting a session and Registering Defaults //PHP file for the defaults/preferences + html to request preferences <?php //start a session session_start(); /* Because the user may come back to reset, so we must check if previous values exist. If previous values of font-family and font_size are not defined, assign default values else extract the values from $_SESSION superglobal */
  • 14. // Preparing for the CSS attributes //check for stored values and register defaults if ((!$_SESSION['font_family']) || (!$_SESSION['font_size'])) { $font_family = "sans-serif"; $font_size = "10"; $_SESSION['font_family'] = $font_family; $_SESSION['font_size'] = $font_size; } else { //extract from $_SESSION superglobal if exist $font_family = $_SESSION['font_family']; $font_size = $_SESSION['font_size']; } ?>
  • 15. Requesting and setting Preferences • HTML portion, see 17-3session01.php – Display the default/previous preferences set by CSS styles – Requests for new preferences in a form – Transmit the new preferences to 17-3session02.php • 17-3session02.php – Set the new preferences in $_SESSION
  • 16. Initially preferences not defined, defaults used
  • 19. Selecting Courier and size 14 pt