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.

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.
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.
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. 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.
JavaScript, Fourth Edition
Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.
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.
Chapter 8 Cookies And Security JavaScript, Third Edition.
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.
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 Programming with MySQL Slide 10-1 CHAPTER 10 Managing State Information.
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.
Web Database Programming Week 7 Session Management & Authentication.
PHP Cookies. Cookies are small files that are stored in the visitor's browser. Cookies can be used to identify return visitors, keep a user logged into.
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.
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.
Programming for the Web Cookies & Sessions Dónal Mulligan BSc MA
PHP – Hypertext Preprocessor.
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● / www,histpk.org Hidaya Institute of Science & Technology
National College of Science & Information 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.
CSE 154 Lecture 20: Cookies.
PHP Cookies What is a Cookie?
Y.-H. Chen International College Ming-Chuan University Fall, 2004
CGS 3066: Web Programming and Design Spring 2016
Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
Sessions and cookies (part 2)
ITM 352 Cookies.
Maintaining State in PHP Part II - Sessions
Web Programming Language
Cookies and Sessions in PHP
Open Source Programming
Implementing Cookies in PHP
Cookies and JavaScript
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.
CSE 154 Lecture 21: Sessions.
Maintaining State in PHP Part II - Sessions
Web Programming Language
CSE 154 Lecture 22: Sessions.
CSc 337 Lecture 27: Cookies.
SESSION TRACKING BY DINESH KUMAR.R.
PHP State.
Cookies and Sessions.
Web Programming Language
PHP an introduction.
Advanced Concepts and AJAX
PHP-II.
CSc 337 Lecture 25: Cookies.
Presentation transcript:

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 the client during and between browsing sessions. A server can access only the cookies that it has placed on the client. Function setcookie takes the name of the cookie to be set as the first argument, followed by the value to be stored in the cookie. The optional third argument indicates the expiration date of the cookie. If no expiration date is specified, the cookie lasts only until the end of the current session—that is, when the user closes the browser. This type of cookie is known as a session cookie, while one with an expiration date is a persistent cookie. ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

19.10 Using Cookies (Cont.) If only the name argument is passed to function setcookie, the cookie is deleted from the client’s computer. Cookies defined in function setcookie are sent to the client at the same time as the information in the HTTP header; therefore, setcookie needs to be called before any other output PHP creates the superglobal array $_COOKIE, which contains all the cookie values indexed by their names, similar to the values stored in array $_POST when an HTML5 form is posted ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Create Cookies With PHP A cookie is created with the setcookie() function. Syntax: setcookie(name, value, expire, path, domain, secure, httponly); (Only the name parameter is required. All other parameters are optional) ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Create Cookies With PHP Parameter Description name The name of the cookie. value The value of the cookie.  expire The time the cookie expires.  path The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain. If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain. The default value is the current directory that the cookie is being set in. domain The (sub)domain that the cookie is available to. secure Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client. When set to TRUE, the cookie will only be set if a secure connection exists. httponly When TRUE the cookie will be made accessible only through the HTTP protocol.  ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

PHP Create/Retrieve a Cookie ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Modify a Cookie Value To modify a cookie, just set (again) the cookie using the setcookie() function: ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Delete a Cookie To delete a cookie, use the setcookie() function with an expiration date in the past: ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Check if Cookies are Enabled First, try to create a test cookie with the setcookie() function, then count the $_COOKIE array variable: ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Using cookies: Example ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.

PHP Sessions A session is a way to store information (in variables) to be used across multiple pages. A session is server-side information intended to exist only throughout the visitor's interaction with the website.  Unlike a cookie, the information is not stored on the users computer. When you work with an application, you open it, do some changes, and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are or what you do, because the HTTP address doesn't maintain state. Session variables solve this problem by storing user information to be used across multiple pages (e.g. username, favorite color, etc). By default, session variables last until the user closes the browser. ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Start a PHP Session A session is started with the session_start() function. Session variables are set with the PHP global variable: $_SESSION. The session_start() function must be the very first thing in your document. Before any HTML tags. Example: ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Get PHP Session Variable Values ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Show all the session variables Output: Array ( [favcolor] => green [favanimal] => cat ) ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Modify a PHP Session Variable To change a session variable, just overwrite it: ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Destroy a PHP Session To remove all global session variables and destroy the session, use session_unset() and session_destroy(): ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

19.11 Dynamic Content The isset function determines whether the $_POST array contains keys representing the various form fields. The notation$$variable specifies a variable variable, which allows the code to reference variables dynamically. You can use this expression to obtain the value of the variable whose name is equal to the value of $variable. The function mysql_real_escape_string inserts a backslash (\) before any special characters in the passed string. ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.