CGS 3066: Web Programming and Design Spring 2016

Slides:



Advertisements
Similar presentations
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.
Advertisements

©2009 Justin C. Klein Keane PHP Code Auditing Session 7 Sessions and Cookies Justin C. Klein Keane
Php cookies & sessions.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
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.
PHP Tutorial - Anas Jaghoub Chapter 2 Control Structures.
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.
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.
Website Development with PHP and MySQL Saving Data.
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
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.
1 Chapter 7 – Object-Oriented Programming and File Handling spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information.
Storing and Retrieving Data
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
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)
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)
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.
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.
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
Week 7 Server side programming PHP Scripting Language MySQL Database Apache Server IT4103 Web Programming
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.
Brad N Greenwood, PhD MBA
Sessions and cookies MIS 3501 Jeremy Shafer Department of MIS
CSE 154 Lecture 20: Cookies.
CHAPTER 5 SERVER SIDE SCRIPTING
PHP Cookies What is a Cookie?
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.
1 CHAPTER 10 ADVANCED PHP.
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
<?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
Sessions and cookies MIS 3501 Jeremy Shafer Department of MIS
Web Programming Language
CSE 154 Lecture 22: Sessions.
SESSION TRACKING BY DINESH KUMAR.R.
Cookie and Session Bayu Priyambadha, S.Kom.
Cookies and Sessions.
Web Programming Language
Advanced Concepts and AJAX
Lecture 6: Processing Forms with PHP
PHP-II.
[Based in part on SWE 432 and SWE 632 materials by Jeff Offutt, GMU]
Presentation transcript:

CGS 3066: Web Programming and Design Spring 2016 Session and Cookies Partially adapted from “Introduction to Server-Side Programming”Charles Liu http://6.470.scripts.mit.edu/2014/slides/php_ppt.pdf

Cookies and Sessions Sometimes we need to keep track of information about the web user/client between consecutive HTTP requests Example: Shopping Cart “Remember Me”

Cookies Cookies are files stored on the client side Contains relevant data in name-value pairs Comes with expiration dates. Expired cookie contents are no longer accessed by the browser Can be managed by server-side scripts(PHP) Relevant cookies are automatically submitted from client to server with HTTP request PHP stores information in $_COOKIE superglobal

Setcookie() To set a cookie in PHP: setcookie(name, value) Or, setcookie(name, value, time_of_expiry) Time of expiry entered in seconds. Present time + time in seconds until expiration Present time can be looked up using PHP time() function

Setcookie() Example: setcookie("TestCookie", ”testvalue”); setcookie("TestCookie", ”testvalue”,  time()+3600);  //set to expire after 1 hour from present time Once set, S_COOKIE[‘TestCookie’] will have value ‘testvalue’ Always check with isset($_COOKIE[$cookie_name]) before trying to use the cookie’s value To delete a cookie, set a new cookie with same arguments but expiration in the past (e.g. 1)

Sessions data stored on the server, managed by Server-side script(PHP) In PHP, session variables store information about user session in $_SESSION superglobal array. Session variables hold information about one single user, and are available to all pages in one application. Session variables expire when the browser is closed

PHP Session management session_start() Before you can store user information in your PHP session, you must first start up the session. The session_start() function must appear at the top of EVERY page, BEFORE the <html> tag

Example Code <?php $_SESSION['views']=1; if(isset($_SESSION['views'])) $_SESSION['views']=$_SESSION['views']+1; else $_SESSION['views']=1; echo "Views=". $_SESSION['views']; ?>

Session Management functions unset(): function used to free the specified session variable Example: unset($_SESSION[‘shopping_cart']); session_destroy(); Resets current session. You will lose all your stored session data. Call when a user signs out

PHP File Handling

Open/Close file in PHP A file on the server-side file system is opened with fopen() Fopen() returns a ‘handle’ to the file that can be used to reference the opened file Each file is opened in a particular mode. A file is closed with fclose()

fopen() Used to create/open a file on the server Parameters: filename with optional pathname, one of the following modes: Example: $myfile = fopen("testfile.txt", "w") 'r' Open for reading only; place the file pointer at the beginning of the file. 'r+' Open for reading and writing; place the file pointer at the beginning of the file. 'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. For more details and complete list of modes: http://php.net/manual/en/function.fopen.php

Read a File using fread() Syntax: fread(open_file_handle, length_in_bytes); Reads content of a specific length from the file $my_file = 'file.txt'; $handle = fopen($my_file, 'r') or die('Cannot open file’); //will read upto 4096 characters or until the end-of-file //whichever comes first $data = fread($handle,4096);

Read a File using fgets() Syntax: fgets(open_file_handle) Reads upto the next newline $my_file = 'file.txt'; $handle = fopen($my_file, 'r') or die('Cannot open file’); //reads one line from the file and echoes to HTTP Response echo fgets($my_file);

Write to a File using fwrite() $my_file = 'file.txt'; $handle = fopen($my_file, 'w') or die('Cannot open file’); $data = ‘data to be written to file'; fwrite($handle, $data);

Reset file pointer using fseek() Syntax: fseek($handle, $offset) Updates file pointer position to $offset bytes from the file beginning Used to “jump to” a specific part of the file To rewind back to the beginning, call fseek($handle,0);