Sessions and cookies MIS 3501, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 4/12/2016.

Slides:



Advertisements
Similar presentations
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK)
Advertisements

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.
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.
Week 9 PHP Cookies and Session Introduction to JavaScript.
Web Programming Language Week 7 Dr. Ken Cosh Security, Sessions & Cookies.
12/3/2012ISC329 Isabelle Bichindaritz1 PHP and MySQL Advanced Features.
Week seven CIT 354 Internet II. 2 Objectives Database_Driven User Authentication Using Cookies Session Basics Summary Homework and Project 2.
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
PHP Workshop ‹#› Maintaining State in PHP Part II - Sessions.
SessionsPHPApril 2010 : [‹#›] 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.
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.
PHP-language, sessions Teppo Räisänen Principal Lecturer Oulu University of Applied Sciences School of Business and Information Management
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.
Class06 Conditional Statements MIS 3501, Fall 2015 Brad Greenwood, PhD MBA Department of MIS Fox School of Business Temple University 9/10/2015 © 2014,
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.
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.
Class03 Introduction to Web Development (Hierarchy and the IDE) MIS 3501, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
Class02 More Arrays MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 1/14/2016.
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.
Sessions and cookies (part 2) MIS 3501, Fall 2015 Brad N Greenwood, PhD Department of MIS Fox School of Business Temple University 11/19/2015.
Form Data (part 2) MIS 3502, Fall 2015 Brad N Greenwood, PhD Department of MIS Fox School of Business Temple University 11/10/2015 Slide 1.
Form Data (part 1) MIS 3502, Fall 2015 Brad Greenwood, PhD Department of MIS Fox School of Business Temple University 11/10/2015.
Introduction to AJAX MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/4/2016.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
Class07 PHP: loops and includes MIS 3501, Fall 2015 Brad Greenwood, PhD MBA Department of MIS Fox School of Business Temple University 9/15/2015.
Class05 How to get data from a form MIS 3501, Fall 2015 Brad N Greenwood, PhD MBA Department of MIS Fox School of Business Temple University 9/8/2015.
Programming for the Web Cookies & Sessions Dónal Mulligan BSc MA
JavaScript, AJAX and JSON MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/9/2016.
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
PDO Database Connections
Brad N Greenwood, PhD MBA
Sessions and cookies MIS 3501 Jeremy Shafer Department of MIS
Form Data (part 2) MIS 3502, Fall 2015 Jeremy Shafer Department of MIS
Organize your code with MVC
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.
PHP: includes MIS 3501 Jeremy Shafer Department of MIS
Sessions and cookies (part 2)
Class07 PHP: loops and includes
Cookies and Sessions in PHP
How to get data from a form
PDO Database Connections: Getting data out of the database
ISC440: Web Programming 2 Server-side Scripting PHP 3
PDO Database Connections: Getting data out of the database
Form Data (part 2) MIS 3501 Jeremy Shafer Department of MIS
Organize your code with MVC
<?php require("header.htm"); ?>
Introduction to relational databases and MySQL
Sessions and cookies (part 1)
Form Data (part 2) MIS 3501 Jeremy Shafer Department of MIS
Class07 PHP: loops MIS 3501 Jeremy Shafer Department of MIS
Class05 How to get data from a form
CSE 154 Lecture 21: Sessions.
Sessions and cookies MIS 3501 Jeremy Shafer Department of MIS
Web Programming Language
CSE 154 Lecture 22: Sessions.
Form Data (part 1) MIS3501 Jeremy Shafer Department of MIS
MVC – Model View Controller
Presentation transcript:

Sessions and cookies MIS 3501, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 4/12/2016

MySQL Weeks 6 & 7 HTML & CSS Weeks 1 & 2 PHP Weeks 3 – 5 PDO Week Course Overview To do: Organize your code with MVC (week 12) Work with forms (week 13) Use cookies and sessions (week 14) We are here. Slide 2

Objectives Learning objectives for the week 1.Understand why HTML pages are referred to as “stateless” 2.Understand how cookies and sessions are used to preserve state, and the differences between cookies and sessions. 3.Understand the PHP commands used to manipulate sessions. 4.Understand the PHP commands used to direct a user to another page. Slide 3

Preserving State Slide 4

This is why we say HTML is “stateless”. There’s nothing implicit in browsers treatment of HTML that “remembers” the pages that were returned previously. HTML pages are, by design, intended to work over an intermittent network connection. Why managing state is difficult with HTTP Slide 5

Choices for managing state We are going to talk about sessions … and in a way that’s a little backwards, because cookies came first. But sessions are generally better / more secure / more useful and easier to work with. In the next slides we will compare these two mechanisms…. Cookies Sessions Slide 6

How cookies work Slide 7

How sessions work Slide 8

Slide 9

Preserving State Let’s try this example… Suggestion Box Slide 10 There should be a “public facing” aspect of our suggestion box application. It is for regular employees. It allows for anonymous submission of suggestions. But there is also a report that only management should see. We need to know the state of a session variable we’ll call “LOGGED_IN” on report.php. Either the user logged in OK or did not.

Mechanics …how is this accomplished? Things we need to know how to do: 1.Let specific PHP pages know that we intend to use sessions 2.Put a value into the $_SESSION[] array on successful login. 3.For protected content, check to see if that $SESSION[] variable exists. Take appropriate action if it does (or does not) exist. 4.Destroy the session when we are done. (#4 is trickier than you might think) Slide 11

The session_start() function Easy, right?! Just remember this one fact … you need to use the session_start() function on each page where you intend to use session data. You also have the option of changing the default behavior of the cookie with this function. You don’t typically need to do this. Slide 12

The $_SESSION array This convention should remind you of working with $_POST and $_GET. It should because $_POST, $_GET and $_SESSION are all superglobals. What’s a superglobal? An superglobal is just an array that the PHP Interpreter gives you “for free” - that is you don’t need to declare it or control it’s behavior. Remember… When the session_start() function is called, PHP either initializes a new $_SESSION superglobal or retrieves any variables for into the $_SESSION superglobal Slide 13

Killing the session PHP gives us the function session_destroy() The session_destroy() function destroys all of the data associated with the current session. But … it does not: 1.unset any of the global variables associated with the session 2.unset the session cookie on the browser Slide 14

A complete logout script – logout.php Slide 15 <?php // Initialize the session... Yes, this is the session we want to destroy. session_start(); // Unset all of the session variables. The session array is assigned to an empty array $_SESSION = array(); // Now... the tricky part... kill the cookie on the browser // Delete the cookie for the session $name = session_name(); // Get name of the session cookie $expire = strtotime('-1 year'); // Create expiration date in the past $setcookie($name, null, $expire); // set the cookie value to null, and expire it // Finally, destroy the session. session_destroy(); // All done with the session. Direct the user back to a landing page. header('Location:../index.php'); ?>

This process of directing the user from one page to another implies that we have some command(s) for doing just that sort of thing. We may want to direct the user to one page or another depending on the state of the application. This sort of conditional operation implies that …. We’re talking about conditional statements in the controller. Directing the user to different pages Slide 16

Directing the user to different pages (2) Slide 17 We have already seen controllers that use include and exit commands to reference different views. But… what if I want to jump the user to an entirely different application?

Directing the user to different pages (3) Slide 18 For that, we will use a different command! The header command has this syntax: header('Location: url-goes-here'); exit();

Directing the user to different pages (4) Slide 19 Technically, this is a bit of an oversimplification… but it is a good rule of thumb. Use include and exit within an application folder. Use header and exit to bounce the user from one application folder to another.

Let’s try it. As they say in show business… Slide 20