1 / 15

State Handling with Sessions CS 4640 Programming Languages for Web Applications

Explore session tracking methods using HTTP requests, cookies, and more to manage state data in web applications effectively. Learn how to implement session objects, share data among clients, and prevent security vulnerabilities.

Download Presentation

State Handling with Sessions CS 4640 Programming Languages for Web Applications

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. State Handling with SessionsCS 4640 Programming Languages for Web Applications [Based in part on SWE 432 and SWE 632 materials by Jeff Offutt, GMU] [Robert W. Sebesta, “Programming the World Wide Web]

  2. revisit Session Tracking Methods HTTP Request (with a token / data) • URL rewriting -- include data as extra parameters • Hidden form fields • Cookies • Server-side session objects All four methods work by exchanging a token between the client and server Web server Container engine UI implemented in a browser Program components Server Client HTTP Response (with a token / HTML)

  3. Sessions • A single coherent use of the system by the same user • All web components sharing a session must start session to join the existing session (or create a new session if none available) • $_SESSION global array stores data in the current active object • Data disappear when the object is destroyed • The session object is destroyed after the session ends, usually 30 minutes after the last request

  4. Sessions (Overview) Web server Time Time Client 1 Client 2 HTTP Request HTTP Request HTTP Response Session ID = 0347 HTTP Response Server returns a new unique session ID when the request has none Session ID = 4403 HTTP Request Session ID = 0347 HTTP Request Session ID = 4403 HTTP Response Session ID = 0347 HTTP Response Session ID = 4403 HTTP Request Session ID = 0347 HTTP Request Session ID = 4403 HTTP Response Session ID = 0347 HTTP Response Session ID = 4403

  5. Sessions (Overview) Web server Time Time Client 1 Client 2 HTTP Request HTTP Request Client stores the ID and sends it to the server in subsequent HTTP Response HTTP Response Session ID = 0347 Session ID = 4403 HTTP Request Session ID = 0347 Server recognizes all the requests as being from the same client. This defines a session. HTTP Request Server recognizes all the requests as being from a different client. Session ID = 4403 HTTP Response Session ID = 0347 HTTP Response Session ID = 4403 HTTP Request Session ID = 0347 HTTP Request Session ID = 4403 HTTP Response Session ID = 0347 HTTP Response Session ID = 4403

  6. Using Session objects • Initialize a session object: Once the session is available, session data are accessible from an implicit $_SESSION global array variable • Store information (state) in the session object, use name as a key: • Get values from session objects, use name as a key: session_start() $_SESSION[name] = value $_SESSION[name]

  7. Using Session Objects count($_SESSION) Return the number of param/value pairs stored in a session object To view state stored in a session object, iterate a $_SESSION array or specify a parameter name $_SESSION[name] Retrieve a session ID foreach ($_SESSION as $key => $value) { echo “$key maps to $value <br/>”; } session_id()

  8. Using Session objects • Remove a session variable, use name as a key: • Terminate a session Completely remove a session object unset($_SESSION[name]) session_destroy()

  9. Session Definition A session is defined by • The web server • PHP container • The client • IP address • Browser • Session objects are kept on the server • Each session object uses different parts of memory (instances of data values) on the server

  10. Sharing Data: Session Object • One program component can store a value in the session object • Another component can retrieve, use, and modify the value • Depends on the PHP container • Software components are threads, not processes • PHP container stays resident and can keep shared memory

  11. Thought Questions • What user-specific data are stored on the server? What user-specific data are stored on the client? • How does the server recognize whether the requests are from the same user or different user? • If a user wants to view his/her information previously stored in the server-side session, how does the server respond to the request? • Assuming a user wants to update (or delete) his/her information previously stored in the server-side session, how does the server respond to the request? • What can happen if someone (let's say, a hacker) knows the user's sessionID? • What can be done to potentially prevent or minimize the chance of the user session being hacked?

  12. More General Concept on Maintaining State Single-user session state • Cookies and session object Multi-user session state • Context object (will not be tested) • Data persistence Why do we need them? • Social network system – allow multiple users to interact • Group working – online meeting • Online bidding • Reservation system, invitation system Sometimes we want to share session data among multiple clients (group of users)

  13. Summary • Managing state is fundamental to any program • Managing state is the most unique aspect of designing and programming web applications • Software vendors are creating new frameworks all the time • Most of them introduce additional state handling techniques • Many professional developers make fundamental mistakes with managing state • Books and tutorials describe syntax, but not concepts State management is the most common source of software faults in web apps

  14. Extra slide More general concepts on maintaining states (will not be tested)

  15. Component1 Component4 Component5 Component6 Component2 Component3 Context Scope Container Engine session object 2 session object 1 context object Session 1 Session 2 Context (application)

More Related