Maintaining State in PHP Part II - Sessions

Slides:



Advertisements
Similar presentations
CookiesPHPMay-2007 : [‹#›] Maintaining State in PHP Part I - Cookies.
Advertisements

UFCE8V-20-3 Information Systems Development 3 (SHAPE HK)
Cookies, Sessions. Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function.
Client State Management & Application Security  Client State Management  Concept  ASP Examples  Application Security  Database Based Approach 
Chapter 10 Managing State Information PHP Programming with MySQL.
Using Session Control in PHP tMyn1 Using Session Control in PHP HTTP is a stateless protocol, which means that the protocol has no built-in way of maintaining.
CSE 154 LECTURE 13: SESSIONS. Expiration / persistent cookies setcookie("name", "value", expiration); PHP $expireTime = time() + 60*60*24*7; # 1 week.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
CHAPTER 12 COOKIES AND SESSIONS. INTRO HTTP is a stateless technology Each page rendered by a browser is unrelated to other pages – even if they are from.
CSC 2720 Building Web Applications Cookies, URL-Rewriting, Hidden Fields and Session Management.
Advanced Web Forms with Databases Programming Right from the Start with Visual Basic.NET 1/e 13.
Web Programming Language Week 7 Dr. Ken Cosh Security, Sessions & Cookies.
Week seven CIT 354 Internet II. 2 Objectives Database_Driven User Authentication Using Cookies Session Basics Summary Homework and Project 2.
PHP1-1 PHP Lecture 2 Xingquan (Hill) Zhu
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
1 Chapter 9 – Cookies, Sessions, FTP, and More spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
Cookies & Session Web Technology
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
PHP Workshop ‹#› Maintaining State in PHP Part II - Sessions.
STATE MANAGEMENT.  Web Applications are based on stateless HTTP protocol which does not retain any information about user requests  The concept of state.
SessionsPHPApril 2010 : [‹#›] Maintaining State in PHP Part II - Sessions.
COOKIES and SESSIONS. COOKIES A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each.
Sessions in PHP – Page 1 of 13CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: Sessions in PHP Reading: Williams.
Cookies and Sessions IDIA 618 Fall 2014 Bridget M. Blodgett.
Lecture 7 Maintaining State (cookies & sessions) & MySQL Interaction (revisited)
Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy.
PHP Session ISYS 475. Session The web server starts a session when a visitor visiting your web site and assigns a unique id, the session id for the session.
How to maintain state in a stateless web Shirley Cohen
Sessions Brendan Knight A visitor accessing your web site is assigned a unique id. This id links to specific data that remains on the server. Sessions.
SESSIONS 27/2/12 Lecture 8. ? Operator Similar to the if statement but returns a value derived from one of two expressions by a colon. Syntax: (expression)
 A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests.
ITM © Port,Kazman 1 ITM 352 Cookies. ITM © Port,Kazman 2 Problem… r How do you identify a particular user when they visit your site (or any.
©SoftMooreSlide 1 Cookies. ©SoftMooreSlide 2 Cookies Basic idea –web application sends a simple name/value pair to the client –when the client connects.
1 PHP HTTP After this lecture, you should be able to know: How to create and process web forms with HTML and PHP. How to create and process web forms with.
Web Page Designing With Dreamweaver MX\Session 1\1 of 9 Session 3 PHP Advanced.
Cookies and Sessions in PHP. Arguments for the setcookie() Function There are several arguments you can use i.e. setcookie(‘name’, ‘value’, expiration,
Unit-6 Handling Sessions and Cookies. Concept of Session Session values are store in server side not in user’s machine. A session is available as long.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
PHP: Further Skills 02 By Trevor Adams. Topics covered Persistence What is it? Why do we need it? Basic Persistence Hidden form fields Query strings Cookies.
Programming for the Web Cookies & Sessions Dónal Mulligan BSc MA
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● / www,histpk.org Hidaya Institute of Science & Technology
The need for persistence Consider these examples  Counting the number of “hits” on a website  i.e. how many times does a client load your web page source.
Brad N Greenwood, PhD MBA
Sessions and cookies MIS 3501 Jeremy Shafer Department of MIS
Managing State Chapter 13.
CHAPTER 5 SERVER SIDE SCRIPTING
CGS 3066: Web Programming and Design Spring 2016
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
19.10 Using Cookies A cookie is a piece of information that’s stored by a server in a text file on a client’s computer to maintain information about.
ITM 352 Cookies.
Maintaining State in PHP Part II - Sessions
Web Programming Language
Cookies and Sessions in PHP
Open Source Programming
ISC440: Web Programming 2 Server-side Scripting PHP 3
<?php require("header.htm"); ?>
Cookies Cookie :- A cookie is often used to identify a user. A cookie is often used to identify a user. A cookie is a small file that the server embeds.
Sessions and cookies (part 1)
CSE 154 Lecture 21: Sessions.
Building ASP.NET Applications
Sessions and cookies MIS 3501 Jeremy Shafer Department of MIS
Web Programming Language
CSE 154 Lecture 22: Sessions.
SESSION TRACKING BY DINESH KUMAR.R.
Cookies and Sessions.
Web Programming Language
[Based in part on SWE 432 and SWE 632 materials by Jeff Offutt, GMU]
PHP-II.
[Based in part on SWE 432 and SWE 632 materials by Jeff Offutt, GMU]
Presentation transcript:

Maintaining State in PHP Part II - Sessions

So… Cookies Sessions Limited storage space Practically unlimited space Insecure storage client-side Reasonably securely stored server-side User controlled No user control

How do ‘Sessions’ work? They are based on assigning each user a unique number, or session id. Even for extremely heavy use sites, this number can for all practical purposes can be regarded as unique. e.g. 26fe536a534d3c7cde4297abb45e275a

How do ‘Sessions’ work? This session id is stored in a cookie, or passed in the URL between pages while the user browses. The data to be stored (e.g. name, log-in state, etc.) is stored securely server-side in a PHP superglobal, and referenced using the session id.

Crucially, sessions are easy to implement as PHP does all the work!

Starting or Resuming a Session session_start(); PHP does all the work: It looks for a valid session id in the $_COOKIE or $_GET superglobals – if found it initializes the data. If none found, a new session id is created. Note that like setcookie(), this function must be called before any echoed output to browser.

Starting or Resuming a Session session_start(); When doing anything with sessions, this is always called first!

Storing Session Data The $_SESSION superglobal array can be used to store any session data. e.g. $_SESSION[‘name’] = $name; $_SESSION[‘age’] = $age;

Reading Session Data Data is simply read back from the $_SESSION superglobal array. e.g. $name = $_SESSION[‘name’]; $age = $_SESSION[‘age’];

Session Propagation Sessions need to pass the session id between pages as a user browses to track the session. It can do this in two ways: Cookie propagation URL propagation

Cookie Propagation A cookie is stored on the users PC containing the session id. It is read in whenever session_start(); is called to initialize the session. Default behaviour is a cookie that expires when the browser is closed. Cookie properties can be modified with session_set_cookie_params if required.

<a href="nextpage.php?<?=SID?>">Next page</a> URL Propagation The session id is propagated in the URL (…some_folder/index.php?sid=26fe536a534d3c7cde4297abb45e275a) PHP provides a global constant to append the session id to any internal links, SID. e.g. <a href="nextpage.php?<?=SID?>">Next page</a>

Which one..? The default setup of a PHP server is to use both methods. it checks whether the user has cookies enabled. If cookies are on, PHP uses cookie propagation. If cookies are off it uses URL propagation.

And this means..? That as developers, we must be aware that sessions can be propagated through URL, and append the constant SID to any internal links. If sessions are being propagated by cookies, the constant SID is an empty string, so the session id is not passed twice.

Destroying a Session Often not required, but if we want to destroy a session: // clear all session variables $_SESSION = array(); // delete the session cookie if there is one if (isset($_COOKIE[session_name()])) { setcookie(session_name(),'',time()-42000,'/'); } // destroy session session_destroy(); // avoid reusing the SID by redirecting // back to the same page to regenerate session header('Location: '.$_SERVER['PHP_SELF']);

Session Expiry By default, PHP sessions expire: after a certain length of inactivity (default 1440s), the PHP garbage collection processes deletes session variables. Important as most sessions will not be explicitly destroyed. if propagated by cookies, default is to set a cookie that is destroyed when the browser is closed. If URL propagated, session id is lost as soon as navigate away from the site.

Long-term Sessions Although it is possible to customize sessions so that they are maintained after the browser is closed, for most practical purposes PHP sessions can be regarded as short-term. Long-term session data (e.g. ‘remember me’ boxes) is usually maintained by explicitly setting and retrieving cookie data.

Session Hi-jacking A security issue: if a malicious user manages to get hold of an active session id that is not their own.. e.g. user 1 browsing site with cookies disabled (URL propagation). user 1 logs in. user 1 sends an interesting link to user 2 by email.. The URL copy and pasted contains his session id. user 2 looks at the link before session id is destroyed, and ‘hijacks’ user 1’s session. user 2 is now logged in as user 1!!

… rule of thumb … If you are truly security conscious you should assume that a session propagated by URL may be compromised. Propagation using cookies is more secure, but still not foolproof..