Open Source Programming

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.
PHP (2) – Functions, Arrays, Databases, and sessions.
Servlets and a little bit of Web Services Russell Beale.
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.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
_______________________________________________________________________________________________________________ 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.
PHP Tutorial - Anas Jaghoub Chapter 2 Control Structures.
CSE 154 LECTURE 12: COOKIES. Including files: include include("filename"); PHP include("header.html"); include("shared-code.php"); PHP inserts the entire.
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.
Nic Shulver, Introduction to Sessions in PHP Sessions What is a session? Example Software Software Organisation The login HTML.
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.
Cookies & Session Web Technology
PHP Workshop ‹#› Maintaining State in PHP Part II - Sessions.
STATE MANAGEMENT.  Web Applications are based on stateless HTTP protocol which does not retain any information about user requests  The concept of state.
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.
Web Database Programming Week 7 Session Management & Authentication.
Cookies and Sessions IDIA 618 Fall 2014 Bridget M. Blodgett.
CSCI 6962: Server-side Design and Programming Java Server Faces Scoping and Session Handling.
Copyright © 2003 Pearson Education, Inc. Slide 7-1 The Web Wizard’s Guide to PHP by David Lash.
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.
ECMM6018 Enterprise Networking for Electronic Commerce Tutorial 7
Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy.
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.
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)
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.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy.
© 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.
CHAPTER 5 SERVER SIDE SCRIPTING
Introduction to Dynamic Web Programming
CGS 3066: Web Programming and Design Spring 2016
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
PHP –MySQL Interview Question And Answer.
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.
Web Design and Development
ITM 352 Cookies.
Maintaining State in PHP Part II - Sessions
Web Programming Language
Cookies and Sessions in PHP
ISC440: Web Programming 2 Server-side Scripting PHP 3
What is Cookie? Cookie is small information stored in text file on user’s hard drive by web server. This information is later used by web browser to retrieve.
<?php require("header.htm"); ?>
Building Web Applications
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
[Based in part on SWE 432 and SWE 632 materials by Jeff Offutt, GMU]
Hypertext Preprocessor
Presentation transcript:

Open Source Programming SESSION A normal HTML website will not pass data from one page to another. (OR) In other words, all information is forgotten when a new page is loaded. This makes a quite problem for tasks like a shopping cart, which requires data (the user's selected product) to be remembered from the time of selection to billing. (i.e. one page to the next page). Cookie used for such requirements, however, limitations on cookie size and the number of cookies allowed, and various inconveniences surrounding their implementation, prompted to introduce another solution called as session handling. A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping cart items, etc). However, this session information is temporary and is usually deleted very quickly after the user has left the website that uses sessions. Open Source Programming

Open Source Programming SESSION Mechanism There are two things that the session mechanism must hang onto: the session ID itself and any associated variable bindings. The session ID is either stored as a cookie on the browser's machine, or it is incorporated into the GET/POST arguments submitted with page requests. The contents of session variables are stored in special files on the server, one file per session ID: Doing this kind of storage requires the session code to serialize the data by turning it into a linear sequence of bytes that can be written to a file and read back to recreate the data It's possible to configure PHP to store the contents of session variables in a server-side database, rather than in files Open Source Programming

Open Source Programming Storing the Sessions The session variables are stored on the web server The Path is /var/lib/php/session The file name is the session ID Eg. sess_076opqrstu56kldr670ndft0op Open Source Programming

Open Source Programming Working of a Session Sessions work by creating a unique identification(UID) number for each visitor and storing variables based on this ID. This helps to prevent two users' data from getting confused with one another when visiting the same webpage. Session_start() – it is required, for every session’s program. $_SESSION[‘session_name’] – can access the created sessions through this super global. isset($_SESSION[‘session_name’]) – used to check the availability of a session. unset($_SESSION[‘session_name’]) – deletes the session Session_destroy() – deletes all sessions. Open Source Programming

Open Source Programming Session Functions session_start — Initialize session data. session_destroy — Destroys all data registered to a session session_unset — Free all session variables session_name — Get and/or set the current session name session_register — Register one or more global variables with the current session. session_destroy — Destroys all data registered to a session. session_id — Get and/or set the current session id. Session_regenerate_id — Update the current session id with a newly generated one session_is_registered — Find out whether a global variable is registered in a session Open Source Programming

Open Source Programming Start a Session Before you can begin storing user information in your PHP session, you must first start the session. When you start a session, it must be at the very beginning of your code, before any HTML or text is sent. bool session_start (void) Sample program to Start Session: Session_start.php <?php session_start(); // starts PHP session! echo “The session was started”; ?> This tiny piece of code will register the user's session with the server, allow you to start saving user information and assign a UID (unique identification number) for that user's session. Open Source Programming

Open Source Programming Create a Session When you want to store user data in a session use the $_SESSION (super global). This is used for both store and retrieve session data. $_SESSION[‘session_name’] Sample program to Create Session: Session_Create.php <?php session_start(); // starts PHP session! $_SESSION[‘view’] = 1; // creates a new session with name “view” ?> Open Source Programming

Open Source Programming Access Session Can access the existing sessions through the following super global: $_SESSION[‘session_name’] – global array $_HTTP_SESSION_VARS[‘session_name’] – environment variables Sample program to Access a Session: Session_Access.php <?php session_start(); // starts PHP session! if (isset($_SESSION[‘view’]) // checks the availability $_SESSION[‘view’] += 1; // access the existing ?> Open Source Programming

Open Source Programming Delete Session Imagine that you were running an online business and a user used your website to buy your goods. The user has just completed a transaction on your website and you now want to remove everything from their shopping cart. Can delete the existing session by the function “unset”. The sample program shows it: Sample program to Delete Session: Session_Delete.php <?php session_start(); // starts PHP session! if (isset($_SESSION[‘view’]) // checks the existing { unset($_SESSION[‘view’]); // deletes the existing echo “The session ‘view’ was deleted”; } else echo “no session with name ‘view’”; ?> Open Source Programming

Open Source Programming Delete All Sessions Session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session. Variables associated with the session, or unset the session cookie. bool session_destroy (void) Sample program to Delete all Session: Sessions_Delete.php <?php session_start(); // starts PHP session ……… //codings session_destroy(); // deletes all sessions ?> Note: destroy will reset your session, so don't call that function unless you are entirely comfortable losing all your stored session data! Open Source Programming

Open Source Programming Session Name session_name() returns the name of the current session. bool session_name (string $name) If name is given, session_name() will update the session name and return the old session name. The session name is reset to the default value stored in session.name at request startup time. Thus, you need to call session_name() for every request Sample program for Session Name: Session_Name.php <?php // set the session name to WebsiteID  $previous_name = session_name("WebsiteID"); echo "The previous session name was $previous_name<br />"; ?> Open Source Programming

Open Source Programming Session id session_id() is used to get or set the session id for the current session. The constant SID can also be used to retrieve the current name and session id as a string suitable for adding to URLs. bool session_id (string $id) The session id can be updated by the function bool session_regenerate_id ([bool $delete_old_session=false]) Sample program for Session Name: Session_Name.php <?php session_start(); echo "session id is ".session_id()."<br>"; echo "barney is ",$_SESSION['barney']++."<br>"; ?> Open Source Programming

Session Functions session_ cache_ expire session_ cache_ limiter session_ commit session_ decode session_ destroy session_ encode session_ get_ cookie_ params session_ id session_ is_ registered session_ module_ name session_ name session_ regenerate_ id session_ register_ shutdown session_ register session_ save_ path session_ set_ cookie_ params session_ set_ save_ handler session_ start session_ status session_ unregister session_ unset session_ write_ close