PHP loeng 2.

Slides:



Advertisements
Similar presentations
Session 3BBK P1 ModuleApril 2010 : [#] Regular Expressions.
Advertisements

BBK P1 Module2010/11 : [‹#›] Regular Expressions.
PHP Workshop ‹#› Forms (Getting data from users).
PHP Form Processing. Using Forms in PHP  Two steps to process  Display the form  Process the submitted data.
PHP Form Processing. Using Forms in PHP  Two steps to process  Display the form  Process the submitted data.
Scripting Languages Chapter 8 More About Regular Expressions.
REGULAR EXPRESSIONS CHAPTER 14. REGULAR EXPRESSIONS A coded pattern used to search for matching patterns in text strings Commonly used for data validation.
XP Tutorial 14 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Unit 7 – Working with Forms 1. Creating a form 2. Accessing the submitted data 3. Common operations on forms.
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.
Week 4  Using PHP with HTML forms  Form Validation  Create your own Contact form Please Visit:
Regular Expressions Week 07 TCNJ Web 2 Jean Chu. Regular Expressions Regular Expressions are a powerful way to validate and format text strings that may.
PHP Workshop ‹#› Data Manipulation & Regex. PHP Workshop ‹#› What..? Often in PHP we have to get data from files, or maybe through forms from a user.
Week 7. Lecture 3 PHP Forms. PHP forms In part 2 of this course, we discussed html forms, php form is similar. Lets do a quick recap of the things we.
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
PHP with Regular Expressions Web Technologies Computing Science Thompson Rivers University.
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Regular Expressions for PHP Adding magic to your programming. Geoffrey Dunn
Copyright © 2003 Pearson Education, Inc. Slide 6a-1 The Web Wizard’s Guide to PHP by David Lash.
GREP. Whats Grep? Grep is a popular unix program that supports a special programming language for doing regular expressions The grammar in use for software.
Global Variables - Superglobals Several predefined variables in PHP are "superglobals", which means that they are always accessible, regardless of scope.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
Part 2 Lecture 9 PHP Superglobals and Form Handling.
+ FORMS HTML forms are used to pass data to a server. begins and ends a form Forms are made up of input elements Every input element has a name and value.
Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.
XP Tutorial 7 New Perspectives on JavaScript, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
PHP and Form Processing CS3520. Idea We have an HTML form that when user clicks on it in their browser a CGI HTTP request is created by the browser and.
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.
Regular Expressions.
Web Systems & Technologies
CGS 3066: Web Programming and Design Spring 2017
CSC 4630 Meeting 7 February 7, 2007.
Pemrograman WEB I Pertemuan 6.
Receiving form Variables
CS 330 Class 7 Comments on Exam Programming plan for today:
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Perl-Compatible Regular Expressions Part 1
How to Write Web Forms By Mimi Opkins.
เอกสารประกอบการบรรยายรายวิชา Web Technology
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Arrays: Checkboxes and Textareas
In this session, you will learn about:
Web Technologies PHP 5 Basic Language.
PHP Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used repeatedly in.
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 ©
Arrays and files BIS1523 – Lecture 15.
Passing variables between pages
PHP FORM HANDLING Post Method
Basic Contact Form user sends an
Web Programming– UFCFB Lecture 17
Intro to PHP & Variables
HTML Forms and User Input
CGI Programming Part II UNIX Security
Intro to PHP at Winthrop
Dr. John P. Abraham Professor UTRGV eCommerce CSCI 6314
PHP.
Data Manipulation & Regex
Creating Forms on a Web Page
Matcher functions boolean find() Attempts to find the next subsequence of the input sequence that matches the pattern. boolean lookingAt() Attempts to.
Lecture 25: Regular Expressions
Validation using Regular Expressions
PHP Lecture 11 Kanida Sinmai
The Web Wizard’s Guide to PHP by David A. Lash
PHP and JSON Topics Review JSON.
REGEX.
Lecture 23: Regular Expressions
Form Design & Validation
PHP –Regular Expressions
Presentation transcript:

PHP loeng 2

<html> <head><title>Random</title></head> <body> <p>I have randomly selected the number <?php $choice = rand(1, 100); echo $choice; ?>. Its square root is <?php echo sqrt($choice); ?>.</p> </body> </html>

Accessing user information. Creating forms <form method="post" action="random.php">  <p>Range Start: <input type="text" name="begin" /></p>  <p>Range End: <input type="text" name="end" /></p>  <p><input type="submit" value="Generate" /></p>  </form> 

Accessing user information. Continue <?php import_request_variables("pg", "form_"); ?> <html> <head> <title>Generate Random Number</title> </head> <body> <p>From the range <?php echo $form_begin; ?> to <?php echo $form_end; ?> I have selected the random number <?php echo rand($form_begin, $form_end); ?>.</p> </body> </html>

Input validation. The preg_match function if(preg_match("/^[0-9]{5}$/", $form_zipcode)) {      echo "The ZIP code must be a 5-digit number.";  }  sequence of digits will return 1 if that sequence appears anywhere in the string string starting and ending with a slash ('/') ^ start of string

Regular Expressions. Continue with validation if(preg_match("^[A-Z]{2}$", $lname)) {      echo "The ZIP code must be a 5-digit number.";  }

Regular Expressions () grouping [] range of characters . any character {} copies of the preceding pattern ? zero or one of the preceding pattern * any number of the preceding pattern (including zero) + at least one of the preceding pattern ^ start of string $ end of string \ treat next character literally instead of as a special symbol

Simple contact form <html><body> <form action="myform.php" method="post"> <p>Your Name: <input type="text" name="yourname" /><br /> E-mail: <input type="text" name="email" /></p> <p>Do you like this website? <input type="radio" name="likeit" value="Yes" checked="checked" /> Yes <input type="radio" name="likeit" value="No" /> No <input type="radio" name="likeit" value="Not sure" /> Not sure</p> <p>Your comments:<br /> <textarea name="comments" rows="10" cols="40"></textarea></p> <p><input type="submit" value="Send it!"></p> </form> </body></html>

All variables passed to the current script via the HTTP POST method are stored in associative array $_POST.

Simple contact form. Script <html> <body> Your name is: <?php echo $_POST['yourname']; ?><br /> Your e-mail: <?php echo $_POST['email']; ?><br /> <br /> Do you like this website? <?php echo $_POST['likeit']; ?><br /> Comments:<br /> <?php echo $_POST['comments']; ?> </body> </html>

Validating forms with PHP. htmlspecialchars() <?php $yourname = htmlspecialchars($_POST['yourname']); $email = htmlspecialchars($_POST['email']); $likeit = htmlspecialchars($_POST['likeit']); $comments = htmlspecialchars($_POST['comments']); ?> <html><body> Your name is: <?php echo $yourname; ?><br /> Your e-mail: <?php echo $email; ?><br /><br /> Do you like this website? <?php echo $likeit; ?><br /><br /> Comments:<br /> <?php echo $comments; ?> </body></html> This function will replace HTML chars like < and > to their HTML version < and >.

Why we need the htmlspecialchars() ? Example. <script>location.href('http://www.SPAM.com')</script>

What else to check? Let's do two more things: 1. strip unnecessary characters from the data. 2. if quotes are escaped with a slash \ let's remove that.

$yourname = check_input($_POST['yourname']); <?php $yourname = check_input($_POST['yourname']); $email = check_input($_POST['email']); $likeit = check_input($_POST['likeit']); $comments = check_input($_POST['comments']); ?> <html><body> Your name is: <?php echo $yourname; ?><br /> Your e-mail: <?php echo $email; ?><br /><br /> Do you like this website? <?php echo $likeit; ?><br /><br /> Comments:<br /> <?php echo $comments; ?> </body></html> function check_input($data){ $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; }?>

Let's edit the check_input function from the previous page function check_input($data, $problem=' ') { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($problem && strlen($data) == 0) die($problem); } return $data; By default $problem is empty $yourname = check_input($_POST['yourname'], "Enter your name");

<?php $yourname = check_input($_POST['yourname'], "Enter your name"); $email = check_input($_POST['email']); $likeit = check_input($_POST['likeit']); $comments = check_input($_POST['comments'], "Write your comments"); ?> <html><body> Your name is: <?php echo $yourname; ?><br /> Your e-mail: <?php echo $email; ?><br /><br /> Do you like this website? <?php echo $likeit; ?><br /><br /> Comments:<br /> <?php echo $comments; ?> </body></html> <?php function check_input($data, $problem=''){ $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($problem && strlen($data) == 0) { die($problem); } return $data; ?>

<?php function check_input($data, $problem='') { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($problem && strlen($data) == 0) { show_error($problem); } return $data; function show_error($myError) { ?> <html> <body> <b>Please correct the following error:</b><br /> <?php echo $myError; ?> </body> </html> <?php exit(); }

Validate e-mail address $email = htmlspecialchars($_POST['email']); if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { die("E-mail address not valid"); } Character Description . a single character \s a whitespace character (space, tab, newline) \S non-whitespace character \d a digit (0-9) \D a non-digit \w a word character (a-z, A-Z, 0-9, _) \W a non-word character p+ It matches any string containing at least one p. \w\- \w match any word character [a-zA-Z0-9_] \- matches the character - literally

FILTER_VALIDATE <!DOCTYPE html> <html><body> <?php // Variable to check $email = "john.doe@example.com"; // Validate email if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) { echo("$email is a valid email address"); } else { echo("$email is not a valid email address"); } ?> </body></html>

$pattern = "^[_a-z0-9-]+(\. [_a-z0-9-]+). @[a-z0-9-]+(\. [a-z0-9-]+) $pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";

Validate URL address $url = htmlspecialchars($_POST['website']); if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i",$url)) { die("URL address not valid"); }

Digits 0-9 only if (preg_match("/\D/",$age)) { die("Please enter numbers only for Age"); }

Letters a-z and A-Z only (no spaces, digits or any other characters) if (preg_match("/[^a-zA-Z]/",$text)) { die("Please enter letters a-z and A-Z only!"); }