HTML Forms, GET, POST Methods Tran Anh Tuan Edit from Telerik Academy

Slides:



Advertisements
Similar presentations
Windows Basic and Dynamic Disk Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator Marian Marinov CEO of 1H Ltd.
Advertisements

Hybrid or Native?! Doncho Minkov Telerik Software Academy Senior Technical Trainer
Make swiftly iOS development Telerik Academy Telerik Academy Plus.
Amazon S 3, App Engine Blobstore, Google Cloud Storage, Azure Blobs Svetlin Nakov Telerik Software Academy academy.telerik.com.
RPN and Shunting-yard algorithm Ivaylo Kenov Telerik Software Academy academy.telerik.com Technical Assistant
Shortest paths in edge-weighted digraph Krasin Georgiev Technical University of Sofia g.krasin at gmail com Assistant Professor.
Telerik Software Academy Telerik School Academy.
Asynchronous Programming with C# and WinRT
Unleash the Power of JavaScript Tooling Telerik Software Academy End-to-end JavaScript Applications.
Touch and Gestures with Xamarin Forms
Character sequences, C-strings and the C++ String class, Working with Strings Learning & Development Team Telerik Software Academy.
Hybrid or Native?! Doncho Minkov Telerik Software Academy Senior Technical Trainer
Done already for your convenience! Telerik School Academy Unity 2D Game Development.
Processing Sequences of Elements Telerik School Academy C# Fundamentals – Part 1.
Welcome to the JSON-stores world Telerik Software Academy Databases.
Telerik Software Academy Telerik School Academy Creating E/R Diagrams with SQL Server.
The Business Plan and the Business Model Margarita Antonova Volunteer Telerik Academy academy.telerik.com Business System Analyst Telerik Corporation.
What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team Telerik Software Academy.
Making JavaScript code by template! Learning & Development Team Telerik Software Academy.
Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training Who, What, Why?
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Web Applications in Hatch Tran Anh Tuan Edit from Telerik Academy
Accessing SQL Server and MySQL – Live Demo Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Processing Matrices and Multidimensional Tables Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Learning & Development Telerik Software Academy.
Reading and Writing Text Files Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Telerik Software Academy ASP.NET Web Forms.
Classical OOP in JavaScript Classes and stuff Telerik Software Academy
Using Selenium for Mobile Web Testing Powered by KendoUI Telerik QA Academy Atanas Georgiev Senior QA Engineer KendoUI Team.
New features: classes, generators, iterators, etc. Telerik Academy Plus JavaScript.Next.
* ASP.NET Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking ASP.NET MVC Telerik Software Academy
Creating E/R Diagrams with SQL Server Management Studio and MySQL Workbench Svetlin Nakov Telerik Software Academy Manager Technical.
Throwing and Catching Exceptions Tran Anh Tuan Edit from Telerik Software Academy
Loops, Conditional Statements, Functions Tran Anh Tuan Edit from Telerik Academy
Telerik Software Academy ASP.NET Web Forms.
Private/Public fields, Module, Revealing Module Learning & Development Team Telerik Software Academy.
Building Data-Driven ASP.NET Web Forms Apps Telerik Software Academy ASP.NET Web Forms.
Telerik Software Academy End-to-end JavaScript Applications.
What is a Database, MySQL Specifics Trần Anh Tuấn Edit from Telerik Software Academy
Planning and Tracking Software Quality Yordan Dimitrov Telerik Corporation Team Leader, Team Pulse, Team Leader, Team Pulse, Telerik Corporation,
What you need to know Ivaylo Kenov Telerik Corporation Telerik Academy Student.
Data binding concepts, Bindings in WinJS George Georgiev Telerik Software Academy academy.telerik.com Technical Trainer itgeorge.net.
Pavel Kolev Telerik Software Academy Senior.Net Developer and Trainer
Objects, Properties, Primitive and Reference Types Learning & Development Team Telerik Software Academy.
When and How to Refactor? Refactoring Patterns Alexander Vakrilov Telerik Corporation Senior Developer and Team Leader.
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Doing the Canvas the "easy way"! Learning & Development Telerik Software Academy.
Creating and Running Your First C# Program Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Subroutines in Computer Programming Telerik School Academy C# Fundamentals – Part 1.
Correctly Formatting the Source Code Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer
Data Types, Primitive Types in C++, Variables – Declaration, Initialization, Scope Telerik Software Academy academy.telerik.com Learning and Development.
The past, the present, the future Learning & Development Team Telerik Software Academy.
Learn to Design Error Steady Code Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Connecting, Queries, Best Practices Tran Anh Tuan Edit from Telerik Software Academy
Processing Sequences of Elements Telerik Software Academy C# Fundamentals – Part 2.
Telerik JavaScript Framework Telerik Software Academy Hybrid Mobile Applications.
Telerik Software Academy Databases.
Integer, Floating-Point, Text Data, Variables, Literals Telerik Corporation
Things start to get serious Telerik Software Academy JavaScript OOP.
Learning & Development Mobile apps for iPhone & iPad.
Processing Matrices and Multidimensional Tables Telerik Software Academy C# Fundamentals – Part 2.
Nikolay Kostov Telerik Software Academy academy.telerik.com Team Lead, Senior Developer and Trainer
Functions and Function Expressions Closures, Function Scope, Nested Functions Telerik Software Academy
Implementing Control Logic in C# Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical trainer
Mocking tools for easier unit testing Telerik Software Academy High Quality Code.
What why and how? Telerik School Academy Unity 2D Game Development.
Windows Security Model Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator
Radoslav Georgiev Telerik Corporation
Presentation transcript:

HTML Forms, GET, POST Methods Tran Anh Tuan Edit from Telerik Academy

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>

 $_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!

 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

 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'

 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

 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 25

 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";}

 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

форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен SEO курс - оптимизация за търсачки уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop уроци по програмиране и уеб дизайн за ученици ASP.NET MVC курс – HTML, SQL, C#,.NET, ASP.NET MVC безплатен курс "Разработка на софтуер в cloud среда" BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране, книги – безплатно от Наков безплатен курс "Качествен програмен код" алго академия – състезателно програмиране, състезания ASP.NET курс - уеб програмиране, бази данни, C#,.NET, ASP.NET курсове и уроци по програмиране – Телерик академия курс мобилни приложения с iPhone, Android, WP7, PhoneGap free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране