Implementing Cookies in PHP

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.
Creating Databases for Web Applications Courses example Persistent information. Cookies. Session Homework: Examine a computer (your own or in a lab) for.
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.
Web Client/Server Communication A290/A590, Fall /09/2014.
1 Cookies & Session Variables. 2 Managing Sessions and Using Session Variables You may have heard that HTTP, the protocol on which the Web runs, is a.
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.
CSC 2720 Building Web Applications Cookies, URL-Rewriting, Hidden Fields and Session Management.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Includes and Dates.
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.
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.
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.
PHP2. PHP Form Handling The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. Name: Age:
Cookies & Session Web Technology
11 1 Cookies CGI/Perl Programming By Diane Zak Objectives In this chapter, you will: Learn the difference between temporary and persistent cookies.
Dynamic Programming with PHP (mktime), Cookies, SQL, Authentication.
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.
Cookies and Sessions IDIA 618 Fall 2014 Bridget M. Blodgett.
The Problem of State. We will look at… Sometimes web development is just plain weird! Internet / World Wide Web Aspects of their operation The role of.
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.
ECMM6018 Enterprise Networking for Electronic Commerce Tutorial 7
 Previous lessons have focused on client-side scripts  Programs embedded in the page’s HTML code  Can also execute scripts on the server  Server-side.
CHAPTER 8 PHP Advanced อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
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)
8 th Semester, Batch 2008 Department of Computer Science SSUET.
 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.
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.
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.
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
File Uploads and Cookies Pat Morin COMP Outline File upload Cookies.
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.
Cookies Tutorial Cavisson Systems Inc..
CSE 154 Lecture 20: Cookies.
CHAPTER 5 SERVER SIDE SCRIPTING
PHP Cookies What is a Cookie?
CGS 3066: Web Programming and Design Spring 2016
Play Framework: Introduction
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)
Cookies and Sessions in PHP
14-мавзу. Cookie, сеанс, FTP и технологиялари
<?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.
PHP State.
Cookies and Sessions.
PHP Forms and Databases.
PHP an introduction.
Advanced Concepts and AJAX
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 time the same computer.
[Based in part on SWE 432 and SWE 632 materials by Jeff Offutt, GMU]
Presentation transcript:

Implementing Cookies in PHP various aspects of cookies in PHP Creating a cookie Accessing a set cookie Deleting a cookie

Creating a cookie You need three basic functions to create a cookie. setcookie() time() mktime() The setcookie() Function You can create a cookie in PHP by using the setcookie() function. The Web server uses this function to set a cookie and send it to the client when the client requests a connection. setcookie (string name, string value, int expiry, string path, string domain, int secure);

Syntax of setcookie() As the above syntax specifies, the setcookie() function takes six arguments. These include: name: This argument specifies the name of the variable that will hold the corresponding value in the name-value pair. value: This argument holds the value of the variable specified by the name argument. expiry: This argument specifies the time after which the cookie will be considered invalid and thus rendered unusable. Note The value of the expiry argument is specified by two functions, time() and mktime(), about which you’ll learn a bit later in this chapter. path: This argument specifies the hierarchy of files and directories on the Web server for which the cookie is considered to be valid. Secure: This argument determines whether the cookie must be transferred through a secure channel. If no value is specified for the argument, an insecure channel is used to transfer the cookie. However, if the value of this argument is 1, the cookie is transmitted using the HTTPS protocol that ensures secure transactions over the Internet.

The time() and mktime() Functions You can use the time() and mktime() functions to set the life span of a cookie to avoid the expiry of the cookie when the browser window closes. The time() function is used to determine the current time. It returns time in seconds. The syntax of the time() function is specified below. int time(); The mktime() function, on the other hand, accepts units of time, such as hours, minutes, seconds, months, and years, as parameters and is used to convert this information into a time stamp. Just like the time() function, the time stamp returned by the mktime() function is in seconds. The syntax of the mktime() function is specified below. int mktime(int hour, int minute, int second, int month, int year);

<?php $name = "MyCookie"; $value = "This is my first Cookie!"; $expiry = mktime(0, 0, 0, 7, 1, 2022); $domain = "localhost.localdomain"; setcookie($name, $value, $expiry, /, $domain, 0); ?> <html> <head> <title> Testing the Cookie </title> </head> <body> <br> <br> <br> <h1 align = center> This appears when the cookie is created successfully! </h1> </body> </html>

Accessing a cookie $_COOKIE [ “cookie_name”] Using the name of the cookie as a variable. When you try to access a cookie in PHP, a variable with the same name as the cookie is created. This variable name, then, can be used by your scripts. As a result, you can either reload the page that was used to create the cookie or load another page that uses the cookie. Suppose you created a cookie called My_Cookie. You can use the name of the cookie as a variable in your script as specified below: echo "My_Cookie"; Using $HTTP_COOKIE_VARS [“cookie_name”]. Again, the variable that is created by default in PHP by using the cookie name is also stored in the global array as $HTTP_COOKIE_VARS ["cookie_name"]. An additional advantage of using this global array is that it helps in distinguishing between different cookie names on the basis of their data sources, even if they happen to have the same name. You can use this global array in your PHP scripts as given below: echo "$HTTP_COOKIE_VARS ["My_Cookie"]" $_COOKIE [ “cookie_name”]

Deleting Cookies After a cookie has served its purpose and you don’t need it any longer, you might want to delete it. No use unnecessarily storing unwanted cookies! PHP does not boast of an exclusive function for this purpose. In fact, the good old setcookie() function once again comes to the rescue! Call the setcookie() function with only the name parameter when you want to delete a cookie. The syntax to delete a cookie is: setcookie("cookie_name");