Radoslav Georgiev Telerik Corporation www.telerik.com.

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

PHP I.
HTML Forms, GET, POST Methods Tran Anh Tuan Edit from Telerik Academy
Guide To UNIX Using Linux Third Edition
PHP Form Processing. Using Forms in PHP  Two steps to process  Display the form  Process the submitted data.
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
PHP Workshop ‹#› PHP: The Basics. PHP Workshop ‹#› What is it? PHP is a scripting language commonly used on web servers. –Stands for “PHP: Hypertext Preprocessor”
Stupid Browser Tricks with PHP and JavaScript By Chris Winikka.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Advance Database Management Systems Lab no. 5 PHP Web Pages.
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries.
Introduction to PHP and Server Side Technology. Slide 2 PHP History Created in 1995 PHP 5.0 is the current version It’s been around since 2004.
Chapter 4 Handling User Input PHP Programming with MySQL 2nd Edition
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
(c) Manzur Ashraf, Short course, KFUPM PHP & MySQL 1 Basic PHP Class 2.
PHP Forms and User Input The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input.
INTERNET APPLICATION DEVELOPMENT For More visit:
1 Creating Web Forms in HTML Web forms collect information from customers Web forms include different control elements including: –Input boxes –Selection.
Lecture 6 – Form processing (Part 1) SFDV3011 – Advanced Web Development 1.
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
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.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
IST 210: PHP BASICS IST 210: Organization of Data IST210 1.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
Chapter 8 Cookies And Security JavaScript, Third Edition.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Accessing Your MySQL Database from the Web with PHP (Ch 11) 1.
Website Development with PHP and MySQL Saving Data.
Web-Based Database Programming with PHP. Dept. of Computing Science, University of Aberdeen2 In this lecture you will learn PHP Basics PHP functions –To.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
XHTML & Forms. PHP and the WWW PHP and HTML forms – Forms are the main way users can interact with your PHP scrip Typical usage of the form tag in HTML.
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
Introduction to 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.
Part 2 Lecture 9 PHP Superglobals and Form Handling.
CS 174: Web Programming October 14 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
PHP A very brief introduction PHP1. PHP = PHP: Hypertext Processor A recursive acronym! PHP scripts are executed on the server side Executed by (a plugin.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Working with Forms in PHP HTTP GET / POST, Validation, Escaping, Input Types, Submitting Arrays, URL Redirecting, PHP Superglobals Svetlin Nakov Technical.
Since you’ll need a place for the user to enter a search query. Every form must have these basic components: – The submission type defined with the method.
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
PHP Form Processing * referenced from
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
CGS 3066: Web Programming and Design Spring 2016 PHP.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
Unit 4 Working with data. Form Element HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes, radio-buttons,
FORMS Explained By: Jasdeep Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
PHP using MySQL Database for Web Development (part II)
CGS 3066: Web Programming and Design Spring 2017
Session 2 Basics of PHP.
Receiving form Variables
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
DBW - PHP DBW2017.
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 III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
PHP (PHP: Hypertext Preprocessor)
HTML Forms and User Input
PHP.
Web DB Programming: PHP
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
PHP an introduction.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

Radoslav Georgiev Telerik Corporation

1. HTML Forms – Handling User Input 2. GET versus POST 3. cURL Magic 4. Escaping user data 5. Files

 The user sends data to the server only one way – with HTML Forms  They are sets of fields that determine the types of data to be sent  The server receives the filled-in data and produces new page  To handle the submitted data you need CGI script  The forms data is similar to arguments to a normal application

The user enters data and submits The form has "action" URL to send the data to The user enters data and submits The form has "action" URL to send the data to <? echo "Welcome ".$_POST ['username']."!"; ?> <? echo "Welcome ".$_POST ['username']."!"; ?> The PHP script receives the data as $_GET and $_POST arrays and runs The PHP script receives the data as $_GET and $_POST arrays and runs … Welcome Dimitar! … Welcome Dimitar! … Producing HTML that is result of the user's posted data Producing HTML that is result of the user's posted data

 PHP receives the data in the $_GET and $_POST arrays  URL parameters go into the $_GET array  Data from forms with method="post" do into the $_POST array  The request method is post  We can check what is the current request method in the $_SERVER array  Both arrays are global and can be used as any other array

 $_POST is associative array  The name attribute of form input becomes key in the array  If in the example form the user fills "John" and "mypass":  test.php will start with built-in array $_POST":  $_POST['name'] will be "John"  $_POST['pass"] will be "mypass" </form>

Live Demo

 $_GET is also associative array  If we open the URL:  The test2.php script will start with built-in array $_GET  $_GET['page'] will be 1  $_GET['user'] will be "john"

Live Demo

 The get requests passes the parameters trough the URL  Allows user to send link or bookmark the page as it is  URL is limited to 255 symbols  The post request passes the parameters trough the request body  User cannot open the page without first filling the post data in the form  Allows sending files  Unlimited size of data

 $_SERVER['REQUEST_METHOD'] holds the name of the request type  Can be one of 'GET', 'POST', 'HEAD', 'PUT'  Can be used to detect if user has submitted data or just opens the page from URL  Case sensitive!

Live Demo

 Escaping is parsing the input so it does not contain symbols or sets of character that may malfunction the code  Very important when the data is sent to database or system processes  Lack of escaping may lead to security issues  Usually necessary only for string-data  PHP is type-less language so all input should be checked!  PHP input is $_GET and $_POST arrays

 First step - making sure the input is with right type  PHP has several functions for type conversions and detection  is_int, is_double, is_numeric, is_string and other functions return true if variable is of the specified type is_int (1); // true is_int ('a'); // false is_int ('1'); // false

Live Demo

 We can read the variables in the necessary type  intval, floatval, doubleval, strval return the variable in the respective type  intval also supports optional second parameter for strings – base of conversion intval (42); //42 intval (4.2); // 4 intval ('042'); // 42 intval (true); // 1 intval ('49.99 лв'); // 49 intval(42, 8); // 42 intval('42', 8); // 34

 settype converts variable to specified type  Types can be: boolean (or bool), integer (or int), float (or double), string, array, object, null $foo = "5 bottles of beer"; $bar = true; settype ($foo, 'int'); // $foo becomes 5 Settype ($bar, 'string'); //$bar becomes '1'

Live Demo

 Type casting is changing the type of variable only for current operation  Syntax is – add the necessary type in brackets before the variable  Sometimes PHP does implicit casting $foo = true; echo (int)$foo; // prints 1, $foo doesn’t change echo (string)FALSE; // prints nothing… $foo = 0 + "123"; // $foo is integer 123 $foo = 0 + "123.4"; // $foo is float $bar = "$foo";// $bar is string '123.4' $foo = "123" + 0; // $foo is string 1230

Live Demo

 Strings must be escaped with extra caution  Quotes, semicolons, Unicode symbols and others may break the code  For instance – quote in a string that is passed on to SQL query may cause the server to execute malicious code  Most issues are when building string from input data that is passed on to other processes

 Example  What if $_POST['user'] contains:  So the command executed becomes:  And at address is sent the entire password file $cmd = "mkdir /users/".$_POST['user']; exec ($cmd); // executes $cmd as shell command dimitar; sendmail < /etc/passwd mkdir /users/dimitar; sendmail < /etc/passwd

 There are several characters to be careful for:  Quotes or double quotes – string ending (beginning)  Semicolons, pipe operators (|<>) – shell operators  Depending on the purpose there may be more and the escaping may differ  Usually you have to place backslash (\) in front of them

 addslashes – escapes all special symbols in a string (quote, double quote, backslash)  addcslashes – escapes given list of characters in a string  Will place backslash in front of all the listed symbols - ; | ' "  Be careful to escape the symbols in the list if necessary addcslashes ("dimitar; format c:", ';|<>\'"');

 There are several other functions for escaping that are useful in variety of cases  quotemeta – escapes the symbols. \ + * ? [ ^ ] ( $ )  htmlspecialchars – convert HTML special characters to entities: &, ", ', become & &quote; ' < and $gt;

 PHP (versions before 6) support the magic_quotes engine that escapes all necessary characters in the $_GET, $_POST and $_COOKIE array automatically  In versions before 5.2 it is turned on by default  Considered dangerous approach and thus – deprecated.  DO NOT USE IT!!! – although increases security may lead to data inconsistency  The developers should handle escaping manually with the supplied functions

How to store things 31

 Files are the basic way to store data  In PHP, there are many ways to read a file // if we have a file with name names.txt $content = file_get_contents(names.txt); $lines = file('test.txt'); // Loop through our array, show HTML source as HTML source; and line numbers too. foreach ($lines as $line_num => $line) { echo "Line # {$line_num} : ". htmlspecialchars($line). " \n"; echo "Line # {$line_num} : ". htmlspecialchars($line). " \n";}

Files.php 33

 Create a file questions.txt that is in the following format  First line – question id  Second line – question text  Third line – question answer  Create a web page that displays the question text and a user input for each question  Create a PHP Script as a POST action which checks if the answers are correct