PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)

Slides:



Advertisements
Similar presentations
LIS651 lecture 3 taming PHP Thomas Krichel
Advertisements

LIS651 lecture 3 functions & sessions Thomas Krichel
CookiesPHPMay-2007 : [‹#›] Maintaining State in PHP Part I - Cookies.
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.
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.
©2009 Justin C. Klein Keane PHP Code Auditing Session 7 Sessions and Cookies Justin C. Klein Keane
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.
CSE 154 LECTURE 13: SESSIONS. Expiration / persistent cookies setcookie("name", "value", expiration); PHP $expireTime = time() + 60*60*24*7; # 1 week.
Php cookies & sessions.
1 The World Wide Web. 2  Web Fundamentals  Pages are defined by the Hypertext Markup Language (HTML) and contain text, graphics, audio, video and software.
Chapter 10 Maintaining State Information Using Cookies.
Objectives Learn about state information
 A cookie is a piece of text that a Web server can store on a user's hard disk.  Cookie data is simply name-value pairs stored on your hard disk by.
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.
PHP Tutorial - Anas Jaghoub Chapter 2 Control Structures.
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.
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:
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
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.
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.
Web Database Programming Week 7 Session Management & Authentication.
Cookies and Sessions IDIA 618 Fall 2014 Bridget M. Blodgett.
ECMM6018 Enterprise Networking for Electronic Commerce Tutorial 7
PHP Session ISYS 475. Session The web server starts a session when a visitor visiting your web site and assigns a unique id, the session id for the session.
How to maintain state in a stateless web Shirley Cohen
Sessions Brendan Knight A visitor accessing your web site is assigned a unique id. This id links to specific data that remains on the server. Sessions.
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.
 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.
1 CS428 Web Engineering Lecture 22 Building Dynamic Web pages (PHP - V)
Web Page Designing With Dreamweaver MX\Session 1\1 of 9 Session 3 PHP Advanced.
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
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.
LIS651 lecture 3 functions & sessions
CSE 154 Lecture 20: Cookies.
CGS 3066: Web Programming and Design Spring 2016
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.
ITM 352 Cookies.
Cookies and Sessions in PHP
Open Source Programming
<?php require("header.htm"); ?>
CSE 154 Lecture 21: Sessions.
PHP-II.
Presentation transcript:

PHP and Sessions

Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses) that take place between 2 computers during a set period of time.  There are many kinds of sessions in the “Computer World” that even you have experienced. ssh / telnet session sftp session Session between your app and a server –like a Bank of America App that communicates with the Bank server

Session – a web definition When we think about Sessions in terms of Web Systems we add to the general definition The interactions (requests and responses) that take place between 2 computers during a set period of time. PLUS, we typically store data (persistence) between these requests and response. This data will go away once the session is ended. Languages built for the web (or that are useful for the web) will have built-in code to handle Web Sessions

PHP Sessions In PHP, we have the ability to:  Start a session  Grab existing session  Add data ‘to” a session  Remove data “from” a session  Set the lifetime of a session  Destroy (kill/end) a session A PHP session variable is used to store information about, or change settings for a user session. Session variables hold information about one single user, and are available to all pages in one application.

PHP Sessions In PHP, we have a special pre-defined array we can use to store session data in:  $_SESSION[] This is an associative array (key to values) where $_SESSION[‘the_name’] is the data value associated with the key ‘the_name’…..

PHP Sessions Remember our Session data holds information about one single user (client) during its session with another computer (server) By default (though you can alter this), any php program served from the same Server and base URL has access to the same $_SESSION[] data. So if you have a cart.php and a processorder.php both coming from you account in puzzle –they have access to the same $_SESSION[] data with the client invoking those php programs.

PHP Sessions session_start() Before you can store user information in your PHP session, you must first start up the session. NOTE: this function will create a new session if none exists between the client and server OR if one exists will “grab” the session and populate $_SESSION[] array --- THIS IS DONE FOR YOU by the PHP interpreter/Apache server. The session_start() function must appear BEFORE the tag

PHP Sessions $_SESSION['views']=1; if(isset($_SESSION['views'])) $_SESSION['views']=$_SESSION['views']+1; else $_SESSION['views']=1; echo "Views=". $_SESSION['views'];

Example --- See our website <?php session_start(); //if session variable already exists then increment it by 1 //else set to 1 if(isset($_SESSION['views'])) $_SESSION['views'] = $_SESSION['views']+ 1; else $_SESSION['views'] = 1; echo "views = ". $_SESSION['views']; ?>

Example --- See our website <?php session_start(); // store session data $_SESSION['views'] = 1; //retrieve data echo "Pageviews = ". $_SESSION['views']; ?>

Example See our website – VIEWS Counter <?php session_start(); //if session variable already exists then increment it by 1 //else set to 1 if(isset($_SESSION['views'])) $_SESSION['views'] = $_SESSION['views']+ 1; else $_SESSION['views'] = 1; echo "views = ". $_SESSION['views']; ?> Hit it 2 times Hit it 3 times

PHP Sessions – removing data unset($_SESSION['views']); The unset() function is used to free the specified session variable <?php session_start(); //removes session variable cart if it exists if(isset($_SESSION['cart'])) unset($_SESSION['cart']); ?>

PHP Sessions --destroying (killing) session_destroy(); will reset your session and you will lose all your stored session data. <?php session_start(); //intermediate code //..... //ready to destory session session_destroy(); ?>

Another page visit example

visit.php <?php session_start(); $current=time(); // look at the current time if($_SESSION[last_click]) { $passed=$current-$_SESSION[‘last_click’]; $to_print.="$passed seconds have passed since your last visit.\n"; $_SESSION[‘last_click’]=$current; } else { $to_print="This is your first visit.\n"; $_SESSION[‘last_click’]=$current; } print "$top\n$to_print\n$bottom"; ?> FIRST TIME: Your Visit Status This is your first visit. Thank you and please return SECOND TIME Your Visit Status 43 seconds have passed since your last visit. Thank you and please return

YOU CAN ALSO STORE INFORMATION ON THE CLIENT CALLED COOKIES THAT IS AUTOMATICALLY SENT TO SERVER WHEN CLIENT RE- REQUESTS THAT SERVER Did you know….

This is how Amazon knows your name See it knows about “Behzad’s Amazon”

Cookies Cookies are (name, value) pairs that are stored in the Client machine (in our case the client SW is a browser and it does this storing for you in a file) that is persistent –and it is returned to the Server everytime you go back to same URL/Server.

cookies A cookie is a piece of attribute/value data. A server can send cookies as value of a HTTP header Set-Cookie:. Multiple headers may be sent. When the client visits the web site again, it will send the cookie back to the server with a HTTP header Cookie:

Set-Cookie Set-Cookie: name=value; [expires= date;] [path=path;] [domain= domain] [secure] where  name= is the variable name set in the cookie  value= is the variable's value  date= is a date when the cookie expires  path= restricts the cookie to be sent only when requests to a path starting with path are made  domain= restricts the sending of the cookie to a certain domain  secure restricts transmission to https

Cookies: The browser compares the request it wants to make with the URL and the domain that sent the cookie. If the path is not set the cookie will only be sent to a request with the originating URL. If the cookie matches the request a request header of the form Cookie: name1=value1 ; name2=value2 is sent.

22 PHP and Cookies Cookies in PHP are fairly easy to use:  setcookie() function is called to create a cookie that will be sent to the client See As always with cookies, they must be sent with the http header  Thus, you should determine and set any cookies in PHP mode prior to using any html (or even simple text)  $_COOKIE array contains the cookies received back from the client machine Cookies sent to client by server previously Associative array allows access of cookies by name

Confused --- Sessions and Cookies