CS 174: Web Programming November 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Slides:



Advertisements
Similar presentations
PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.
Advertisements

PHP I.
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.
PHP (2) – Functions, Arrays, Databases, and sessions.
Chapter 10 Managing State Information Using Sessions.
CSE 154 LECTURE 13: SESSIONS. Expiration / persistent cookies setcookie("name", "value", expiration); PHP $expireTime = time() + 60*60*24*7; # 1 week.
CS 160: Software Engineering August 27 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 174: Web Programming February 26 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
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.
Server-side Scripting Powering the webs favourite services.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
CSE 154 LECTURE 12: COOKIES. Including files: include include("filename"); PHP include("header.html"); include("shared-code.php"); PHP inserts the entire.
PHP meets MySQL.
CS 235: User Interface Design October 15 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 174: Web Programming September 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Feedback #2 (under assignments) Lecture Code:
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
School of Computing and Information Systems CS 371 Web Application Programming PHP – Forms, Cookies, Sessions and Database.
Week 7. Lecture 2 Functions, Arrays, PHP&MySQL. Function with More than one argument and a return statement For a function to return a value, the return.
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
CS 174: Web Programming September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 235: User Interface Design September 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 160: Software Engineering October 1 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Chapter 6 Server-side Programming: Java Servlets
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.
CS 160: Software Engineering October 6 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 174: Web Programming August 31 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 174: Web Programming September 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CMPE 226 Database Systems September 23 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak
Cookies and Sessions IDIA 618 Fall 2014 Bridget M. Blodgett.
CMPE 226 Database Systems October 7 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak
CS 174: Web Programming September 28 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
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.
Sessions and Cookies State Management, Cookies, Sessions, Hidden Fields SoftUni Team Technical Trainers Software University
CS 174: Web Programming October 12 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
CS 174: Web Programming October 14 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
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.
1 State and Session Management HTTP is a stateless protocol – it has no memory of prior connections and cannot distinguish one request from another. The.
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 and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)
How Web Database Architectures Work CPS181s April 8, 2003.
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.
CS 157B: Database Management Systems II April 22 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
1 CS428 Web Engineering Lecture 22 Building Dynamic Web pages (PHP - V)
AJAX CS456 Fall Examples Where is AJAX used? Why do we care?
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.
CS 160 and CMPE/SE 131 Software Engineering March 15 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy.
Managing State Chapter 13.
Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
ITM 352 Cookies.
CS 174: Server-Side Web Programming February 12 Class Meeting
<?php require("header.htm"); ?>
PHP and Forms.
CSE 154 Lecture 21: Sessions.
Client-Server Model: Requesting a Web Page
[Based in part on SWE 432 and SWE 632 materials by Jeff Offutt, GMU]
Presentation transcript:

CS 174: Web Programming November 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak More AJAX with jQuery  When using AJAX, an HTML page does not explicitly need to have a form. A JavaScript function creates a “virtual form” to pass data to a PHP page on the web server.  The PHP page only needs to generate a snippet of HTML instead of an entire page. Simpler PHP code. Less data transmission over the network. 2

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak More AJAX with jQuery, cont’d  JavaScript and jQuery directly manage the request to the web server and the response. Rather than letting it happen automatically via the web browser - web server cycle.  More control by the programmer! 3

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak Dynamically Populated Menu  Use AJAX to obtain results from a database to dynamically populate a drop-down menu: 4

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak Dynamically Populated Menu, cont’d 5 Teacher's Students Students of school/studentsof4.html

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak Dynamically Populated Menu, cont’d 6 $(init); function init() { $("#teachermenu").selectmenu(); $("#teachermenu").on("selectmenuchange", showStudents); $.get("teachers.php", null, loadMenu); } function loadMenu(data, status) { $("#teachermenu").html(data); } school/studentsof4.js

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak Dynamically Populated Menu, cont’d 7 class Teacher { private $id; private $first; private $last; public function getId() { return $this->id; } public function getFirst() { return $this->first; } public function getLast() { return $this->last; } }... $con = new PDO("mysql:host=localhost;dbname=school", "root", "sesame"); $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); school/teachers.php

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak Dynamically Populated Menu, cont’d 8 $query = "SELECT id, first, last FROM teacher ORDER BY last"; $ps = $con->prepare($query); $ps->execute(); $ps->setFetchMode(PDO::FETCH_CLASS, "Teacher"); // Construct menu options. Start with a blank option. print " $full "; while ($teacher = $ps->fetch()) { $id = $teacher->getId(); $first = $teacher->getFirst(); $last = $teacher->getLast(); $full = $first. " ". $last; print " $full "; } school/teachers.php

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak Dynamically Created Table 9  Use AJAX to obtain results from a database to dynamically create a table:

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak Dynamically Created Table, cont’d 10 function init() { $("#teachermenu").selectmenu(); $("#teachermenu").on("selectmenuchange", showStudents); $.get("teachers.php", null, loadMenu); } Teacher's Students Students of school/studentsof4.js school/studentsof4.html

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak Dynamically Created Table, cont’d 11 function showStudents(event, ui) { teacherId = $("#teachermenu").val(); $.post("students.php", {"id": teacherId}, loadTable); } function loadTable(data, status) { $("#output").html(data); } studentsof4.js

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak Dynamically Created Table, cont’d 12 class StudentSubject { private $first; private $last; private $subject; public function getFirst() { return $this->first; } public function getLast() { return $this->last; } public function getSubject() { return $this->subject; } } $teacherId = filter_input(INPUT_POST, 'id'); if ($teacherId == 0) return; school/students.php

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak Dynamically Created Table, cont’d 13 $con = new PDO("mysql:host=localhost;dbname=school", "root", "sesame"); $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Prepared statement query. $query = "SELECT student.first, student.last, subject ". "FROM student, class, student_class ". "WHERE teacher_id = :teacher_id ". "AND code = class_code ". "AND student.id = student_id ". "ORDER BY subject, student.last"; $ps = $con->prepare($query); $ps->bindParam(':teacher_id', $teacherId); $ps->execute(); createTable($ps); school/students.php

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak Dynamically Created Table, cont’d 14 function createTable(PDOStatement $ps) { print " \n"; createHeaderRow($ps); $ps->execute(); $ps->setFetchMode(PDO::FETCH_CLASS, "StudentSubject"); // Construct the data rows. while ($ss = $ps->fetch()) { print " \n"; createDataRow($ss); print " \n"; } } school/students.php

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak Dynamically Created Table, cont’d 15 function createHeaderRow(PDOStatement $ps) { $row = $ps->fetch(PDO::FETCH_ASSOC); print " \n"; foreach ($row as $field => $value) { print " $field \n"; } print " \n"; } function createDataRow(StudentSubject $ss) { print " \n"; print " ". $ss->getFirst(). " \n"; print " ". $ss->getLast(). " \n"; print " ". $ss->getSubject(). " \n"; print " \n"; } school/students.php

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak load() Instead of $.get()  Instead of:  Shorter: 16 function init() { $("#teachermenu").selectmenu(); $("#teachermenu").on("selectmenuchange", showStudents); $.get("teachers.php", null, loadMenu); } function loadMenu(data, status) { $("#teachermenu").html(data); } function init() { $("#teachermenu").selectmenu().load("teachers.php"); $("#teachermenu").on("selectmenuchange", showStudents); } school/studentsof5.js school/studentsof4.js

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak load() Instead of $.post()  Instead of:  Shorter: 17 function showStudents(event, ui) { teacherId = $("#teachermenu").val(); $.post("students.php", {"id": teacherId}, loadTable); } function loadTable(data, status) { $("#output").html(data); } function showStudents(event, ui) { teacherId = $("#teachermenu").val(); $("#output").load("students.php", {"id": teacherId}); } school/studentsof4.js school/studentsof5.js

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak Cookies  Normally, each connection you make to the web server via a URL is a separate transaction.  The web server has no memory of your previous transactions.  One way for the web server to remember information from one transaction to another is by using cookies. 18

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak Cookies, cont’d  A cookie is a small packet of data created by the web application. A cookie can hold at most about 4 KB of data.  The web server sends the cookie to your web browser. The browser stores the cookie in its cookie folder.  The next time you connect to the web app, the browser sends the cookie data along with any form data. The web application thereby “recalls” information from the previous transaction. 19

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak Cookies, cont’d 20 PHP and MySQL for Dynamic Web Sites, 4 th ed. by Larry Ullman Peachpit Press, 2012 ISBN  Each newly created cookie contains a unique session id to distinguish it from other cookies.

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak Cookies, cont’d 21 <?php $counter = 0; if (isset($_COOKIE['counter'])) { $counter = $_COOKIE['counter'] + 1; } setCookie('counter', $counter); ?> Cookie Counter <?php echo " Cookie Counter: $counter \n"; ?> Send a cookie. Is there a cookie? Send cookies before sending any text to the web browser. session/cookie.php

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak Deleting Cookies  To delete a cookie, call setCookie() with only the name parameter but no value. Example: 22 setCookie('counter');

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak Problems with Cookies  Small amount of data Only at most 4 KB.  Insecure The cookie data is kept by the web browser.  Sometimes disallowed Some browsers may have cookies turned off. 23

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak Sessions  Similar functionality to cookies.  More data can be stored.  More secure: Data is stored on the web server.  Also assigns a unique session id to each user. Sessions use cookies. But sessions can also work without cookies. 24

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak Sessions, cont’d 25 <?php session_start(); ?> Session Counter <?php $counter = 0; if (isset($_SESSION['counter'])) { $counter = $_SESSION['counter'] + 1; } $_SESSION['counter'] = $counter; echo " Session Counter: $counter "; ?> Start the session before sending any text to the web browser. sessaion/session.php

Computer Science Dept. Spring 2015: April 14 CS 174: Web Programming © R. Mak Deleting Sessions  Delete a session variable:  Delete all session variables:  Delete the session: 26 unset($_SESSION['counter']); $_SESSION = array(); session_start(); set_cookie('PHPSESSID');