1 / 28

Web Technology Solutions

Web Technology Solutions. Class: State Management with PHP. Date : 2/18/2014. Tonight. HTTP State OverviewCookies in PHPSessions in PHPiHear DB and App ReviewLab. Lab Preview. Install “State” in your authentication sub system. Install State via Sessions. Final Project Working Session

hiero
Télécharger la présentation

Web Technology Solutions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Web Technology Solutions • Class: State Management with PHP Date : 2/18/2014

  2. Tonight • HTTP State OverviewCookies in PHPSessions in PHPiHear DB and App ReviewLab

  3. Lab Preview • Install “State” in your authentication sub system. • Install State via Sessions. • Final Project Working Session • Create CRUD functionality for Survey • Install State throughout App. • Auth • Registration • Survey

  4. HTTP Review • HTTP is a “Stateless” protocol • Requests between client and server retain no memory of the previous request or future requests. • OK? So what does that mean to me as an app dev?

  5. HTTP Review Want to Share info from page to page? You can’t! Unless you...

  6. HTTP Review Maintain State via... Sessions Cookies

  7. HTTP Review

  8. Cookies with PHP • Cookies are small files stored on your (clients) machine. • Consists of clear text in named\value pairs (non-secure) • Send by and stored in your browser. • Sent in the HTTP Header

  9. Cookies with PHP • Cookies are good for • site prefs / personalization • remember non-secure data • shopping cart values • remember me login’s • Anything that doesn’t “break” the functionality of your site.

  10. Cookies with PHP • Cookies are bad for • storing sensitive data • storing required data for app • storage of persistent data

  11. PHP - setCookie • setcookie(); • uses the PHP superglobal $_COOKIE (array) • set’s a first party cookie on the client (browser) • safe to store basic data values (not sensitive data like passwords) • Browsers limit size of total count of cookies.

  12. PHP - setCookie • setcookie() • used to create a cookie • param1: name • param2: value • param3: time to live in milsec • param4: folder • param5: domain • param6: secure • setcookie("user", “bob”, time()+3600, "/home", ".site.com", 1);

  13. PHP - setCookie • setcookie() • also used to delete • setcookie ("user", "", time() - 3600); • note the negative seconds. • setcookie("user", “bob”, time()-3600, "/home", ".site.com", 1);

  14. Cookie Example

  15. PHP Sessions Sessions

  16. Sessions in PHP • Sessions store data on the server (note config) • Sessions also store a session ID cookie on the client • Sessions need to be started on each page • PHP offers an auto start option in the php.ini

  17. Sessions in PHP • Sessions are good for: • Secure data • Quasi-Persistent Storage • Full App functionality.

  18. Sessions in PHP • Three methods for passing Session ID • via Cookie (default) • via DB storage (ok) • via URL (bad)

  19. Sessions in PHP • Sessions are bad for: • Overall very good to use. • Beware session hijacking.

  20. PHP Sessions • To start: • session_start(); • needs to be called on EACH page or session data will not be carried onto that page. • $_SESSION[‘name’] = value; • echo $_SESSION[‘name’];

  21. PHP Sessions • To kill session value • unset($_SESSION['name']); • To kill off all session data • $_SESSION = array(); • session_destroy(); • setcookie('PHPSESSID', '',time()-300, '/',0);

  22. Session Example

  23. PHP Output Control • Output Control allows you to tell PHP when to submit information to the browser. • Great: • Working with header(), avoid errors • Controlling Browser Output • Cons: • Buffer Limits (default bite size of 4096kb) • Memory Limits

  24. Output Buffering • ob_start(); • Turns on output buffering • data is held within internal “buffer” waiting to be published to the browser. • Call at start of script • Can have a callback function • Can nest buffers

  25. Output Buffering • ob_end_flush() • Sends the data in the buffer to the browser • Turns off output buffer. • Loop through ob_end_flush() to close all jobs

  26. Output Buffering • ob_end_clean() • //removes data from the buffer (doesn’t go to browser) • ob_flush() • //send data to the browser but buffer remains on • ob_get_contents() • //get the content of the buffer (no browser or erase)

  27. App Review

  28. Lab & Next Week • Lab • implement sessions in app • working session for final • Reading: Chapter 13 See you Tuesday!

More Related