CSC 405: Web Application Engineering II5.1 Web programming using PHP What have we learnt? What have we learnt? Motivation for validating user input (form.

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

PHP I.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
CSCI 6962: Server-side Design and Programming Input Validation and Error Handling.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to C Programming
Introduction to scripting
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6)
Regular Expressions. String Matching The problem of finding a string that “looks kind of like …” is common  e.g. finding useful delimiters in a file,
Last Updated March 2006 Slide 1 Regular Expressions.
PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Web forms in PHP Forms Recap  Way of allowing user interaction  Allows users to input data that can then be processed by a program / stored in a back-end.
PHP : Hypertext Preprocessor
1 Chapter 6 – Creating Web Forms and Validating User Input spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information.
CSC 405: Web Application And Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming.
An Introduction to Textual Programming
Server-Side Validation Jayden Bryant. What is Server-Side Validation?  Validation of form input done on the server, not the web browser program //Validate.
Tutorial 14 Working with Forms and Regular Expressions.
INTERNET APPLICATION DEVELOPMENT For More visit:
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
Robinson_CIS_285_2005 HTML FORMS CIS 285 Winter_2005 Instructor: Mary Robinson.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
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.
CIS 451: Regular Expressions Dr. Ralph D. Westfall January, 2009.
Open Source Software Unit – 3 Presented By Mr. R.Aravindhan.
Website Development with PHP and MySQL Saving Data.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
1 PHP Intro PHP Strings After this lecture, you should be able to: Manipulate and Output PHP Strings: Manipulate and Output PHP Strings: Single- or Double-quoted.
12. Regular Expressions. 2 Motto: I don't play accurately-any one can play accurately- but I play with wonderful expression. As far as the piano is concerned,
Introduction to Java Java Translation Program Structure
Storing and Retrieving Data
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
PHP Programming.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Homework #4: Operator Overloading and Strings By J. H. Wang Apr. 17, 2009.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
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.
CSC 405: Web Application Engineering II8.1 Web programming using PHP What have we learnt? What have we learnt? Underlying technologies of database supported.
CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
-Joseph Beberman *Some slides are inspired by a PowerPoint presentation used by professor Seikyung Jung, which was derived from Charlie Wiseman.
PZ02CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ02CX - Perl Programming Language Design and Implementation.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 17.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting.
CS 330 Class 7 Comments on Exam Programming plan for today:
Perl Programming Language Design and Implementation (4th Edition)
Lecture 9 Shell Programming – Command substitution
Chapter 19 PHP Part II Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
Introduction to Scripting
Data Structures Mohammed Thajeel To the second year students
WEB PROGRAMMING JavaScript.
PHP.
Fundamentals of Python: First Programs
Tutorial 6 PHP & MySQL Li Xu
Presentation transcript:

CSC 405: Web Application Engineering II5.1 Web programming using PHP What have we learnt? What have we learnt? Motivation for validating user input (form variables) Motivation for validating user input (form variables) Regular expressions (patterns) Regular expressions (patterns) The PHP function ereg The PHP function ereg Examples of validating form variables Examples of validating form variables Library of functions for validating form variables Library of functions for validating form variables Other uses of regular expressions Other uses of regular expressions Using ereg to extract data from web pages Using ereg to extract data from web pages Introduction to exercises Introduction to exercises

CSC 405: Web Application Engineering II5.2 What have we learnt? OVERVIEW: A PHP file: Hello World Hello World Hello "; Hello "; echo " WORLD "; echo " WORLD "; ?> ?> </html>

CSC 405: Web Application Engineering II5.3 What have we learnt? 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

CSC 405: Web Application Engineering II5.4 Motivation for validating form variables Without validating user input our web programs are not robust! Example: Currency exchange service—exchange.html Exchange Bank Exchange Bank Enter value in kroner: Enter value in kroner: </html>

CSC 405: Web Application Engineering II5.5 Motivation for validating form variables The file exchange.php: Exchange Bank Exchange Bank <? $rate = 8.43; $fee = 20.0; <? $rate = 8.43; $fee = 20.0; $dollars = ($kroner - $fee) / $rate; $dollars = ($kroner - $fee) / $rate; $dollars = number_format($dollars, 2, ",", "."); $dollars = number_format($dollars, 2, ",", "."); echo "For DKr. $kroner you receive \$$dollars"; ?> echo "For DKr. $kroner you receive \$$dollars"; ?> New Computation New Computation </html> Problem: We do not check whether the form variable $kroner contains a number

CSC 405: Web Application Engineering II5.6 What can go wrong if we do not validate form variables? Various kinds of errors can occur in the continued computations based on wrong user input: Cosmetic errors: the program returns non-wellformed HTML code to the browser Cosmetic errors: the program returns non-wellformed HTML code to the browser Semantic errors: the program returns well-formed HTML code that seems correct, but in fact isn’t! Semantic errors: the program returns well-formed HTML code that seems correct, but in fact isn’t! Very grave errors: data is deleted from the database because missing validation can result in hacker access Very grave errors: data is deleted from the database because missing validation can result in hacker access By validating user input we can avoid most kinds of errors

CSC 405: Web Application Engineering II5.7 Many kinds of form variables We have seen the need for determining whether a form variable contains a number There are many other kinds of data that we can ask users to enter: Decimal numbers Decimal numbers addresses addresses Numbers that must lie in a specific interval (e.g., postal codes) Numbers that must lie in a specific interval (e.g., postal codes) URL addresses URL addresses Colours (red, green, blue) Colours (red, green, blue) Dates Dates We need a language for describing strings with a common pattern The language we need is called regular expressions, also often called patterns.

CSC 405: Web Application Engineering II5.8 Syntax of regular expressions, part 1 A pattern m can have various forms, including:. is matched by any character C is matched by the character c; the character. is written \. m1m2 a sequential match of m1 and m2. Ex: the pattern ‘a.c’ is matched by the string ‘abc’ m* is matched by 0 or more sequential occurrencies of character sequences that match the pattern m. Ex: the strings ‘abbbbbba’ and ‘aa’ matches the pattern ‘ab*a’ (m) is matched by strings that match m. Ex: the string ‘cababcc’ matches the pattern ‘c(ab)*cc’ m+ is matched by one or more sequential occurrencies of character sequences that match the pattern m. Ex: the pattern ‘ca+b’ is matched by the string ‘caaab’ but not by the string ‘cb’ pattern m. Ex: the pattern ‘ca+b’ is matched by the string ‘caaab’ but not by the string ‘cb’ m? is matched by 0 or 1 occurrencies of character sequences that match the pattern m. Ex: the pattern ‘abc?’ is matched by both the strings ‘abc’ and ‘ab’. pattern ‘abc?’ is matched by both the strings ‘abc’ and ‘ab’.

CSC 405: Web Application Engineering II5.9 Syntax of regular expressions, part 2 A pattern m can have various forms, including: m1 | m2 is matched by character sequences that match m1 or m2. Ex: the pattern ‘(hi|hello)’ is matched by the character sequence ‘hi’ and the character sequence ’hello’ matched by the character sequence ‘hi’ and the character sequence ’hello’ [...] is matched by characters in the specified class or range. Ex: the pattern [abc1-4]* is matched by character sequences consisting of the characters a, b, c, 1, 2, 3, 4 [ˆ...] is matched by all characters not in this class or range. Ex: the pattern [ˆabc1-4]* is matched by character sequences consisting of any characters except a, b, c, 1, 2, 3, 4. So the circumflex ˆ means “anything but” the characters that follow it

CSC 405: Web Application Engineering II5.10 Examples of patterns [A-Za-zÆØÅæøå] : is matched by any Danish letter [A-Za-zÆØÅæøå] : is matched by any Danish letter [0-9][0-9] : is matched by two digit numbers (including a prepended zero) [0-9][0-9] : is matched by two digit numbers (including a prepended zero) (hi|hello) : is matched by the two strings hi and hello (hi|hello) : is matched by the two strings hi and hello ((a|b)a)* : is matched by aa, ba, aaaa, baaa,... ((a|b)a)* : is matched by aa, ba, aaaa, baaa,... (0|1)+ : is matched by binary numbers, i.e., 0, 1, 01, 11, ,... (0|1)+ : is matched by binary numbers, i.e., 0, 1, 01, 11, ,..... : is matched by any two arbitrary characters.. : is matched by any two arbitrary characters (red|green|blue) : is matched by the colours red, green and blue (red|green|blue) : is matched by the colours red, green and blue ([1-9][0-9]*)/([1-9][0-9]*) : is matched by integer quotients, e.g., 1/8, 32/5645, 45/6,... ([1-9][0-9]*)/([1-9][0-9]*) : is matched by integer quotients, e.g., 1/8, 32/5645, 45/6,... Does the string 012/54 match the last pattern? Does the string 2/0 match the last pattern?

CSC 405: Web Application Engineering II5.11 The PHP function ereg The built-in function ereg can be used to decide whether a pattern m is matched by a substring of a string s. The built-in function ereg can be used to decide whether a pattern m is matched by a substring of a string s. A call to ereg(m,s) returns 1 (TRUE) if pattern m is matched by a substring of s. Otherwise it returns 0 (FALSE). A call to ereg(m,s) returns 1 (TRUE) if pattern m is matched by a substring of s. Otherwise it returns 0 (FALSE). If the pattern m starts with ‘^’ the matching substring must be a prefix of s, i.e., start at the first character of s. If the pattern m starts with ‘^’ the matching substring must be a prefix of s, i.e., start at the first character of s. Correspondingly, if the pattern m is terminated with a ‘$’ the matched substring must be a suffix of s. Correspondingly, if the pattern m is terminated with a ‘$’ the matched substring must be a suffix of s. Function call Result Function call Result ereg(’[0-9]+’, "aa38AA") 1 ereg(’^[0-9]+’, "aa99") 0 ereg(’^[0-9]+’, "aa99") 0 ereg(’^[0-9]+’, "77AA") 1 ereg(’^[0-9]+’, "77AA") 1 ereg(’^[0-9]+$’, "aa87AA") 0 ereg(’^[0-9]+$’, "aa87AA") 0Note: We will usually use ^ and $ as the first and last characters of the pattern when validating user input We will usually use ^ and $ as the first and last characters of the pattern when validating user input We will delimit pattern strings in PHP by ’...’ to avoid the special meaning of $ in strings of the form "..." We will delimit pattern strings in PHP by ’...’ to avoid the special meaning of $ in strings of the form "..."

CSC 405: Web Application Engineering II5.12 Example usages of the PHP function ereg Examples: Function call Result Function call Result ereg(’[a-zA-Z]+’, "Allan Hansen") __ ereg(’[a-zA-Z]+’, "Allan Hansen") __ ereg(’^[a-zA-Z]+$’, "Ulla Jensen") __ ereg(’^[a-zA-Z]+$’, "Ulla Jensen") __ ereg(’^[a-zA-Z]+$’, "") __ ereg(’^[a-zA-Z]+$’, "") __ ereg(’^[0-9][0-9]-[0-9][0-9]-[0-9]+$’, " ") __ ereg(’^[0-9][0-9]-[0-9][0-9]-[0-9]+$’, " ") __ ereg(’^[0-1][0-9]-[0-3][0-9]-[0-9]+$’, " ") __ ereg(’^[0-1][0-9]-[0-3][0-9]-[0-9]+$’, " ") __ ereg(’^(red|green|blue)$’, "red") __ ereg(’^(red|green|blue)$’, "red") __ ereg(’^(red|green|blue)$’, "redblue") __ ereg(’^(red|green|blue)$’, "redblue") __ ereg(’^(a|bb)*b$’, "bbab") __ ereg(’^(a|bb)*b$’, "bbab") __ ereg(’^(a|bb)+b*$’, "b") __ ereg(’^(a|bb)+b*$’, "b") __

CSC 405: Web Application Engineering II5.13 Example: The web program pattern.php: We can build a pattern matcher to experiment with patterns—pattern.php: Pattern matcher Pattern matcher <?php if ( $r != "" ) { <?php if ( $r != "" ) { if ( ereg("^($r)\$", $s) ) { echo " Success: The string ’$s’ matches pattern ’$r’ "; if ( ereg("^($r)\$", $s) ) { echo " Success: The string ’$s’ matches pattern ’$r’ "; } else { echo " Failure: The string ’$s’ does NOT match pattern ’$r’ "; } else { echo " Failure: The string ’$s’ does NOT match pattern ’$r’ "; } } echo " } echo " Pattern: Pattern: String: String: </form>";?></body></html> Note: We use the function ereg to determine whether a string matches a pattern Note: We use the function ereg to determine whether a string matches a pattern

CSC 405: Web Application Engineering II5.14 Improving the currency exchange service by validating form variables We can improve our currency exchange service by validating user input—exchange2.php: Currency Exchange Service Currency Exchange Service Currency Exchange Service (version 2) Currency Exchange Service (version 2) <? if ( ereg(’^(0|[1-9][0-9]*)$’, $kroner) ) { $rate = 8.43; $commission = 20.0; <? if ( ereg(’^(0|[1-9][0-9]*)$’, $kroner) ) { $rate = 8.43; $commission = 20.0; if ( $kroner > $commission ) { $dollars = ($kroner - $commission) / $rate; if ( $kroner > $commission ) { $dollars = ($kroner - $commission) / $rate; $dollars = number_format($dollars, 2, ",", "."); $dollars = number_format($dollars, 2, ",", "."); echo "In exchange for kr. $kroner you will receive \$$dollars"; echo "In exchange for kr. $kroner you will receive \$$dollars"; } else { echo "You cannot change an amount less than the commission!"; } else { echo "You cannot change an amount less than the commission!"; } } else { echo "Please go back and enter a number!"; } else { echo "Please go back and enter a number!"; } ?> } ?></body></html> Question: How does the program react to various kinds of user input?

CSC 405: Web Application Engineering II5.15 Function for checking addresses We can check whether an entered address has the expected form by using the pattern: The following function takes as argument a string and checks whether the string looks like an address: function chk_ ( $ ) { function chk_ ( $ ) { if ( Z!#$%&*+/=?^_‘{|}~.]+$’, $ ) == 0 ) { if ( Z!#$%&*+/=?^_‘{|}~.]+$’, $ ) == 0 ) { error("You must enter a valid address"); error("You must enter a valid address"); } }Note: The function does not guarantee that the address exists! The function does not guarantee that the address exists! We use a generic function error for displaying an error message: We use a generic function error for displaying an error message: function error ( $msg ) { echo " Error: $msg "; function error ( $msg ) { echo " Error: $msg "; exit; // Don’t continue the script! exit; // Don’t continue the script! } The exact definition of addresses can be found in RFC2822 at The exact definition of addresses can be found in RFC2822 at

CSC 405: Web Application Engineering II5.16 A Library of functions for validating form variables We can construct a file formvars.php containing a list of functions for validating form variables Then we can include the file formvars.php (using the include function) in scripts that read form variables In this way, form variable validating becomes easy! Example:—exchange3.php: Currency Exchange Service Currency Exchange Service Currency Exchange Service (version 3) Currency Exchange Service (version 3) <? $rate = 8.43; $commission = 20.0; <? $rate = 8.43; $commission = 20.0; if ( $kroner > $commission ) { $dollars = ($kroner - $commission) / $rate; $dollars = number_format($dollars, 2, ",", "."); $dollars = number_format($dollars, 2, ",", "."); echo "In exchange for kr. $kroner you will receive \$$dollars"; echo "In exchange for kr. $kroner you will receive \$$dollars"; } else { echo "You cannot change an amount less than the commission!"; } else { echo "You cannot change an amount less than the commission!";}?></body></html>

CSC 405: Web Application Engineering II5.17 Library of functions for validating form variables—continued The file formvars.php: Error: $msg "; Error: $msg "; exit; // Don’t continue the script! exit; // Don’t continue the script! } function chk_ ( $ ) { function chk_ ( $ ) { if ( Z!#$%&*+/=?^_‘{|}~.]+$’, $ ) == 0 ) { if ( Z!#$%&*+/=?^_‘{|}~.]+$’, $ ) == 0 ) { error("You must enter a valid address"); error("You must enter a valid address"); } } function chk_integer ( $number ) { function chk_integer ( $number ) { if ( ereg(’^(0|[1-9][0-9]*)$’, $number) == 0 ) { if ( ereg(’^(0|[1-9][0-9]*)$’, $number) == 0 ) { error("You must enter an integer number"); error("You must enter an integer number"); } }?>

CSC 405: Web Application Engineering II5.18 Other uses of regular expressions Regular expressions can also be used for other tasks than validating user input Examples: Finding patterns in a text Finding patterns in a text Search-and-replace in a text Search-and-replace in a text Fetching data from foreign web sites: In web technology, regular expressions can be used to find relevant information in texts downloaded automatically from the Internet, e.g.: The daily news from Reuters The daily news from Reuters The exchange rates The exchange rates The stock market rates The stock market rates The weather report The weather report The pollen count The pollen count

CSC 405: Web Application Engineering II5.19 Fetching data from foreign web sites using PHP The following function can be used to load a foreign web site into a string in PHP : function fetchurl ( $url ) { $fp = fopen($url, ’r’); function fetchurl ( $url ) { $fp = fopen($url, ’r’); if ( ! $fp ) return "No page"; if ( ! $fp ) return "No page"; for ($t = "" ; !feof($fp) ; ) for ($t = "" ; !feof($fp) ; ) $content.= fread($fp, 8192); $content.= fread($fp, 8192); fclose($fp); fclose($fp); return $content; return $content; } The ereg function can also be used to extract information from a web page contained in a string. If you give ereg an additional argument $result, the substring that matches the nth parenthesised part of the pattern is stored in $result[n]. When calling ereg($p,$txt,$result) the three arguments are: $p is a pattern with which to match the text to be extracted $p is a pattern with which to match the text to be extracted $txt is the text in which a substring that matches the pattern is sought $txt is the text in which a substring that matches the pattern is sought $result is a result array that will contain the matching text when the function ereg returns $result is a result array that will contain the matching text when the function ereg returns

CSC 405: Web Application Engineering II5.20 Example: World population—popclock.php World Population Watch World Population Watch World Population Watch World Population Watch <?php function fetchurl ( $url ) { <?php function fetchurl ( $url ) { $fp = fopen($url, ’r’); $fp = fopen($url, ’r’); if ( ! $fp ) return "No page"; if ( ! $fp ) return "No page"; for ($t = "" ; !feof($fp) ; ) for ($t = "" ; !feof($fp) ; ) $content.= fread($fp, 8192); $content.= fread($fp, 8192); fclose($fp); fclose($fp); return $content; return $content; } $content = fetchurl(" ereg("<h1>([0-9,]+)</h1>",$content,$result); echo "There are at present $result[1] people in the world."; ?> </html>

CSC 405: Web Application Engineering II5.21 Introduction to exercise 5 At the exercises you must: Exercise regular expressions. Exercise regular expressions. Construct an improved version of the body mass index web service. Construct an improved version of the body mass index web service. Construct a dollar exchange rate web service that extracts the rate from a foreign web site and offers conversion of kroner to dollars or vice versa. Construct a dollar exchange rate web service that extracts the rate from a foreign web site and offers conversion of kroner to dollars or vice versa. Come up with a useful automatic extraction of information from one or more sites on the Internet and implement a web service that utilises this information—weather report, stock market rates, pollen counts,... Come up with a useful automatic extraction of information from one or more sites on the Internet and implement a web service that utilises this information—weather report, stock market rates, pollen counts,...