Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Form Handling.

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

Error HandlingPHPMay-2007 : [#] PHP Error Handling.
Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
CHAPTER 3 MORE ON FORM HANDLING INCLUDING MULTIPLE FILES WRITING FUNCTIONS.
CSCI 215 Web Programming II Debugging & Error Handling.
JavaScript Forms Form Validation Cookies CGI Programs.
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.
. If the PHP server is an server or is aware of which server is the server, then one can write code that s information. –For example,
CST JavaScript Validating Form Data with JavaScript.
Lecture 8 : PHP Errors & Exceptions UFCFR Advanced Topics in Web Development II 2014/15 SHAPE Hong Kong.
MS3304: Week 4 PHP & HTML Forms. Overview HTML Forms elements refresher Sending data to a script via an HTML form –The post vs. get methods –Name value.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Advance Database Management Systems Lab no. 5 PHP Web Pages.
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.
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries.
Chapter 4 Handling User Input PHP Programming with MySQL 2nd Edition
JavaScript Form Validation
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.
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.
4-Sep-15 HTML Forms Mrs. Goins Web Design Class. Parts of a Web Form A Form is an area that can contain Form Control/Elements. Each piece of information.
INTERNET APPLICATION DEVELOPMENT For More visit:
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
Lecture 7 – Form processing (Part 2) SFDV3011 – Advanced Web Development 1.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
CHAPTER 12 COOKIES AND SESSIONS. INTRO HTTP is a stateless technology Each page rendered by a browser is unrelated to other pages – even if they are from.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
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.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Includes and Dates.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
A little PHP. Enter the simple HTML code seen below.
PHP Workshop ‹#› PHP Error Handling. PHP Workshop ‹#› Types There are 12 unique error types, which can be grouped into 3 main categories: Informational.
Chapter 8 Cookies And Security JavaScript, Third Edition.
Using Client-Side Scripts to Enhance Web Applications 1.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
NMD202 Web Scripting Week3. What we will cover today Includes Exercises PHP Forms Exercises Server side validation Exercises.
Website Development with PHP and MySQL Saving Data.
PHP2. PHP Form Handling The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. Name: Age:
ITM © Port, Kazman1 ITM 352 More on Forms Processing.
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.
HTML FORMS GET/POST METHODS. HTML FORMS HTML Forms HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes,
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.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
If statements and validation. If statement In programming the if statement allows one to test certain conditions and respond differently depending on.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
ITM © Port, Kazman1 ITM 352 More on Forms Processing.
Part 2 Lecture 9 PHP Superglobals and Form Handling.
PHP Error Handling Section :I Source: 1.
Making dynamic pages with javascript Lecture 1. Java script java versus javascript Javascript is a scripting language that will allow you to add real.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
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.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
8 th Semester, Batch 2008 Department Of Computer Science SSUET.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
PHP Syntax You cannot view the PHP source code by selecting "View source" in the browser - you will only see the output from the PHP file, which is plain.
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
PHP Form Processing * referenced from
PHP Exception Handling How to handle and create user-defined exceptions Mario Peshev Technical Trainer Software University
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,
Martin Kruliš Who is General Failure and why is he reading my disk? by Martin Kruliš (v1.0)1.
CGS 3066: Web Programming and Design Spring 2016 PHP.
INTERNET APPLICATIONS CPIT405 JavaScript Instructor: Rasha AlOmari
Linux Administration Working with the BASH Shell.
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,
Radoslav Georgiev Telerik Corporation
Simple PHP Web Applications Server Environment
Web Programming– UFCFB Lecture 17
Presentation transcript:

Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Form Handling

Open Source Server Side Scripting 2 ECA 236 HTML Forms  field names  no spaces  will match variable names (letters, numbers, underscores)  method  GET  POST  action  the script to which data is sent

Open Source Server Side Scripting 3 ECA 236 accessing variables Three ways to access form data: 1. $first_name and $last_name  variable names are the same as field names  register_globals must be set to ON in php.ini  least secure of the three ways First Name: Last Name:

Open Source Server Side Scripting 4 ECA 236 accessing variables cont … 2. superglobals: $_GET $_POST $_REQUEST  global associative arrays  $first_name = $_GET[‘first_name’];  only accepted variables are ones submitted through form  introduced in PHP version 4 First Name: Last Name:

Open Source Server Side Scripting 5 ECA 236 accessing variables cont … 3. $HTTP_GET_VARS or $HTTP_POST_VARS  associative arrays  $first_name = $HTTP_GET_VARS[‘first_name’];  PHP version 3 and earlier – still works in version 4  may be unsupported by future versions First Name: Last Name:

Open Source Server Side Scripting 6 ECA 236 self-submission  set the action of the form to itself from a document named test.php, if we wanted to send data to a separate form handler, the form would read: to reference itself, set action to test.php :

Open Source Server Side Scripting 7 ECA 236 self-submission cont …  isset( ) when passed a variable, isset( ) will return TRUE if that variable is set to some value, FALSE if the variable is NULL before form is submitted, all variables have a value of NULL once submitted, variable will have one of the following values:  information entered by user  empty string  TRUE

Open Source Server Side Scripting 8 ECA 236 self-submission cont … First Name: Last Name:

Open Source Server Side Scripting 9 ECA 236 self-submission cont … ”> A more efficient way of setting the action of a form to send data to itself is to use the $PHP_SELF variable accessed through the superglobal $_SERVER $PHP_SELF will always contain the current script’s name as the value Notice that the reference to the variable must be placed between the tagset

Open Source Server Side Scripting 10 ECA 236 validating form data  isset( )  returns TRUE if variable holds a value  drawback: returns TRUE if it holds an empty string if( isset( $first_name ) ) { echo “Hello, $first_name.”; } else{ echo “You forgot to enter your first name.”; }

Open Source Server Side Scripting 11 ECA 236 validating form data  empty( )  returns TRUE if argument is  “ ” (an empty string)  0 (zero as an integer)  “0” (zero as a string)  NULL  FALSE  array( ) (an empty array)  returns FALSE if it holds a non-empty, non-zero value if( empty( $first_name ) ) { echo “Please enter your first name”; }

Open Source Server Side Scripting 12 ECA 236 validating form data cont …  strlen( )  returns the length of a string  can be used to test for empty strings if( strlen( $first_name ) > 0 ){ echo “Hello, $first_name.”; } else{ echo “You forgot to enter your first name.”; }

Open Source Server Side Scripting 13 ECA 236 validating form data cont …  trim( )  removes white space from both ends of a variable  can be used to eliminate empty strings, and remove extraneous white space at beginning and end of variables $first_name = trim( $_GET[‘first_name’] );

Open Source Server Side Scripting 14 ECA 236 validating form data cont … radio buttons "> Male: Female:

Open Source Server Side Scripting 15 ECA 236 validating form data cont …  Purpose of validation  make sure the script has all the information it needs to do what it was designed to do  ensure the data is of the right type  added level of security by reducing user error and user maliciousness

Open Source Server Side Scripting 16 ECA 236 sending values manually Two other ways to pass variables and values 1. HTML form hidden input type

Open Source Server Side Scripting 17 ECA 236 sending values manually cont … 2. Append name=value pair to anchor tags to access these variables use $_GET or $_REQUEST superglobal Click Here for author Click Here for Subject $author = $_REQUEST[‘author’];

Open Source Server Side Scripting 18 ECA 236 error handling  ERRORS: fatal run-time errors, such as calling a function which does not exist – cause immediate termination  WARNINGS: non-fatal run-time errors, such as trying to include( ) a file that does not exist  NOTICES: less serious warnings which may result from a bug in your code, but may actually be intentional ( such as using an uninitialized variable)

Open Source Server Side Scripting 19 ECA 236 error handling cont … E_ERROR1Fatal run-time errors E_WARNING2Run-time warnings ( non-fatal errors ) E_PARSE4Compile-time parse errors E_NOTICE8Notices (may or may not be a problem ) E_CORE_ERROR16Fatal start-up errors E_CORE_WARNING32Non-fatal start-up errors E_COMPILE_ERROR64Fatal compile-time errors E_COMPILE_WARNING128Non-fatal compile-time errors E_USER_ERROR256User-generated error messages E_USER_WARNING512User-generated warnings E_USER_NOTICE1024User-generated notices E_ALL All errors, warnings, and notices

Open Source Server Side Scripting 20 ECA 236 error handling cont …  default error handling is set to E_ALL & ~E_NOTICE or E_ALL // beginning test echo “... begin test... ”; // include a non-existent variable echo “ The variable $no_such_var is not initialized. ”; // end test echo “... end test... “;... begin test... Notice: undefined variable: no_such_var in test_error.php The variable is not initialized.... end test...

Open Source Server Side Scripting 21 ECA 236 error handling cont …  example of a WARNING // beginning test echo “... begin test... ”; // include a non-existent file include( ‘no_such_file.inc’ ); // print more test echo “... end test... “;... begin test... Warning: main(no_such_file.inc): failed to open stream: No such file or directory in testError.php on line end test...

Open Source Server Side Scripting 22 ECA 236 error handling cont …  example of fatal error // beginning test echo “... begin test... ”; // call to a non-existent function no_such_function( ); // print more test echo “... end test... “;... begin test... Fatal error: Call to undefined function: no_such_function() in testError.php on line 29

Open Source Server Side Scripting 23 ECA 236 error handling cont …  in a live, production site  turn off error reporting  create custom error messages  during site development  use highest level of error reporting  display notices, warnings, and errors  to change level of error reporting  reconfigure php.ini  PHP functions

Open Source Server Side Scripting 24 ECA 236 error handling in php.ini  change level of error reporting in php.ini file  turn error display functionality on or off error_reporting = E_ALL ; or other appropriate value error_display = Off

Open Source Server Side Scripting 25 ECA 236 error handling functions  error_reporting( ) one argument: level of error reporting // turn off all error reporting error_reporting( 0 ); // beginning text echo “... begin text... ”; // call to a non-existent function no_such_function( ); // print more text echo “... end text... “;... begin text...

Open Source Server Side Scripting 26 ECA 236 error handling functions  error_reporting( ) // turn on all error reporting error_reporting( E_ALL ); // beginning text echo “... begin text... ”; // call to an undeclared variable echo $undeclared_var; // print more text echo “... end text... “;... begin text... Notice: Undefined variable: undeclared_var in testError.php on line end text...

Open Source Server Side Scripting 27 ECA 236 error handling functions  temporarily shut off error handling operator // beginning text echo “... begin text... ”; // call to a non-existent ); // print more text echo “... end text... “;... begin text...

Open Source Server Side Scripting 28 ECA 236 error handling functions  set_error_handler( ) one argument: name of custom function  custom error handler function takes at least 2, up to 5 arguments  error type  error message optional:  file name  line number  current PHP variables

Open Source Server Side Scripting 29 ECA 236 error handling functions  set_error_handler( ) // define custom error handler set_error_handler( ‘customError’ ); // create custom function to handle errors function customError( $type, $msg ) { echo " Error! "; echo " Error code: $type "; echo "Error msg: $msg "; echo " Please contact your system administrator. "; } Error! Error code: 2 Error msg: main(no_such_file.inc): failed to open stream: No such file or directory Please contact your system administrator.

Open Source Server Side Scripting 30 ECA 236 error handling functions  set_error_handler( ) setting all 5 arguments // define custom error handler set_error_handler( ‘customError’ ); // create custom function to handle errors function customError( $type, $msg, $file, $line, $vars ) { // statements... }

Open Source Server Side Scripting 31 ECA 236 error handling functions  set_error_handler( ) further customization function customError( $type, $msg) { switch( $type ){ case E_NOTICE: // do nothing break; case E_WARNING: echo “ A non-fatal error occurred: $msg ”; break; case E_ERROR: die( “ A fatal error occurred: $msg ” ); break; }

Open Source Server Side Scripting 32 ECA 236 error handling functions  set_error_handler( )  the default error handlers for E_ERROR and E_PARSE cannot be overwritten by a user-defined function.