Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

17 sessions

217 Aufrufe

Veröffentlicht am

  • Als Erste(r) kommentieren

  • Gehören Sie zu den Ersten, denen das gefällt!

17 sessions

  1. 1. Sessions session_start() functionsession_register(‘variable’) function $_SESSION[‘variable’]
  2. 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 processs umask.; http://php.net/session.save-pathsession.save_path = "c:/wamp/tmp“For Windows, modify session.save_path so that a sessionfile can be written to a directory that exists. In the case ofWAMP, it already exists.
  3. 3. Check with Windows Explorer
  4. 4. For cs346.cs.uwosh.edu/var/lib/php5 already exists and is emptyhuen@CS346:/var/lib/php5$ sudo pwd/var/lib/php5huen@CS346:/var/lib/php5$ sudo ls -ltotal 0huen@CS346:/var/lib/php5$
  5. 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. 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. 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. 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. 9. Registering and Modifying Session Variables• See 17-2countme.php<?phpsession_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>Youve been here $_SESSION[count] times. Thanks!</p>";?>
  10. 10. Accessing 17-2countme.phpRefresh the page 6 more times:
  11. 11. Session temporary fileSession temporary file contains count|i:7;
  12. 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. 13. Starting a session and Registering Defaults//PHP file for the defaults/preferences + html to request preferences<?php//start a sessionsession_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. 14. // Preparing for the CSS attributes//check for stored values and register defaultsif ((!$_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. 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. 16. Initially preferences not defined, defaults used
  17. 17. Selecting new preferences
  18. 18. Wingdings and font_size 12
  19. 19. Selecting Courier and size 14 pt
  20. 20. Courier and size 14pt