Sessions and Cookies State Management, Cookies, Sessions, Hidden Fields SoftUni Team Technical Trainers Software University

Slides:



Advertisements
Similar presentations
LIS651 lecture 3 taming PHP Thomas Krichel
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.
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.
Chapter 10 Managing State Information PHP Programming with MySQL.
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
Sys Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 15: PHP Introduction.
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.
Software Quality Assurance QA Engineering, Testing, Bug Tracking, Test Automation Software University Technical Trainers SoftUni Team.
CSC 2720 Building Web Applications Cookies, URL-Rewriting, Hidden Fields and Session Management.
AngularJS Routing Routes, Route Parameters, Templates, Location, Navigation SoftUni Team Technical Trainers Software University
AngularJS Services Built-in and Custom Services SoftUni Team Technical Trainers Software University
Software University Curriculum, Courses, Exams, Jobs SoftUni Team Technical Trainers Software University
PHP Basics Course Introduction SoftUni Team Technical Trainers Software University
Fundamentals SoftUni Welcome to Software University SoftUni Team Technical Trainers Software University
CSE 154 LECTURE 12: COOKIES. Including files: include include("filename"); PHP include("header.html"); include("shared-code.php"); PHP inserts the entire.
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC SoftUni Team Technical Trainers Software University
Software Testing Lifecycle Exit Criteria Evaluation, Continuous Integration Ivan Yonkov Technical Trainer Software University.
Web Programming Language Week 7 Dr. Ken Cosh Security, Sessions & Cookies.
School of Computing and Information Systems CS 371 Web Application Programming PHP – Forms, Cookies, Sessions and Database.
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.
Database APIs and Wrappers
Microsoft Azure SoftUni Team Technical Trainers Software University
Svetlin Nakov Technical Trainer Software University
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
Processing Redis with.NET How to Operate with Redis Databases SoftUni Team Technical Trainers Software University
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC Angel Georgiev Part-time Trainer Software University
MVC Concepts Basics Model-View-Controller (MVC) Concepts for Web Developers SoftUni Team Technical Trainers Software University
STATE MANAGEMENT.  Web Applications are based on stateless HTTP protocol which does not retain any information about user requests  The concept of state.
PHP Programming with MySQL Slide 10-1 CHAPTER 10 Managing State Information.
Test-Driven Development Learn the "Test First" Approach to Coding SoftUni Team Technical Trainers Software University
Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University
Web Database Programming Week 7 Session Management & Authentication.
Cookies and Sessions IDIA 618 Fall 2014 Bridget M. Blodgett.
Trainers Team Ivan Yonkov Rated in the top 7% at Stack Overflow
AMD and RequireJS Splitting JavaScript Code into Dependent Modules Software University Technical Trainers SoftUni Team.
Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University
Jekyll Static Site Generator Template-Based Site Generation Svetlin Nakov Technical Trainer Software University
Forms Overview, Query string, Submitting arrays, PHP & HTML, Input types, Redirecting the user Mario Peshev Technical Trainer Software.
Working with Forms in PHP HTTP GET / POST, Validation, Escaping, Input Types, Submitting Arrays, URL Redirecting, PHP Superglobals Svetlin Nakov Technical.
1 State and Session Management HTTP is a stateless protocol – it has no memory of prior connections and cannot distinguish one request from another. The.
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)
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.
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.
ORM Basics Repository Pattern, Models, Entity Manager Ivan Yonkov Technical Trainer Software University
Test-Driven Development Learn the "Test First" Approach to Coding Svetlin Nakov Technical Trainer Software University
Sets, Dictionaries SoftUni Team Technical Trainers Software University
HTTP Transactions 1. 2 Client-Server Model 3 HTTP HyperText Transport Protocol Native protocol for WWW Sits on top of internet’s TCP/IP protocol HTTP.
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.
Unit 4 Working with data. Form Element HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes, radio-buttons,
Doctrine The PHP ORM SoftUni Team Technical Trainers Software University
Creating Content Defining Topic, Creating Technical Training Materials SoftUni Team Technical Trainers Software University
Web Storage and Cookies Cookies, Local and Session Storage SoftUni Team Technical Trainers Software University
First Steps in PHP Creating Very Simple PHP Scripts SoftUni Team Technical Trainers Software University
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Introduction to MVC SoftUni Team Introduction to MVC
Deploying Web Application
State Management Cookies, Sessions SoftUni Team State Management
MVC Architecture. Routing
Caching Data in ASP.NET 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.
Software Quality Assurance
JavaScript: ExpressJS Overview
<?php require("header.htm"); ?>
Web Programming Language
Presentation transcript:

Sessions and Cookies State Management, Cookies, Sessions, Hidden Fields SoftUni Team Technical Trainers Software University

Table of Contents 1.State Management in Web Applications 2.Working with Cookies 3.Working with User Sessions  Implementing Session-Based Counter  Implementing Login / Logout 4.Hidden Fields 5.Parameterized Address 2

 The HTTP protocol is stateless  No built-in way to implement a stateful interaction (conversation)  Ways to preserve state between the HTTP requests:  Cookies (used by the PHP session)  Hidden fields (used to pass hidden data between pages)  Can be combined with HTML5 local storage / session storage  Parameterized addresses (used to implement cookieless sessions)  Session state is used in most Web applications: login / logout State Management in Web Applications

Cookies Working with Cookies in PHP

5  Cookie == a small piece of data (up to 4KB)  Sent to the Web browser by the Web server  Saved locally inside the browser  Sent back by the browser in all subsequent requests  Cookies are created through the HTTP response header:  Browser sends the cookie back in the subsequent HTTP requests: What is a Cookie? Set-Cookie: UserID=baj.ivan; path=/; domain=nakov.com; Expires=Wed, 14 Jun :18:14 GMT Cookie: UserID: baj.ivan;

 Send cookies to be stored in the client's browser  setcookie(name, value, expiration)  Reading the cookies sent by the browser  $_COOKIE['cookie_name'] Cookies in PHP: $_COOKIE and setcookie() setcookie("user", "Nakov", time() + 5); // expires in 5 sec. if (isset($_COOKIE["user"])) { echo "Welcome ". $_COOKIE["user"]. "! "; }

7 Cookies – Example <html><body><?php if (isset($_COOKIE["user"])) : echo "Welcome ". $_COOKIE["user"]; echo "Welcome ". $_COOKIE["user"]; else : echo "Welcome guest!"; echo "Welcome guest!";endif; setcookie("user", "Nakov", time() + 5); // expires in 5 sec. ?></body></html>Cookies-Example.php

Using Cookies in PHP Live Demo

Sessions Session Management in PHP

10  A user session is a way to store data (in variables) to be shared between multiple server-side scripts (pages)  Session data is stored at the server-side  Survives during subsequent HTTP requests  Usually implemented by cookies + server-side session storage  In PHP session data is stored at the server in text files  Session data files are stored in the TEMP directory: /tmp  Can be configured to keep session data in memory or in database What is Session?

11  Sessions hold user-specific data at the server side  Sessions are automatically managed by the server-side runtime  PHP, ASP.NET and Java maintain a session object automatically  Each user browser has different user session  If you open the same site in Chrome and Firefox  You will have two different sessions (different users)  If you open the same site in two tabs in the same Web browser  Both tabs will share the same session data User Sessions: Concepts

 In PHP $_SESSION is a global array holding the session variables  After session_start() it is auto maintained at the server-side  Cookies are automatically maintained by PHP to support the sessions  Developers just store and read values from $_SESSION[…] PHP Sessions: $_SESSION and session_start() <?phpsession_start(); if (!isset($_SESSION['count'])) { $_SESSION['count'] = 0; $_SESSION['count'] = 0;} echo "Session counter: ". ++$_SESSION['count']; Session-Counter.php

13  At the first request a cookie PHPSESSID is sent to the browser  Holds a unique PHP session identifier  Generated at the server by crypto algorithm  Based on remote IP, current time + more PHP Sessions in Action: First Request

14  The browser sends back the PHPSESSID cookie at each subsequent request  Session dies when the browser is closed  No timeout by default (in the PHP implementation) PHP Sessions in Action: Next Request

Session-Based Counter Live Demo

16 Implementing Login / Logout in PHP <?php if (isset($_POST['user'])) { if (checkLogin($_POST['user'], $_POST['pass'])) { if (checkLogin($_POST['user'], $_POST['pass'])) { session_start(); session_start(); $_SESSION['user'] = $_POST['user']; $_SESSION['user'] = $_POST['user']; header('Location: main.php'); die; header('Location: main.php'); die; } echo 'Error: Invalid login.'; echo 'Error: Invalid login.'; } ?> Username: Username: Password: Password: </form>login.php

17 Implementing Login / Logout in PHP (2) Hi,, Hi,, how are you? how are you? This page is for logged-in users only. This page is for logged-in users only. main.php <?php session_start(); if (isset($_SESSION['user'])) : ?> User: User: [Logout] [Logout] <?php else : header('Location: login.php'); header('Location: login.php'); die; die; endif; ?> auth_header.php

18 Implementing Login / Logout in PHP (3) <?phpsession_start(); session_destroy(); // Delete all data in $_SESSION[] // Remove the PHPSESSID cookie $params = session_get_cookie_params(); setcookie(session_name(), '', time() , $params["path"], $params["domain"], $params["path"], $params["domain"], $params["secure"], $params["httponly"] $params["secure"], $params["httponly"]); header('Location: login.php'); die;logout.php

Implementing Login / Logout in PHP Live Demo

Hidden Fields Preserving State in Hidden Form Fields

21  HTML hidden form fields  Hold text data in the HTML form  Submitted as part of the form data  Not visible to the user (visible through the Browser inspector)  Hidden fields can preserve data between HTTP requests  Hidden fields data is loaded at some source page (PHP script)  Submitted to some destination page (PHP script) HTML Hidden Form Fields Hidden data

22  Scenario:  Step1-Name.php enters customer name  Posts the data to Step2-Address.php  Step2-Address.php enters customer address  Saves the customer name in hidden field  Posts both customer name (hidden) + address (visible)  Step3-Confirm.php shows customer data  Both customer name and address come as POST data Transferring Data with Hidden Fields

23 Transferring Data with Hidden Fields Name: Name: </form>Step1-Name.php <input type="hidden" name="name" <input type="hidden" name="name" value=" " /> value=" " /> Address: Address: </form>Step2-Address.php Name: Name: <br/> Address: Address: Step3-Confirm.php

Transferring Data with Hidden Fields Live Demo

Parameterized Addresses Preserving State in URL Parameters

26  The idea is to hold state in the URL query strings  Setting the parameters in the URL of a page after the " ? " sign:  Reading a query parameter:  Used to pass data from one page to another  Not popular technique (need to re-pass the parameters)  Sessions and hidden fields work better Parameterized Addresses $selectedTabID = $_GET['tabid'];

Using Parameterized Addresses Live Demo

? ? ? ? ? ? ? ? ? Sessions and Cookies

License  This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" licenseCreative Commons Attribution- NonCommercial-ShareAlike 4.0 International 29

Free Software University  Software University Foundation – softuni.orgsoftuni.org  Software University – High-Quality Education, Profession and Job for Software Developers  softuni.bg softuni.bg  Software Facebook  facebook.com/SoftwareUniversity facebook.com/SoftwareUniversity  Software YouTube  youtube.com/SoftwareUniversity youtube.com/SoftwareUniversity  Software University Forums – forum.softuni.bgforum.softuni.bg