Sessions in PHP – Page 1 of 13CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: Sessions in PHP Reading: Williams.

Slides:



Advertisements
Similar presentations
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK)
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.
PHP and the Web: Session : 4. Predefined variables PHP provides a large number of predefined global variables to any script which it runs also called.
Chapter 10 Managing State Information Using Sessions.
©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.
Chapter 10 Maintaining State Information Using Cookies.
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.
Cookies Set a cookie – setcookie() Extract data from a cookie - $_COOKIE Augment user authentication script with a cookie.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
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.
CSE 154 LECTURE 12: COOKIES. Including files: include include("filename"); PHP include("header.html"); include("shared-code.php"); PHP inserts the entire.
SHOPPING CARTS CHAPTER 19. E-COMMERCE Typically, an e-commerce site will have public pages and admin pages.
Web Programming Language Week 7 Dr. Ken Cosh Security, Sessions & Cookies.
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.
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
1 Maryland ColdFusion User Group Session Management December 2001 Michael Schuler
Chapter 6 Server-side Programming: Java Servlets
Cookies & Session Web Technology
Prof Frankl, Spring 2008CS Polytechnic University 1 Overview of Web database applications with PHP.
PHP Workshop ‹#› Maintaining State in PHP Part II - Sessions.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
SessionsPHPApril 2010 : [‹#›] Maintaining State in PHP Part II - Sessions.
Dynamic Programming with PHP (mktime), Cookies, SQL, Authentication.
PHP. $_GET / $_POST / $_SESSION PHP uses predefined variables to provide access to important information about the server and requests from a browser.
Web Database Programming Week 7 Session Management & Authentication.
Cookies and Sessions IDIA 618 Fall 2014 Bridget M. Blodgett.
Sessions and Cookies State Management, Cookies, Sessions, Hidden Fields SoftUni Team Technical Trainers Software University
Database Access Control IST2101. Why Implementing User Authentication? Remove a lot of redundancies in duplicate inputs of database information – Your.
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)
PHP-language, sessions Teppo Räisänen Principal Lecturer Oulu University of Applied Sciences School of Business and Information Management
PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)
1 DIG 3134 Lecture 6: Maintaining State Michael Moshell University of Central Florida Media Software Design.
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.
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.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
CGS 3066: Web Programming and Design Spring 2016 PHP.
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.
Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy.
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
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.
Managing State Chapter 13.
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.
ITM 352 Cookies.
Maintaining State in PHP Part II - Sessions
Web Programming Language
Web Systems Development (CSC-215)
<?php require("header.htm"); ?>
CSE 154 Lecture 21: Sessions.
Maintaining State in PHP Part II - Sessions
Web Programming Language
CSE 154 Lecture 22: Sessions.
SESSION TRACKING BY DINESH KUMAR.R.
Cookies and Sessions.
Web Programming Language
Presentation transcript:

Sessions in PHP – Page 1 of 13CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: Sessions in PHP Reading: Williams & Lane pp

Sessions in PHP – Page 2 of 13CSCI 2910 – Client/Server-Side Programming Today’s Goals Today’s lecture will cover: An introduction to sessions, their purpose, and their use Starting and stopping a session Using session variables

Sessions in PHP – Page 3 of 13CSCI 2910 – Client/Server-Side Programming Purpose of a Session In general, HTTP is a "stateless" system, i.e., clients access documents through links without regard to past interactions This is not acceptable when it comes to managing a complex interaction with a client such as: –the use of a shopping cart; –logging into a database or other secure site; or –tracking a user's settings/data values as he or she progresses through a site.

Sessions in PHP – Page 4 of 13CSCI 2910 – Client/Server-Side Programming Mechanics of a Session Session is identified using a session ID (32 digit hexadecimal value) The session ID is transmitted between the client and server with each HTTP request and response Client keeps track of a session through the use of a cookie Server keeps track of a session through locally stored text files or a database

Sessions in PHP – Page 5 of 13CSCI 2910 – Client/Server-Side Programming Mechanics of a Session (continued) Databases are used for large traffic applications while text files are used for lower traffic. The server maintains the session variables in the text file or database. To prevent security risks due to someone hijacking an old session and to avoid clogging the server with unused sessions, the server will clean up old sessions after a specified timeout period.

Sessions in PHP – Page 6 of 13CSCI 2910 – Client/Server-Side Programming Implementing a Session session_start() – creates a new session or finds an existing session. Basically, it identifies a session and accesses the session's variables if it is an existing session. Once a session has been started, the session's variables are accessed through a superglobal associative array called $_SESSION. (This is the same sort of array as $_GET and $_POST.) Example: $_SESSION[variable_name]

Sessions in PHP – Page 7 of 13CSCI 2910 – Client/Server-Side Programming Implementing a Session (continued) Because of the dual purpose of session_start(), i.e., it can initialize a session or access an existing one, the PHP code must have a method for identifying whether a session has already been initiated. isset($_SESSION[variable_name]) can be used to determine if the session has already initialized a particular variable. For example: if (!isset( $_SESSION['quantity'] )) $_SESSION['quantity'] = 0; else $_SESSION['quantity']++;

Sessions in PHP – Page 8 of 13CSCI 2910 – Client/Server-Side Programming Implementing a Session (continued) A variable can be removed from a session using the unset() function Example: unset($_SESSION['quantity']); All session variables can be removed by simply re-initializing the $_SESSION array Example: $_SESSION = array();

Sessions in PHP – Page 9 of 13CSCI 2910 – Client/Server-Side Programming Implementing a Session (continued) Since the client receives its session ID through a cookie in one of the HTTP header files, it must be sent before any HTML is generated for the client's output. Therefore, session_start() must be executed before any output is generated.

Sessions in PHP – Page 10 of 13CSCI 2910 – Client/Server-Side Programming Session Variable Types A session variable can be of any type or object If a session variable is an object, be sure to define the object before running session_start(). If an existing session that uses an object is opened before the object is defined, it will cause problems. The following slide presents an example

Sessions in PHP – Page 11 of 13CSCI 2910 – Client/Server-Side Programming <?php session_start(); if(!isset($_SESSION['count'])) $_SESSION['count'] = 0; $_SESSION['count']++; ?> Hello, World! in PHP <?php print "You've visited ".$_SESSION['count']." times."; ?>

Sessions in PHP – Page 12 of 13CSCI 2910 – Client/Server-Side Programming Ending a Session If you receive a signal from the client that they are ending the session, e.g., clicking on a link to log out, there is a method you can use to force a session to end. –First, you must open the session so that it is identified as the one to be closed. –Next, you must end the session with the function session_destroy() –Last, use the header function to direct an output to the client.

Sessions in PHP – Page 13 of 13CSCI 2910 – Client/Server-Side Programming Ending a Session (continued) <?php // Begin by accessing the session session_start(); // Close the session session_destroy(); // Direct output to the client header("Location: logout.html"); ?>