CSC 405: Web Application Engineering II Web programmering using PHP What have we learnt? What have we learnt? Cookies Cookies Sessions Sessions User Tracking.

Slides:



Advertisements
Similar presentations
LIS651 lecture 3 taming PHP Thomas Krichel
Advertisements

PHP I.
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.
6/10/2015Cookies1 What are Cookies? 6/10/2015Cookies2 How did they do that?
How Clients and Servers Work Together. Objectives Learn about the interaction of clients and servers Explore the features and functions of Web servers.
Intermediate PHP & MySQL
CSE 154 LECTURE 13: SESSIONS. Expiration / persistent cookies setcookie("name", "value", expiration); PHP $expireTime = time() + 60*60*24*7; # 1 week.
Chapter 10 Maintaining State Information Using Cookies.
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.
Cookies Set a cookie – setcookie() Extract data from a cookie - $_COOKIE Augment user authentication script with a cookie.
INTERNET APPLICATION DEVELOPMENT For More visit:
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.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
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.
HOW WEB SERVER WORKS? By- PUSHPENDU MONDAL RAJAT CHAUHAN RAHUL YADAV RANJIT MEENA RAHUL TYAGI.
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.
School of Computing and Information Systems CS 371 Web Application Programming PHP – Forms, Cookies, Sessions and Database.
Week seven CIT 354 Internet II. 2 Objectives Database_Driven User Authentication Using Cookies Session Basics Summary Homework and Project 2.
CSC 405: Web Application And Engineering II9.1 Web programming using PHP What have we learnt? What have we learnt? Sending s using PHP Sending s.
PHP1-1 PHP Lecture 2 Xingquan (Hill) Zhu
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
Mark Dixon 1 03 – Passing Data between pages: Forms, Sessions, & Query Strings.
Internet Information Systems Writing to Databases and Amending Data.
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.
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
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
PHP Workshop ‹#› Maintaining State in PHP Part II - Sessions.
More on Variables Some related techniques. Header() function void header ( string $string [, bool $replace = true [, int $http_response_code ]] ) header()
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.
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.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
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.
Controlling Web Site Access Using Logins CS 320. Basic Approach HTML form a php page that collects the username and password  Sends them to second PHP.
ASP. What is ASP? ASP stands for Active Server Pages ASP is a Microsoft Technology ASP is a program that runs inside IIS IIS stands for Internet Information.
ECMM6018 Enterprise Networking for Electronic Commerce Tutorial 7
CSC 405: Web Application Engineering II9.1 Web programming using PHP What have we learnt? What have we learnt? Sending s using PHP Sending s.
How to maintain state in a stateless web Shirley Cohen
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)
CSC 405: Web Application Engineering II10.1 Web programming using PHP What have we learnt? What have we learnt? Dates in PHP Dates in PHP – Various date.
 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.
CSC 405: Web Application Engineering II8.1 Web programming using PHP What have we learnt? What have we learnt? Underlying technologies of database supported.
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.
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.
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 ©
Web Programming Language
Cookies BIS1523 – Lecture 23.
CSE 154 Lecture 21: Sessions.
Web Programming Language
Web Programming Language
Presentation transcript:

CSC 405: Web Application Engineering II Web programmering using PHP What have we learnt? What have we learnt? Cookies Cookies Sessions Sessions User Tracking User Tracking Walk-through of the exam E2000 (Pizza ordering system) Walk-through of the exam E2000 (Pizza ordering system) Exercise introduction Exercise introduction 11.1

CSC 405: Web Application Engineering II Web programmering using PHP OVERVIEW: A PHP file: <html> Hello World Hello World Hello "; Hello "; echo " WORLD "; echo " WORLD ";?> </html> 11.2

CSC 405: Web Application Engineering II Until now: Variables, numbers, strings and arrays Variables, numbers, strings and arrays Computations Computations if-constructs and loops if-constructs and loops Functions and code reuse Functions and code reuse Entering user data using forms Entering user data using forms Validating user data using regular expressions Validating user data using regular expressions Database access (MySQL) Database access (MySQL) Sending s using PHP, dates Sending s using PHP, dates 11.3

CSC 405: Web Application Engineering II What are cookies? A cookie is a text string that a web server sends to a browser, which the browser will return unchanged next A cookie is a text string that a web server sends to a browser, which the browser will return unchanged next time the browser visits the same site. Cf. also Cookies are handy for handling a simple concept of state—session tracking: Cookies are handy for handling a simple concept of state—session tracking: – User customization – Focusing banner ads—what was the user interested in last time? – Admission control—login mechanism In Netscape/Mozilla on Linux you can use the cookie manager to see which cookies are installed, or simplyread the file.mozilla/...../cookies.txt: In Netscape/Mozilla on Linux you can use the cookie manager to see which cookies are installed, or simplyread the file.mozilla/...../cookies.txt: # Netscape HTTP Cookie File # # This is a generated file! Do not edit. linuxlab.dk FALSE / FALSE ad_browser_id 13 linuxlab.dk FALSE / FALSE last_visit linuxlab.dk FALSE / FALSE second_to_last_visit linuxlab.dk FALSE / FALSE ad_user_login 60,4C6F674E

CSC 405: Web Application Engineering II What are cookies? It uses the following file format: 11.5

CSC 405: Web Application Engineering II Problems using Cookies Normal browsers only support approximately 20 cookies per site Normal browsers only support a total of approximately 300 cookies Normal browsers only support a total of approximately 300 cookies A cookie can take up at most 4 kilobytes of space A cookie can take up at most 4 kilobytes of space Cookies implement a different form of state than state implemented using form variables Cookies implement a different form of state than state implemented using form variables Security problems using cookies Security problems using cookies – “triangle attack” Cookies can infringe on people’s privacy Cookies can infringe on people’s privacy 11.6

CSC 405: Web Application Engineering II Security problems using cookies—a “triangle attack” If a site uses cookies for identifying a user, the user can experience a “triangle attack”: An enemy publishes a web site that redirects users An enemy publishes a web site that redirects users to Amazon’s order pages (bypassing all forms) The enemy then sends an to an innocent The enemy then sends an to an innocent user, who the enemy knows does some shopping at Amazon’s web site. The enemy requests the user to click on a link When the user clicks on the link a book is ordered When the user clicks on the link a book is ordered without the user being able to avoid it—Amazon receives a cookie and accepts the order! Solution: Add a “Confirm Page” to the web site Add a “Confirm Page” to the web site 11.7

CSC 405: Web Application Engineering II Cookies can infringe on people’s privacy Reasons that some users choose to disable cookies in their browser: Search engines display ads related to what you searched for last time Search engines display ads related to what you searched for last time It is problematic if your boss is sniffing in your cookie file It is problematic if your boss is sniffing in your cookie file State problems Differences between state implemented using cookies and state implemented using form variables Differences between state implemented using cookies and state implemented using form variables Problems using cookies when several browsers are used concurrently Problems using cookies when several browsers are used concurrently What about the “Back”-button? What about the “Back”-button? 11.8

CSC 405: Web Application Engineering II Setting cookies in PHP Setting a cookie in the user’s browser is achieved by calling the PHP function setcookie(): Setting a cookie in the user’s browser is achieved by calling the PHP function setcookie(): – setcookie(name, value, expire, path, domain, secure) An abbreviated form is allowed: An abbreviated form is allowed: – setcookie(name, value) – Example: setcookie("a","5") Cookies must be set before any HTML is sent (e.g., using echo) to the browser Cookies must be set before any HTML is sent (e.g., using echo) to the browser Reading cookies in PHP A cookie sent by the browser in an HTTP request is read by looking in a cookie array: A cookie sent by the browser in an HTTP request is read by looking in a cookie array: – $HTTP_COOKIE_VARS["mycookie"] – $_COOKIE["mycookie"] Deleting cookies Cookies are deleted by setting its timeout value to a time in the past! Cookies are deleted by setting its timeout value to a time in the past! 11.9

CSC 405: Web Application Engineering II Example: CookieCount—cookiecount.php <?$count = $_COOKIE["count"]; if ( $count == "" ) { $count = 0; } setcookie("count", $count + 1); echo " CookieCount echo " CookieCount CookieCount: $count CookieCount: $count Up Up </body></html>";?>Note: No form variables are sent to the script in the Up link! No form variables are sent to the script in the Up link! How does the script work when several browsers are running on the same client? How does the script work when several browsers are running on the same client? 11.10

CSC 405: Web Application Engineering II PHP sessions When sessions are used, the state is stored in the web server; only a session ID is stored as a cookie on the client. When sessions are used, the state is stored in the web server; only a session ID is stored as a cookie on the client. PHP has good support for sessions: PHP has good support for sessions: – The function session_start() checks whether a session ID is already present as a cookie in the client browser. If not, a new session ID is generated and stored in the browser as a cookie. – When a session has been started (using start_session()) data can be stored and retrieved from a session array: $_SESSION["myvar"] $_SESSION["myvar"] $HTTP_SESSION_VARS["myvar"] $HTTP_SESSION_VARS["myvar"] The function session_destroy() can be used to clean up after a session The function session_destroy() can be used to clean up after a session – memory is released and the session ID cookie is deleted from the client. Potential problems using sessions Cookie problems, as well as: Cookie problems, as well as: It is unclear when the web server can assume that memory used by session data can be released and reused It is unclear when the web server can assume that memory used by session data can be released and reused The only solution: timeouts! The only solution: timeouts! 11.11

CSC 405: Web Application Engineering II Example: Session Basket—basket.php An implementation of a simple shopping basket using sessions. <? session_start(); if ( $submit == "Empty Basket" ) { // destroy basket session_destroy(); // destroy session header("Location: basket.php"); exit; // reload and exit} $basket = $_SESSION["basket"]; if ( $new != "" ) $basket[count($basket)] = $new; // maybe add new stuff $_SESSION["basket"] = $basket; echo " Session Basket echo " Session Basket Session Basket "; Session Basket "; for ( $i = 0 ; $i < count($basket) ; $i ++ ) { // loop through array echo " $basket[$i] "; } echo " echo " </ul></form></body></html>";?> 11.12

CSC 405: Web Application Engineering II User Tracking: Where do the users arrive from and how many hits do I get on my Web site? Look in the Web servers access logs: $ wc -l /web/login/log/access.log 1143 access.log $ less /web/login/log/access.log [12/Apr/2000:01:47: ] \ "GET /faerdig.html HTTP/1.0" "" "AltaVista V2.3A [12/Apr/2000:03:37: ] \ "GET /robots.txt HTTP/1.0" "" "AltaVista V2.3A Different Web servers (Apache, AOLserver,...) use the same standard format for access logs

CSC 405: Web Application Engineering II Statistics from access logs The following information can be extracted from the access log of a Web server: The type of browser used by a specific user The type of browser used by a specific user The number of users that request nonexistent files, and where they have found these URLs The number of users that request nonexistent files, and where they have found these URLs The number of users that request a specific file The number of users that request a specific file The time users spend on average on a specific file before going on to another file The time users spend on average on a specific file before going on to another file The number of users that click on specific banner ads The number of users that click on specific banner ads Does a user return regularly? Does a user return regularly? 11.14

CSC 405: Web Application Engineering II Nonexistent files Example: The user enters an incorrect URL directly in the location bar of the browser Search for “404” (File Not Found) in the access log: [17/Feb/2000:15:51: ] "GET /temperatur.html HTTP/1.1" "" "Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)" The file name temperatur.html should have been temperature.html The file name temperatur.html should have been temperature.html The user uses Internet Explorer (MSIE 5.0) on a Windows’98 machine The user uses Internet Explorer (MSIE 5.0) on a Windows’98 machine Using the UNIX host command, we find that is the IP address of stud250.itu.dk: Using the UNIX host command, we find that is the IP address of stud250.itu.dk: # host Name: stud250.itu.dk Address: The user has probably entered the URL directly into the browser—there is no referring page. The user has probably entered the URL directly into the browser—there is no referring page

CSC 405: Web Application Engineering II Nonexistent file due to incorrect links We look once again for “404” (File Not Found) in the access log: [20/Mar/2001:02:10: ] "GET /F2001/lec8/list2v.tcl HTTP/1.0" " "Mozilla/4.73 [en] (X11; U; Linux i686)" The file /list2v.tcl should have been /listv2.tcl The file /list2v.tcl should have been /listv2.tcl The user uses Netscape v on a Linux machine (kernel v ). The user uses Netscape v on a Linux machine (kernel v ). Using the UNIX command host, we find that is the IP address of Using the UNIX command host, we find that is the IP address of adsl.suoe.worldonline.dk. The error is caused by an incorrect link in the script slide_extractor.tcl. The error is caused by an incorrect link in the script slide_extractor.tcl. – This shows that we can see which page the user comes from – Conclusion: we can see which pages contain incorrect links 11.16

CSC 405: Web Application Engineering II The number of users that have requested a specific file Determine the number of lines in the access log that contain, e.g., GET /SlideExtractor/slide_extractor.tcl This can be done using the UNIX grep command: # grep ’GET /SlideExtractor/slide_extractor.tcl’ access.log* | wc -l 1609 Here is one of the lines (with added line breaks): [05/Feb/2001:11:05: ] "GET /SlideExtractor/slide_extractor.tcl?presentation_id=48903 HTTP/1.0" " "Mozilla/4.73 [en] (X11; U; Linux i686)" Using the UNIX host command, we find that is the IP address of vip160.itu.dk—it is a local user Using the UNIX host command, we find that is the IP address of vip160.itu.dk—it is a local user The user was sent from the page February 5, 2001 at 05:55 The user was sent from the page February 5, 2001 at 05:55 The hit time, combined with the remaining lines, can be used to determine how long time a person spends on a specific page The hit time, combined with the remaining lines, can be used to determine how long time a person spends on a specific page 11.17

CSC 405: Web Application Engineering II Solution to Test Exam in Web Publishing with Databases, Fall 2000 Solution to Problem 1 (15 percent) - HTML Problem 1.1 <html><head><title>About</title></head> Video Projector Reservation System Video Projector Reservation System </tr> About The Video Projector Reservation System allows About The Video Projector Reservation System allows registered users to make reservations of video projectors at the IT University of Copenhagen. the IT University of Copenhagen. </body></html> 11.18

CSC 405: Web Application Engineering II Problem 1.2 Projector 1 Projector 1 Projector 2 Projector 2 Projector 3 Projector 3</select> Solution to Problem 2 (20 percent) - PHP Problem 2.1 <ul> Projector 4 Projector 4 Projector 1 Projector 1 </ul> 11.19

CSC 405: Web Application Engineering II Problem 2.2 function select_box ($n, $l) { $res = " \n"; for ( $i = 0 ; $i < count($l) ; $i ++ ) { $id = $l[$i][0]; $text = $l[$i][1]; $res = $res. " $text \n"; } $res = $res. " "; return $res; } 11.20

CSC 405: Web Application Engineering II Problem 2.3 function vp_return_page ( $title, $body ) { echo " echo " $title $title Video Projector Reservation System Video Projector Reservation System </tr> $body </body></html>"; exit;} 11.21

CSC 405: Web Application Engineering II Solution to Problem 3 (15 percent) - Regular expressions Problem 3.1 0|([1-9][0-9]*) Problem 3.2 [0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9] Problem 3.3 function vp_check_date ( $d ) { if ( !ereg (’^[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]$’, $d ) ) if ( !ereg (’^[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]$’, $d ) ){ vp_return_page ("FEJL: Forkert dato format", "Gå tilbage og vp_return_page ("FEJL: Forkert dato format", "Gå tilbage og indtast datoen i formatet YYYY-MM-DD."); exit;}return;} 11.22

CSC 405: Web Application Engineering II Solution to Problem 4 (20 percent) - SQL Problem 4.1 NAME John Paulin Niels Hallenberg Problem 4.2 select count(*) from vp_projector; 11.23

CSC 405: Web Application Engineering II Solution to Problem 4 (20 percent) - SQL Problem 4.3 create table vp_reservation ( projector_id integer not null, person_id integer not null, res_date date not null, primary key(projector_id, res_date) ); Problem 4.4 insert into vp_reservation (projector_id, person_id, res_date) values (2, 1, ’ ’); Insert into vp_reservation (projector_id, person_id, res_date) values (1, 2, ’ ’); 11.24

CSC 405: Web Application Engineering II Solution to Problem 4 (20 percent) - SQL Problem 4.5 select vp_projector.id as proj_id, vp_projector.name as proj_name, vp_person.id as pers_id, vp_person.name as pers_name, from vp_person, vp_projector, vp_reservation where vp_person.id = vp_reservation.person_id and vp_projector.id = vp_reservation.projector_id and res_date = ’ ’; 11.25

CSC 405: Web Application Engineering II Solution to Problem 4 (20 percent) - SQL Problem 4.6 select id as proj_id, name as proj_name from vp_projector left join vp_reservation on res_date = ’ ’ and id = projector_id and id = projector_id group by id, name having count(res_date) = 0; 11.26

CSC 405: Web Application Engineering II Solution to Problem 5 (30 percent) - Web- service Problem 5.1 PHP-code for insertion after the definition of the variable $prevday: $nextweek = date("Y-m-d", mktime(0,0,0,$month,$day+7,$year)); $prevweek = date("Y-m-d", mktime(0,0,0,$month,$day-7,$year)); Modifications to the call to vp_return_page: vp_return_page ("Projector Reservations", " Projector Reservations for $date " Projector Reservations for $date [ Prev Day | Prev Week | Prev Week | Next Week | Next Week | Next Day ] Next Day ]<ul>$reservations</ul>"); 11.27

CSC 405: Web Application Engineering II Solution to Problem 5 (30 percent) - Web- service Problem 5.2 Program point ## A ##: vp_check_date ($date); Program point ## B ##: vp_return_page ("Add Projector Reservation", " Add Projector Reservation for $date Add Projector Reservation for $date <b> </b><br> <b>Password:</b><br> $sel_box </form>"); 11.28

CSC 405: Web Application Engineering II Solution to Problem 5 (30 percent) - Web- service Problem 5.3 <? include ("vp.php"); vp_check_date ($date); vp_check_ ($ ); vp_check_id ($proj_id); vp_check_passwd ($password); mydb_connect(); $rows = mysql_query ("select id from vp_person where = ’$ ’ and password= ’$password’"); if ( $row = mysql_fetch_row ($rows) ) { $res = mysql_query ("insert into vp_reservation (person_id, projector_id, res_date) values (’$id’, ’$proj_id’, ’$date’)"); values (’$id’, ’$proj_id’, ’$date’)"); if ( $res == 0 ) // ERROR vp_return_page ("Could not add reservation", " Could not add reservation " Could not add reservation Perhaps the projector is already booked"); Perhaps the projector is already booked"); else header ("Location: projector.php?date=$date");} else vp_return_page ("Could not add reservation", " Could not add reservation " Could not add reservation Wrong password or address"); ?> 11.29

CSC 405: Web Application Engineering II Exercise set 11 Exercise set 11 is an “open exercise” If you already have an idea for a small web service, you now have a chance of realising it! See the exercise set at: