PIC 40A. Lecture 19: PHP Form handling, session variables and regular expressions. Copyright 2011 Jukka Virtanen UCLA 1 05/25/12

Similar documents
Regular Expressions. Regular expressions are a powerful search-and-replace technique that is widely used in other environments (such as Unix and Perl)

Privacy and Security in Online Social Networks Department of Computer Science and Engineering Indian Institute of Technology, Madras

Form Processing in PHP

Introduction. Server-side Techniques. Introduction. 2 modes in the PHP processor:

BEGINNER PHP Table of Contents

PIC 40A. Review for the Final. Copyright 2011 Jukka Virtanen UCLA 1 06/05/15

UNIT-VI CREATING AND USING FORMS

Submitting forms (client-side)

CSc 337 Final Examination December 13, 2013

PHP 5 if...else...elseif Statements

MCIS/UA. String Literals. String Literals. Here Documents The <<< operator (also known as heredoc) can be used to construct multi-line strings.

University of Washington, CSE 154 Homework Assignment 7: To-Do List

WEB APPLICATION ENGINEERING II

CSE 154 LECTURE 11: REGULAR EXPRESSIONS

Lab 4: Basic PHP Tutorial, Part 2

Database Systems Fundamentals

This is CS50. Harvard College Fall Quiz 1 Answer Key

Server-Side Web Programming: Python (Part 1) Copyright 2017 by Robert M. Dondero, Ph.D. Princeton University

Lecture 7: Dates/Times & Sessions. CS 383 Web Development II Wednesday, February 14, 2018

PHP INTERVIEW QUESTION-ANSWERS

GET /index.php HTTP/1.1 Host: User- agent: Mozilla/4.0

B. V. Patel Institute of BMC & IT 2014

ITS331 IT Laboratory I: (Laboratory #11) Session Handling

Chapter 7:- PHP. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

How browsers talk to servers. What does this do?

PIC 40A. Lecture 10: JS: Wrapper objects, Input and Output, Control structures, random numbers. Copyright 2011 Jukka Virtanen UCLA 1 04/24/17

Common Gateway Interface CGI

USQ/CSC2406 Web Publishing

User authentication, passwords

What is PHP? [1] Figure 1 [1]

WEB APPLICATION ENGINEERING II

Perl Regular Expressions. Perl Patterns. Character Class Shortcuts. Examples of Perl Patterns

CPSC 481: CREATIVE INQUIRY TO WSBF

Phpmyadmin Error In Processing Request Error Code 200

University of Washington, CSE 190 M Homework Assignment 9: Remember the Cow (To-Do List)

HTML Forms. By Jaroslav Mohapl

Chapter 9. Managing State Information. Understanding State Information (continued) Understanding State Information 10/29/2011.

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

Lecture 9 Server Browser Interactions

Lecture 6: More Arrays & HTML Forms. CS 383 Web Development II Monday, February 12, 2018

VISIONTRACKER FREQUENTLY ASKED QUESTIONS FAQ

CS4604 Prakash Spring 2016! Project 3, HTML and PHP. By Sorour Amiri and Shamimul Hasan April 20 th, 2016

Understanding Regular Expressions, Special Characters, and Patterns

HTML forms and the dynamic web

PHP with data handling

Web Programming. Based on Notes by D. Hollinger Also Java Network Programming and Distributed Computing, Chs.. 9,10 Also Online Java Tutorial, Sun.

PHP Personal Home Page PHP: Hypertext Preprocessor (Lecture 35-37)

Hello everyone! Page 1. Your folder should look like this. To start with Run your XAMPP app and start your Apache and MySQL.

Princess Nourah bint Abdulrahman University. Computer Sciences Department

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Network Administration/System Administration (NTU CSIE, Spring 2018) Homework #1. Homework #1

JavaScript Functions, Objects and Array

Practice problems. 1 Draw the output for the following code. 2. Draw the output for the following code.

Forms, CGI. Objectives

zend. Number: Passing Score: 800 Time Limit: 120 min.

JQuery and Javascript

Web Focused Programming With PHP

exam. Number: Passing Score: 800 Time Limit: 120 min File Version: Zend Certified Engineer

Regular Expressions Explained

Here's an example of how the method works on the string "My text" with a start value of 3 and a length value of 2:

Attacks Against Websites. Tom Chothia Computer Security, Lecture 11

CSE 154, Autumn 2012 Final Exam, Thursday, December 13, 2012

CPET 499/ITC 250 Web Systems. Topics

Mail Merge for Gmail v2.0

Student, Perfect Final Exam May 25, 2006 ID: Exam No CS-081/Vickery Page 1 of 6

5/19/2015. Objectives. JavaScript, Sixth Edition. Saving State Information with Query Strings. Understanding State Information

PIC 40A. Lecture 4b: New elements in HTML5. Copyright 2011 Jukka Virtanen UCLA 1 04/09/14

Course Wiki. Today s Topics. Web Resources. Amazon EC2. Linux. Apache PHP. Workflow and Tools. Extensible Networking Platform 1

Problem Description Earned Max 1 HTML / CSS Tracing 20 2 CSS 20 3 PHP 20 4 JS / Ajax / JSON 20 5 SQL 20 X Extra Credit 1 TOTAL Total Points 100

Developing Online Databases and Serving Biological Research Data

Attacks Against Websites 3 The OWASP Top 10. Tom Chothia Computer Security, Lecture 14

Week 13 Thursday (with Page 5 corrections)

The HTTP Protocol HTTP

Manipulating Data in Strings and Arrays

[Frequently Asked Questions] Accommodation Booking Website

Composer Help. Web Request Common Block

9.1 Origins and Uses of Perl

COM1004 Web and Internet Technology

HTML 5 Form Processing

Table of contents. DMXzone Universal Form Validator ASP DMXzone.com

Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 11 Introduction to PHP

PHP State Maintenance (Cookies, Sessions, Hidden Inputs)

CITS1231 Web Technologies. PHP s, Cookies and Session Control

PHP Introduction. Some info on MySQL which we will cover in the next workshop...

WEBD 236 Web Information Systems Programming

Web Programming with PHP

More Scripting and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

Forms, CGI. Cristian Bogdan 2D2052 / 2D1335 F5 1

LING115 Lecture Note Session #7: Regular Expressions

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

<form>. input elements. </form>

GETTING STARTED GUIDE

CNIT 129S: Securing Web Applications. Ch 12: Attacking Users: Cross-Site Scripting (XSS) Part 2

Web Development and HTML. Shan-Hung Wu CS, NTHU

GroupWise 7 WebAccess Introduction

You are Unable to Log into MBEF

Handout 7, Lex (5/30/2001)

Server-side Web Development (I3302) Semester: 1 Academic Year: 2017/2018 Credits: 4 (50 hours) Dr Antoun Yaacoub

Lecture 12. PHP. cp476 PHP

Hyperlinks, Tables, Forms and Frameworks

Transcription:

PIC 40A Lecture 19: PHP Form handling, session variables and regular expressions 05/25/12 Copyright 2011 Jukka Virtanen UCLA 1

How does a browser communicate with a program on a server? By submitting an HTTP request to the server (possibly via an XHTML form). Such a request is made by a certain method. Each HTTP request method is different. There are several different possible requests, but we will concentrate on the two most frequently used ones. 05/25/12 Copyright Jukka Virtanen 2011 2

HTTP request methods GET Most common For simple document requests and small forms submission POST For submitting large forms to the server to be processed 05/25/12 Copyright Jukka Virtanen 2011 3

HTTP method GET Browsers can pass parameters to a server script via a request URI. Example: http://www.pic.ucla.edu/~v/serverscript.php?x=1&y=2 The URL of the target PHP script is: http://www.pic.ucla.edu/~v/serverscript.php The query string is: x=1&y=2 A form can send data this way, but you do not need a form to use this method. 05/25/12 Copyright Jukka Virtanen 2011 4

Query Strings appear after a question mark? in a URL. consist of name=value pairs separated by ampersands (&). name is the name attribute of a widget or control of the XHTML form spaces in value can be replaced by + signs Widget values in a query string can have special characters encoded as a % followed by a 2 digit hex ASCII code representing the special character. For more detail, consult : http://www.permadi.com/tutorial/urlencoding/ 05/25/12 Copyright Jukka Virtanen 2011 5

GET vs. POST GET method: Values (query string) are encoded directly into URI. Requests are cached by the browser, server, or proxy. Appropriate if the amount of data to send is small, and there are no side effects on the server. POST method: Values encoded in a separate part of the HTTP request (query string is not visible in URL as it is in GET). Requests cannot be cached (each request is independent and matters). 05/25/12 Copyright Jukka Virtanen 2011 6

Submitting an HTTP request using a form Click the submit button of the XHTML form to send form data to the script or JavaScript function that will process the form. The action attribute is a URI or a JavaScript function call. The method attribute specifies the HTTP request method. <form action="http://www.ucla.edu/some.php" method="post"> <!-- Form stuff --> </form> 05/25/12 Copyright Jukka Virtanen 2011 7

Processing Form Data How does a PHP script get information from a client? How does a PHP script get information from the server it is running on? How does PHP save information from a session with a client? Answer: Using PHP superglobal arrays 05/25/12 Copyright Jukka Virtanen 2011 8

Superglobal Arrays Superglobals are built-in variables that are always available in all scopes. There is no need to do global $variable; to access them within functions. $_SERVER stores data about the currently running server. $_ENV stores data about the current client's environment. $_GET stores data sent to the server using HTTP method GET. $_POST stores data sent to the server using HTTP method POST. $_COOKIE stores data contained in cookies on the client's computer. $_SESSION used by PHP to stores data pertaining to a the server's session with a client. 05/25/12 Copyright Jukka Virtanen 2011 9

Form example using get <form action="../php/calculator.php" method="get"> <fieldset> <label for="x">x: </label> <input type="text" name="x" id="x" /><br/> <label for="y">y:</label> <input type="text" name="y" id="y" /><br/><br/> <input type="submit" value="calculate Sum" /><br/><br/> <input type="reset" /> </fieldset> </form> 05/25/12 Copyright Jukka Virtanen 2011 10

PHP Script calculator.php <?php // Get the form data using form field names // as keys for superglobalarray $_GET. $x = $_GET['x']; $y = $_GET['y']; // Process form data print("$x". " + ". "$y = ". ($x + $y). "<br />");?> 05/25/12 Copyright Jukka Virtanen 2011 11

Example using post <form action="../php/calculator.php" method="post"> <fieldset> <label for="x">x: </label> <input type="text" name="x" id="x" /><br/> <label for="y">y:</label> <input type="text" name="y" id="y" /><br/><br/> <input type="submit" value="calculate Sum" /><br/><br/> <input type="reset" /> </fieldset> </form> 05/25/12 Copyright Jukka Virtanen 2011 12

Processing post data <?php // Get the form data using form field names // as keys for superglobalarray $_POST. $x = $_POST['x']; $y = $_POST['y']; // Process form data print("$x". " + ". "$y = ". ($x + $y). "<br />");?> 05/25/12 Copyright Jukka Virtanen 2011 13

Processing form data Often when we process form data we have to check what data was actually submitted through the form. Common method is illustrated in the following example: $from = (isset($_post["from"]))?$_post["from"]:""; Here we are checking if $_post array has value for the key called from. If it does we simply access the key and assign the value to a PHP variable that we call $from. If there is no such key/value we assign the default value of empty string ("") to the $from variable. We use the common C++ syntax:(bool expression)? do this : do that Where do this is done if bool expression is true and do that is done if it is false. 05/25/12 Copyright Jukka Virtanen 2011 14

email example See email example on examples page. Some notes on PHP mail function: bool mail ( string $to, string $subject, string $message [, string $additional_headers [, string $additional_parameters ]] ) additional_headers are optional. String to be inserted at the end of the email header. This is typically used to add extra headers (From, Cc, and Bcc). Check PHP.net for additional details on this function. 05/25/12 Copyright Jukka Virtanen 2011 15

Session Session is the time span during which a browser interacts with a particular server. It begins when a browser connects to a server. Ends when the connection is terminated or the browser connects to a different server. 05/25/12 Copyright Jukka Virtanen 2011 16

PHP Session Tracking You can create a unique session ID for your session by calling the function session_start(). Subsequent calls to session_start() retrieves the $_SESSION superglobal array. $_SESSION array contains key-value pairs that were created by the script during the session. 05/25/12 Copyright Jukka Virtanen 2011 17

Session example See examples. Note that currently PIC does not support session variables, so we have to make due with just reading the code and not being able to run the program :( I am working on getting this fixed... 05/25/12 Copyright Jukka Virtanen 2011 18

Temporary fix Create your own session save folder: public_html/sessions Then set the permissions to the folder so everyone can read and write to it. Now include the line in your PHP that sets the new session save path. session_save_path('/net/walnut/h1/grad/virtanen /public_html/sessions'); 05/25/12 Copyright Jukka Virtanen 2011 19

Using session to authenticate One of the common situations is that a person will have to log onto the website to gain access. Once in the website they are free to move among many pages. If the user has already logged in he should not have to re-login when he goes to the next page. We can keep track of the user and whether the user has logged in or not through the session variable. 05/25/12 Copyright Jukka Virtanen 2011 20

Example <?php // At this point user has already entered login information through a form. //This script has been called and users login and password have been compared against a database. //The password has been found to be correct and we will now grant the user access. 05/25/12 Copyright Jukka Virtanen 2011 21

Example continued //We now record that the user has logged in by storing his //login at the session variable. session_start(); $_SESSION['login'] = $login; //Now we might direct the user to the main part of the website. header("location: member-index.php"); 05/25/12 Copyright Jukka Virtanen 2011 22

Example continued We will know whether the user is logged in or not by the presence or absence of login in the session variable. If a variable login exists in the session, then the user has been logged in and authenticated. session_start(); if(!isset($_session['login']) { header("location: access-denied.php"); exit(); } If the login is set then the script will keep running and generate rest of the page. 05/25/12 Copyright Jukka Virtanen 2011 23

Example continued To logout the user we can just unset his login in the session variable. unset($_session['login']); 05/25/12 Copyright Jukka Virtanen 2011 24

Regular expressions Regular expressions are used for searching patterns of characters through text. For example you might want to extract all email addresses from a file. Regular expressions pop up in many places, not just PHP so they are well worth it to learn. Applications of regular expressions include data validation, data manipulation. You can match phone numbers, email addresses, url's, credit card numbers, social security numbers, zip codes, states, cities...(the list never ends). A huge script that is supposed to validate a user input and prepare it for a database can be reduced to only one line with the help of preg_replace. 05/25/12 Copyright Jukka Virtanen 2011 25

Patterns Regular expressions are all about specifying a pattern. You may have done a search on your computer for all doc files by searching for *.doc. Regular expression patterns give you way more flexibility, but the idea is the same. Example: [A-Z0-9\._]+@[A-Z0-9\.]+\.[A-Z]{2,4} Matches an email address. 05/25/12 Copyright Jukka Virtanen 2011 26

Building blocks of patterns The simplest match is a single character: Example: Pattern: a Input string: This is a fine day. Match will be found in the word a as well as in word day. Single character is any character except: [\^$.?*+() These are special characters that we will talk about shortly. Any special character can be written as a regular match character if you escape it with a \ 05/25/12 Copyright Jukka Virtanen 2011 27

Simple patterns Pattern: apple Result: Matches any occurrence of apple. Including Snapple. Pattern: ^apple Result: Matches only words that begin with apple. Pattern: apple$ Result: Matches only words that end with apple. 05/25/12 Copyright Jukka Virtanen 2011 28

Character classes Examples: Pattern: [JTV] Result: Matches a single character which is either J or T or V. Pattern: [a-z] Result: Matches any character a through z. Pattern: [^ucla] Result: Match any character that is not u,c,l,a. 05/25/12 Copyright Jukka Virtanen 2011 29

Predefined Character Classes Shorthand Literal value Matches \d [0-9] "0","1","2",... \D [^0-9] Non-digit \w [A-Za-z_0-9] Word character \W [^A-Za-z_0-9] Not a word character \s [\r\t\n\f] Whitespace \S [^\r\t\n\f] Not a whitespace 05/25/12 Copyright Jukka Virtanen 2011 30

Count indicators Pattern: [1-5]* Match: A number 1-5 may occur 0 or more times. Pattern: [H-S]+ Match: A letter H-S must occur at least once. Pattern: [v]? Match: The letter v may occur once or not at all. Pattern: [ucla]{2,3} Match: Any of u,c,l,a may occur twice or three times. 05/25/12 Copyright Jukka Virtanen 2011 31

More regular expression examples US zip code: [0-9]{5} UCLA Bruin email: [a-z][a-z0-9]{2,14}@ucla\.edu Telephone number in (XXX)-XXX-XXXX form: \(\d\d\d\)-\d\d\d-\d\d\d\d 05/25/12 Copyright Jukka Virtanen 2011 32

More regular expression examples "ab*": matches a string that has an a followed by zero or more b's ("a", "ab", "abbb", etc.); "ab+": same, but there's at least one b ("ab", "abbb", etc.); "ab?": there might be a b or not; "a?b+$": a possible a followed by one or more b's ending a string. You can also use bounds, which come inside braces and indicate ranges in the number of occurences: "ab{2}": matches a string that has an a followed by exactly two b's ("abb"); "ab{2,}": there are at least two b's ("abb", "abbbb", etc.); "ab{3,5}": from three to five b's ("abbb", "abbbb", or "abbbbb"). Note that you must always specify the first number of a range (i.e, "{0,2}", not "{,2}"). Also, as you might have noticed, the symbols '*', '+', and '?' have the same effect as using the bounds "{0,}", "{1,}", and "{0,1}", respectively. 05/25/12 Copyright Jukka Virtanen 2011 33

More regular expression examples "a(bc)*": matches a string that has an a followed by zero or more copies of the sequence "bc"; "a(bc){1,5}": one through five copies of "bc." There's also the ' ' symbol, which works as an OR operator: "hi hello": matches a string that has either "hi" or "hello" in it; "(b cd)ef": a string that has either "bef" or "cdef"; "(a b)*c": a string that has a sequence of alternating a's and b's ending in a c; A period ('.') stands for any single character: "a.[0-9]": matches a string that has an a followed by one character and a digit; "^.{3}$": a string with exactly 3 characters. "[ab]": matches a string that has either an a or a b (that's the same as "a b"); "[a-d]": a string that has lowercase letters 'a' through 'd' (that's equal to "a b c d" and even "[abcd]"); "^[a-za-z]": a string that starts with a letter; "[0-9]%": a string that has a single digit before a percent sign; ",[a-za-z0-9]$": a string that ends in a comma followed by an alphanumeric character. You can also list which characters you DON'T want -- just use a '^' as the first symbol in a bracket expression (i.e., "%[^a-za-z]%" matches a string with a character that is not a letter between two percent signs). 05/25/12 Copyright Jukka Virtanen 2011 34

PHP and regular expressions Recall that our pattern is a string composed of a regular expression. It looks something like: ^\d{5}(-\d{4})?$ Any guess what the above matches? Next we want to know how to search for this pattern in a given text. In PHP we have to add slashes around this expression. $pattern = "/^\d{5}(-\d{4})?$/"; 05/25/12 Copyright Jukka Virtanen 2011 35

PHP and regular expressions preg_match($pattern, $string ) Returns 1 if it finds a $pattern match in string, 0 otherwise. Stops looking in $string after the first match of $pattern. Returns FALSE if an error occurred. 05/25/12 Copyright Jukka Virtanen 2011 36

PHP and regular expressions Example: $url = "http://www.math.ucla.edu/~virtanen"; if (preg_match('/^(http https ftp)://([a-z0-9][a-z0-9_-]*(?:.[a-z0-9][a-z0-9_-]*)+):?(d+)?/?/i', $url)) { echo "Your url is ok."; } else { echo "Invalid url."; } 05/25/12 Copyright Jukka Virtanen 2011 37

PHP and regular expressions preg_match($pattern, $string, $matches ) Can also be used to return the first occurrence of the matched pattern. $matches is an array where 0 index contains the first match. <?php $s="the zip code is: 976435"; preg_match('/^\d{5}(-\d{4})?$/', $s, $matches); $zip = $matches[0];?> 05/25/12 Copyright Jukka Virtanen 2011 38

PHP and regular expressions Sub-patterns Inside a pattern we can specify a sub-pattern by using parenthesis. The sub-patterns can themselves be used to find a specific text inside a matched pattern. preg_match($pattern, $string, $matches) $matches is an array where 0 index contains the first match and subsequent indexes contain sub-matches. <?php $url="http://www.pic.ucla.edu/~virtanen"; preg_match('/http:\/\/([^\/]+)/', $url, $matches); $domain = $matches[1]; print "Domain is: $domain";?> Ouput: Domain is: www.pic.ucla.edu 05/25/12 Copyright Jukka Virtanen 2011 39

PHP and regular expressions preg_match_all($pattern, $string, $matches ) Searches for all matches to $pattern in $string and stores the substrings which matches in array $matches. Note: $matches[0] IS an array!! This is bit confusing but all the strings we want are contained in $matches[0]. Read the PHP manual for clarification. 05/25/12 Copyright Jukka Virtanen 2011 40

Example <?php $str = implode("",file("http://www.google.com/intl/en/contact/index.html ")); $Phone_Pattern = "/(\d)?(\s -)?(\()?(\d){3}(\))?(\s -){1}(\d){3} (\s -){1}(\d){4}/"; preg_match_all($phone_pattern,$str,$phone); for($i=0; $i < count($phone[0]); $i++) { echo $phone[0][$i]."<br>"; }?> 05/25/12 Copyright Jukka Virtanen 2011 41

PHP and regular expressions preg_grep($pattern, $arraytosearch) Returns the array entries that match $pattern. Values are indexed using the keys of $arraytosearch. 05/25/12 Copyright Jukka Virtanen 2011 42

PHP and regular expressions preg_replace($pattern, $replacement, $string) Replaces all substrings of $string which match $pattern by $replacement. Any of the parameters above can either be a string or an array of strings. Returns an array or string with replacements depending on whether $string is a string or an array of strings. If $pattern is an array and $replacement is a string, then PHP replaces any substring that matches any pattern with the same $replacement. If they both are arrays, PHP replaces substrings of $string that match pattern element at index i with replacement element at index i. 05/25/12 Copyright Jukka Virtanen 2011 43

Example This is bit beyond what we have time for, but I include the following example for completeness. Feel free to read up on this on the web. <?php echo preg_replace("/([cc]opyright) 20(09 10 11)/", "$1 2012", "Copyright 2009");?> In the above example we use back references in the replacement string. Back references make it possible for you to use part of a matched pattern in the replacement string. To use this feature, you should use parentheses to wrap any elements of your regular expression that you might want to use. You can refer to the text matched by subpattern with a dollar sign ($) and the number of the subpattern. For instance, if you are using subpatterns, $0 is set to the whole match, then $1, $2, and so on are set to the individual matches for each subpattern. 05/25/12 Copyright Jukka Virtanen 2011 44

Regular expressions and JS Patterns remain the same. There are different functions for matching patterns. 3 string methods search(/bruin/) returns the index of the first substring that matches the pattern /bruin/or -1 if no match. match(/bruin/) returns the first substring that matches the pattern /bruin/or undefined if no match replace(/trojan/, "bruin") replaces the first substring that matches the pattern /trojan/with the string "bruin" Example: "Hello".search(/He/); //returns 0 since He is found at index 0. 05/25/12 Copyright Jukka Virtanen 2011 45

Submitting forms via JS <form action="process_form.php" onsubmit="return validateform()" method="post"> First name: <input id="fname" type="text" name="fname"> <input type="submit" value="submit"> </form> function validateform() { value = document.getelementbyid("fname").value; if ((value.search(/[a-za-z]{2,100}/)==-1)){ alert("nope!!"); return false; } else {alert("ok!"); return true; } } 05/25/12 Copyright Jukka Virtanen 2011 46