Cookies and Sessions IDIA 618 Fall 2014 Bridget M. Blodgett.

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.
Lecture 6/2/12. Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms.
V 1.0 OE NIK 2013 PHP+SQL 5. Password management (password hashing) Stateless HTTP, storage methods Login form 1.
Chapter 10 Managing State Information Using Sessions.
©2009 Justin C. Klein Keane PHP Code Auditing Session 7 Sessions and Cookies Justin C. Klein Keane
Chapter 10 Managing State Information PHP Programming with MySQL.
CSE 154 LECTURE 13: SESSIONS. Expiration / persistent cookies setcookie("name", "value", expiration); PHP $expireTime = time() + 60*60*24*7; # 1 week.
Chapter 10 Maintaining State Information Using Cookies.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
Cookies Set a cookie – setcookie() Extract data from a cookie - $_COOKIE Augment user authentication script with a cookie.
Chapter 12 Cookies and Sessions Part 2. Setting Cookie Parameters setcookie(name, value, expiration, path, host, secure, httponly) epoch – midnight on.
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.
Lecture 14 – Web Security SFDV3011 – Advanced Web Development 1.
JavaScript, Fourth Edition
Week 9 PHP Cookies and Session Introduction to JavaScript.
CSE 154 LECTURE 12: COOKIES. Including files: include include("filename"); PHP include("header.html"); include("shared-code.php"); PHP inserts the entire.
Web Programming Language Week 7 Dr. Ken Cosh Security, Sessions & Cookies.
Chapter 8 Cookies And Security JavaScript, Third Edition.
12/3/2012ISC329 Isabelle Bichindaritz1 PHP and MySQL Advanced Features.
Feedback #2 (under assignments) Lecture Code:
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.
Nic Shulver, Introduction to Sessions in PHP Sessions What is a session? Example Software Software Organisation The login HTML.
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
Prof Frankl, Spring 2008CS Polytechnic University 1 Overview of Web database applications with PHP.
PHP Workshop ‹#› Maintaining State in PHP Part II - Sessions.
PHP. $_GET / $_POST / $_SESSION PHP uses predefined variables to provide access to important information about the server and requests from a browser.
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.
Web Database Programming Week 7 Session Management & Authentication.
Cookies Bill Chu. © Bei-Tseng Chu Aug 2000 Definition A cookie is a TEXT object of max 4KB sent from a web server to a browser It is intended for the.
Copyright © 2003 Pearson Education, Inc. Slide 7-1 The Web Wizard’s Guide to PHP by David Lash.
MEMBERSHIP AND IDENTITY Active server pages (ASP.NET) 1 Chapter-4.
Sessions and Cookies State Management, Cookies, Sessions, Hidden Fields SoftUni Team Technical Trainers Software University
ECMM6018 Enterprise Networking for Electronic Commerce Tutorial 7
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)
PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)
 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.
Session Management Tyler Moore CS7403 University of Tulsa Slides adapted in part or whole from Dan Boneh, Stanford CS155 1.
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.
Copyright © 2003 Pearson Education, Inc. Slide 7-1 The Web Wizard’s Guide to PHP by David Lash.
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
CSE 154 Lecture 20: Cookies.
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.
Web Programming Language
What is Cookie? Cookie is small information stored in text file on user’s hard drive by web server. This information is later used by web browser to retrieve.
Web Systems Development (CSC-215)
Web Systems Development (CSC-215)
<?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.
CSE 154 Lecture 21: Sessions.
Web Programming Language
CSE 154 Lecture 22: Sessions.
Web Programming Language
Presentation transcript:

Cookies and Sessions IDIA 618 Fall 2014 Bridget M. Blodgett

User Handling Even if your site doesn’t offer logins and passwords – often there is a need to store details about a user’s current session and recognize when they return Multiple methods of handling this: – Cookies – Session handling – HTTP Authentication

Cookies items of data that a web server saves to your computer’s hard disk via a web browser – Cannot be larger than 4kb – Can be read and changed by your web browser Common uses include: – session tracking – maintaining data across multiple visits – holding shopping cart contents – storing login details

Cookies and Privacy In order to protect privacy cookies are meant to only be read by the site which issues them One page can contain multiple domains and issue multiple cookies – Ads are infamous for doing this Most browsers allow cookies to be turned off or prevent 3 rd parties from posting cookies

Setting Cookies As long as no HTML has yet been transferred, you can call the setcookie function setcookie(name, value, expire, path, domain, secure, httponly); Example: setcookie('username', 'Hannah', time() + 60 * 60 * 24 * 7, '/');

Accessing Cookies Like $_POST, $_GET, and $_SESSION there is a $_COOKIE value included in PHP To read it simply call the variable with the specified heading you wish to read: if (isset($_COOKIE['username'])) $username = $_COOKIE['username']; However, if the cookie was issued recently it takes a page reload or load to a new page before the cookie can be read

Destroying Cookies Unlike creating and reading cookies the method for deleting them manually is a bit difficult To delete a cookie the value of the cookie must be updated to expire in the past If all the parameters of the update (with the exception of the timestamp) are the same the delete will succeed setcookie('username', 'Hannah', time() , '/');

Exercise Using cookies, create a form that asks a user for his/her name and some comments Create two PHP scripts that create a response: – The first script displays the information which the user has submitted and asks the user whether he/she really wants to submit the information – The second script is invoked by the first one and displays "Thank you $name. Your comments have been submitted: $comments"

HTTP Authentication For websites that require basic user management services – Allows the webserver to authenticate valid users – Useful in most instances but not the maximum user security PHP sends a header request for authentication to the user’s browser – Web server must have feature turned on but is very common – Example 13-1

HTTP Authentication Issues Once authenticated, the authentication dialog will not pop up again unless the user closes and reopens all browser windows Error messages should be a bit vague to discourage attempts to brute force a correct login Any log in information is stored as plaintext in the PHP files/database/web browser in the previous examples

Storing Passwords One-way functions garble the submitted password so that it remains secure even if a server is hacked Md5 hashes of text are some of the most common, but sha1 are more secure Example: $token = md5('mypassword'); 34819d7beeabb9260a5c854bc85b3e44

Salting Salting is a method of wrapping secure plaintext in a unique string to make it much harder to break $token = md5('hqb%$tmypasswordcg*l'); So long as these are added correctly by the server when a user re-enters a password the resulting md5 should match the one stored Example 13-4

Sessions Many times it is useful to have saved information about what a user has accessed in other pages Sessions are groups of variables that are stored on the server but relate only to the current user A cookie is saved in their web browsers to uniquely identify each user, providing security – A get snippet is included instead if the browser has cookies turned off

Starting Sessions Starting a session requires calling the PHP function session_start before any HTML has been output To begin saving session variables, assign them as part of the $_SESSION array: $_SESSION['variable'] = $value; They can then be read back: $variable = $_SESSION['variable']; Unlike other methods sessions are stored on the server and are very secure

Ending A Session When a user requests to log out, you can use session_destroy() in association with the unsetfunction, – a useful function for totally destroying a session, logging a user out, and unsetting all session variables A timeout may be set in case a user does not remember to manually logout or close the browser ini_set('session.gc_maxlifetime', 60 * 60 * 24);

Session Security Using https or SSL based security reduced the likelihood of malicious users misusing stored session information Locking sessions to IP addresses as well as usernames also helps $_SESSION['ip'] = $_SERVER['REMOTE_ADDR']; It is often useful to store the HTTP User agent as well in order to access certain browser information $_SESSION['ua'] = $_SERVER['HTTP_USER_AGENT'];

Session Security Using session_regenerate_id() will force any open sessions to new ids making it so malicious users can’t hijack others You can also force users into using cookies (although this limits your potential users) Any data generated by your site should be stored in a folder that is not shared with other users and is not accessible from the web – It should also be periodically cleared