Cookies & Session Web Technology

Slides:



Advertisements
Similar presentations
LIS651 lecture 3 taming PHP Thomas Krichel
Advertisements

LIS651 lecture 3 functions & sessions Thomas Krichel
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.
6/10/2015Cookies1 What are Cookies? 6/10/2015Cookies2 How did they do that?
Website Development Registering Users – Introducing Cookies.
Chapter 10 Managing State Information Using Sessions.
©2009 Justin C. Klein Keane PHP Code Auditing Session 7 Sessions and Cookies Justin C. Klein Keane
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.
Php cookies & sessions.
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.
First Name Last Name Please enter your logon information: John Submit Chen Web Server Login.php Web Server Hello John Chen Greetings. php Please enter.
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.
Creating Databases for Web Applications cookie examples lab time: favorites cookies & Sessions class time for group work/questions on projects Next class:
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.
PHP1-1 PHP Lecture 2 Xingquan (Hill) Zhu
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
Cookies Web Browser and Server use HTTP protocol to communicate and HTTP is a stateless protocol. But for a commercial website it is required to maintain.
Slide 7-1 CHAPTER 7 Managing Multiple-Form Applications: Writing scripts with multiple screens.
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.
11 1 Cookies CGI/Perl Programming By Diane Zak Objectives In this chapter, you will: Learn the difference between temporary and persistent cookies.
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.
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.
CP476 Internet Computing CGI1 Cookie –Cookie is a mechanism for a web server recall info of accessing of a client browser –A cookie is an object sent by.
Sessions and Cookies State Management, Cookies, Sessions, Hidden Fields SoftUni Team Technical Trainers Software University
How to maintain state in a stateless web Shirley Cohen
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)
Cookies / Sessions Week 10 TCNJ Web 2 Jean Chu. Webpages have no memories.
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.
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.
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.
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.
COOKIES AND SESSIONS.
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
Cookies Tutorial Cavisson Systems Inc..
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.
Sessions and cookies (part 2)
ITM 352 Cookies.
Maintaining State in PHP Part II - Sessions
Client / Session Identification Cookies
Web Programming Language
Cookies and Sessions in PHP
<?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.
Cookies and Sessions Part 2
Maintaining State in PHP Part II - Sessions
Web Programming Language
Cookies and sessions Saturday, February 23, 2019Saturday, February 23,
Cookies and Sessions.
Web Programming Language
Presentation transcript:

Cookies & Session Web Technology

Introduction HTTP is stateless and cannot keep information over a series of accesses. We need to let the server know that this browser is the one that works on the previous page This user is still looking for more products after some he just selected. We need some mechanism to provide memory for a web server Cookies: Browser stores information on client’s side Session: Server carries over the information for the browser.

What Are Cookies? Cookies were developed to maintain state between subsequent visits to a webpage, or between visits to different pages within a website. Cookies enable web servers to store and retrieve data on the clients hard drive. Webapp can track a clients path through a website. E-commerce may store items selected by a customer. A membership site might remember an ID for every use Cookies can be used to store data on client.

Cookies Restrictions Scope of Cookies Expiry information (e.g. 01/01/2004, 03:00:00) Path information (e.g. /cgi-bin/php) Domain information (e.g. webserver.com) A secure parameter (cookies are sent only over secure channel (i.e. HTTPS) Parameter Name Default Value path “/” (all directories on the server) Domain The domain of server that set the cookies Expire information Until the browser is closed. Secure Disabled

Our First Cookie <? $_COOKIE['count']++; setcookie("count", $_COOKIE['count'] ); $count = $_COOKIE['count']; echo "You have been here $count ".($count>1?"times":"time"); ?> <? echo “ABC”; $_COOKIE['count']++; setcookie("count", $_COOKIE['count'] ); $count = $_COOKIE['count']; echo "You have been here $count ".($count>1?"times":"time"); ?> ABC Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\webtech\cookie\index.php:2) in C:\xxx\index.php on line 4

setcookie() Function cookiename: value to be used for accessing cookie value: value to be stored in cookiename lifetime: time when cookie will expire (unit in seconds since the start of cookie) path: subset of paths for which cookie is valid domain: which servers cookie will be sent secure: prevent cookies being sent over an insecure connection (standard HTTP) int setcookie(string cookiename, string [value], int [lifetime], string [path], string [domain], int [secure];

Setting Cookies Setting cookie expiration Setting cookie path Setting cookie domain $expt = time()+60; setcookie("count", $count, $expt); //Cookie’s life is 60 seconds (1 minute) setcookie("count", $count, 0, “./webtech”); // Allowing to use cookies // under director “webtech” setcookie("count", $count, 0, “./”, “.ced.kmutnb.ac.th”); // Allowing to access any directories on any server that ends with “ced.kmutnb.ac.th”

Delete Cookies Set nothing to cookie name will delete it If we want to delete the previous one and create it again, the order is confusing like this <? setcookie("username"); ?> <? //set the new one setcookie("username", "Joe"); //delete the old one setcookie("username"); ?>

Check for Cookie Support <? if(empty($_GET['check'])) { //1. Set cookie and redirect to itself $page = $PHP_SELF."?check=1"; setcookie("testcookie", "1"); // set cookie header("Location: $page"); //redirect to itself with check variable } else { //2. Check if the test cookie is set if(empty($_COOKIE['testcookie'])) { echo "Your browser does not support cookie. Please enable cookies."; }else { echo "Your browser supports cookies, OK."; setcookie("testcookie"); // Delete test cookie, then redirect //header("Location: mainpage.php"); //Redirect to the page we wish } ?>

Cookies & Session Web Technology

Session Sessions use a cookie called PHPSESSID When a session starts, PHP checks for this cookie and sets it if it doesn't exist PHPSESSID cookie is a random alphanumeric string. Each web client gets a different session ID, session ID in the PHPSESSID cookie identifies that web client uniquely to the server. We can create session variables to store information and carry it over until the session ends or browser is closed.

Store and Retrieve Information Session data is stored in the $_SESSION array We use session_start() to initiate a session To end a session, we use session_destroy() or close browser). <? session_start( ); // start a session $_SESSION['count'] = $_SESSION['count'] + 1; print "You've looked at this page " . $_SESSION['count'] . ' times.'; ?> <? session_destroy( ); // End the session ?>

Login Page

Using Session Variable for Login Page <? session_start(); if(isset($_SESSION['tct'])) session_destroy(); if($_POST['submit']=="Login") { if($_POST['txtUser']=="tct" && $_POST['txtPass']=="tct") $_SESSION['tct'] = "OK"; header('Location: menu.php'); } $_SESSION['tct'] = "FAILED"; ?> <html><head><title>Login Page</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <form action="<? echo $_SERVER["PHP_SELF"]; ?>" method="post"> …………………See Next Slide………………..

Using Session Variable for Login Page (Cont.) <table width="20%" border="1" align="center"> <tr> <td width="14%"><strong>User</strong></td> <td width="86%"><input type="text" name="txtUser" value=""></td> </tr> <td><strong>Passwd</strong></td> <td><input type="password" name="txtPass" value=""></td> <td colspan="2" align="center"><input type="reset" value="Cancel"><input type="submit" name="submit" value="Login"></td> </table> </form> </body> </html>

Checking Successful Login All pages that are under login control must include this piece of code at the top of the page. (xxx.php); <? session_start(); if(!isset($_SESSION['tct'])) { header( 'Location: login.php' ) ; } ?> Note: This code is saved under chk_login.php.

Menu Page Under Login Control <? include('chk_login.php'); // ?> <html> <head> <title>Menu</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <a href="http://www.sun.com">Sun</a><BR> <a href="login.php">Logout</a> echo $_REQUEST['PHPSESSID']."<HR>"; </body> </html>