SlideShare a Scribd company logo
 PHP Form Handling
◦ GET and POST
 PHP Form Validation
◦ Validate names
◦ Validate E-mail
◦ Validate URL
Superglobals
Superglobals — Built-in variables that are always
available in all scopes
 These superglobal variables are:
 $GLOBALS
 $_SERVER
 $_GET
 $_POST
 $_FILES
 $_COOKIE
 $_SESSION
 $_REQUEST
 $_ENV
 (PHP 4, PHP 5, PHP 7, PHP 8)
 $GLOBALS — References all variables
available in global scope
 An array containing references to all variables
which are currently defined in the global
scope of the script.
 The variable names are the keys of the array.
 $_SERVER
 (PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)
 $_SERVER — Server and execution
environment information
 Description
 $_SERVER is an array containing information
such as headers, paths, and script locations.
 The PHP superglobals $_GET and $_POST are
used to collect form-data.
When the user fills out the form above and
clicks the submit button:
 The form data is sent for processing to a PHP
file named "welcome.php".
 The form data is sent with the HTTP POST
method.
To display the submitted data you could simply echo all the variables.
The "welcome.php" looks like this:
The same result could also be achieved using
the HTTP GET method:
GET vs. POST
 Both GET and POST create an array
◦ e.g. array( key1 => value1, key2 => value2, key3
=> value3, ...).
 This array holds key/value pairs:
◦ Keys are the names of the form controls
◦ Values are the input data from the user.
 Both GET and POST are treated as $_GET and
$_POST.
 These are superglobals:
◦ which means that they are always accessible,
regardless of scope
 - you can access them from any function, class or file
without having to do anything special.
But
 $_GET is an array of variables passed to the
current script via the URL parameters.
 $_POST is an array of variables passed to the
current script via the HTTP POST method.
When to use GET?
 Information sent from a form with the GET
method is visible to everyone
◦ all variable names and values are displayed in the
URL.
 GET also has limits on the amount of
information to send.
◦ The limitation is about 2000 characters.
 GET may be used for sending non-sensitive
data.
When to use GET?
 However, because the variables are displayed
in the URL, it is possible to bookmark the
page.
 Note: GET should NEVER be used for sending
passwords or other sensitive information!
When to use GET?
 A bookmark is a place holder for a web page
that will allow you quick access to that page
instead of having to browse to it or search for
it.
 Instead of typing a web page in Google,
clicking the bookmark will direct you to that
page immediately.
When to use POST?
 Information sent from a form with the POST
method is invisible to others
◦ all names/values are embedded within the body of
the HTTP request
 Has no limits on the amount of information
to send.
 Moreover POST supports advanced
functionality such as support for multi-part
binary input while uploading files to server.
When to use POST?
 However, because the variables are not
displayed in the URL, it is not possible to
bookmark the page.
 Developers prefer POST for sending form
data.
 Think SECURITY when processing PHP forms!
 Example above does not contain any form
validation, it just shows how you can send
and retrieve form data.
The HTML form contains various input fields:
 Text fields
◦ Required
◦ Optional
 Radio buttons,
 A submit button:
Suppose we have the following form
 The validation rules for the form above are as follows:
 First we will look at the plain HTML code for
the form:
Text Fields
 text input elements
◦ The name
◦ Email
◦ Website
 Textarea
◦ comment field is a.
 The HTML code looks like this:
Radio Buttons
 The gender fields are radio buttons and the
HTML code looks like this:
The Form Element
 The HTML code of the form looks like this:
When the form is submitted, the form data is sent with
method="post".
What is the $_SERVER["PHP_SELF"] variable?
 The $_SERVER["PHP_SELF"] is a super global
variable that returns the filename of the
currently executing script.
 Sends the submitted form data to the page
itself, instead of jumping to a different page.
 This way, the user will get error messages on
the same page as the form.
 What is the htmlspecialchars() function?
 A function converts special characters to
HTML entities.
 This means that it will replace HTML
characters like < and > with &lt; and &gt;.

 This prevents attackers from exploiting the
code by injecting HTML or Javascript code
(Cross-site Scripting attacks) in forms.
 Validate Form Data With PHP
 The first thing we will do is to pass all
variables through PHP's htmlspecialchars()
function.
 When we use the htmlspecialchars() function;
then if a user tries to submit the following in a
text field:
 <script>location.href('http://www.hacked.com')</script>
 this would not be executed, because it would be
saved as HTML escaped code, like this:
 &lt;script&gt;location.href('http://www.hacked.co
m')&lt;/script&gt;
 The code is now safe to be displayed on a page
or inside an e-mail.
 We will also do two more things when the
user submits the form:
 Strip unnecessary characters (extra space,
tab, newline) from the user input data (with
the PHP trim() function)
 Remove backslashes () from the user input
data (with the PHP stripslashes() function)
 The next step is to create a function that will
do all the checking for us (which is much
more convenient than writing the same code
over and over again).
 Now, we can check each $_POST variable with
the test_input() function
 We check whether the form has been
submitted using $_SERVER["REQUEST_METHOD".
 If the REQUEST_METHOD is POST, then the
form has been submitted - and it should be
validated.
 If it has not been submitted, skip the
validation and display a blank form.
 However, in the example above, all input
fields are optional.
 The script works fine even if the user does
not enter any data.
PHP Forms - Required Fields
 In the previous example the validation rules are:
But in the code above all input fields were optional.
PHP - Required Fields
 To make a required filed:
 1-We have added some new variables:
◦ $nameErr,
◦ $emailErr,
◦ $genderErr,
◦ $websiteErr.
 These error variables will hold error messages
for the required fields
 2-We have also added an if –else for each
$_POST variable.
 This checks if the $_POST variable is empty
using empty() function
◦ If it is empty, an error message is stored in the
different error variables
◦ If it is not empty, it sends the user input data
through the test_input() function
PHP - Display The Error Messages
 3- Then in the HTML form, we add a little
script after each required field
◦ which generates the correct error message if
needed
 The <span> HTML element is a generic inline
container for phrasing content, which does
not represent anything.
 It can be used to group elements for styling
purposes (using the class or id attributes), or
because they share attribute values
PHP - Validate Name
 The code below shows a simple way to check
if the name field only contains letters,
dashes, apostrophes and whitespaces. If the
value of the name field is not valid, then store
an error message:
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z-' ]*$/",$name))
{
$nameErr = "Only letters and white space allowed";
}
The preg_match() function searches a string for
pattern, returning true if the pattern exists, and false
otherwise.
PHP - Validate E-mail
 The easiest and safest way to check whether
an email address is well-formed is to use
PHP's filter_var() function.
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$emailErr = "Invalid email format";
}
PHP - Validate URL
 The code below shows a way to check if a URL
address syntax is valid
◦ this regular expression also allows dashes in the
URL.
 If the URL address syntax is not valid, then
store an error message:
 $website = test_input($_POST["website"]);
if (!preg_match("/b(?:(?:https?|ftp)://|www.)[-a-
z0-9+&@#/%?=~_|!:,.;]*[-a-z0-
9+&@#/%=~_|]/i",$website))
 {
$websiteErr = "Invalid URL";
}
 How to use the CREATE TABLE command to
create a table
 How to use the INSERT command to enter
records
 How to use the SELECT command to retrieve
records
 How to use basic functions, the WHERE
clause, and the GROUP BY clause in SELECT
expressions
 How to select from multiple tables, using
JOIN
 How to use the UPDATE and REPLACE
commands to modify existing records
 How to use the DELETE command to remove
records
Table Creation Syntax
◦ The table creation command requires
◦ Name of the table
◦ Names of fields
◦ Definitions for each field
 The generic table creation syntax is
 CREATE TABLE table_name (column_name column_type);
 Using the INSERT Command
 After you have created some tables, you'll use
the SQL command INSERT for adding new
records to these tables.
 The basic syntax of INSERT is
INSERT INTO table_name (column list) VALUES (column
values);
forms.pptx
forms.pptx
forms.pptx
forms.pptx
forms.pptx
forms.pptx

More Related Content

Similar to forms.pptx

PHP Form Validation Technique
PHP Form Validation TechniquePHP Form Validation Technique
PHP Form Validation Technique
Morshedul Arefin
 
Php, mysq lpart4(processing html form)
Php, mysq lpart4(processing html form)Php, mysq lpart4(processing html form)
Php, mysq lpart4(processing html form)
Subhasis Nayak
 
Php forms
Php formsPhp forms
Php forms
Anne Lee
 
Form Handling using PHP
Form Handling using PHPForm Handling using PHP
Form Handling using PHP
Nisa Soomro
 
Chapter 1.Web Techniques_Notes.pptx
Chapter 1.Web Techniques_Notes.pptxChapter 1.Web Techniques_Notes.pptx
Chapter 1.Web Techniques_Notes.pptx
ShitalGhotekar
 
Php
PhpPhp
web2_lec6.pdf
web2_lec6.pdfweb2_lec6.pdf
web2_lec6.pdf
ssuser893014
 
Form handling in php
Form handling in phpForm handling in php
Form handling in php
Fahad Khan
 
php-mysql-tutorial-part-3
php-mysql-tutorial-part-3php-mysql-tutorial-part-3
php-mysql-tutorial-part-3tutorialsruby
 
&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/
&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/
&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/tutorialsruby
 
php-mysql-tutorial-part-3
php-mysql-tutorial-part-3php-mysql-tutorial-part-3
php-mysql-tutorial-part-3tutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />tutorialsruby
 
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...webhostingguy
 
10_introduction_php.ppt
10_introduction_php.ppt10_introduction_php.ppt
10_introduction_php.ppt
GiyaShefin
 
introduction_php.ppt
introduction_php.pptintroduction_php.ppt
introduction_php.ppt
ArunKumar313658
 
10_introduction_php.ppt
10_introduction_php.ppt10_introduction_php.ppt
10_introduction_php.ppt
MercyL2
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
Yoeung Vibol
 

Similar to forms.pptx (20)

PHP Form Validation Technique
PHP Form Validation TechniquePHP Form Validation Technique
PHP Form Validation Technique
 
Php, mysq lpart4(processing html form)
Php, mysq lpart4(processing html form)Php, mysq lpart4(processing html form)
Php, mysq lpart4(processing html form)
 
Php forms
Php formsPhp forms
Php forms
 
Form Handling using PHP
Form Handling using PHPForm Handling using PHP
Form Handling using PHP
 
Web development
Web developmentWeb development
Web development
 
PHP-04-Forms.ppt
PHP-04-Forms.pptPHP-04-Forms.ppt
PHP-04-Forms.ppt
 
Chapter 1.Web Techniques_Notes.pptx
Chapter 1.Web Techniques_Notes.pptxChapter 1.Web Techniques_Notes.pptx
Chapter 1.Web Techniques_Notes.pptx
 
Php
PhpPhp
Php
 
web2_lec6.pdf
web2_lec6.pdfweb2_lec6.pdf
web2_lec6.pdf
 
GET and POST in PHP
GET and POST in PHPGET and POST in PHP
GET and POST in PHP
 
Form handling in php
Form handling in phpForm handling in php
Form handling in php
 
php-mysql-tutorial-part-3
php-mysql-tutorial-part-3php-mysql-tutorial-part-3
php-mysql-tutorial-part-3
 
&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/
&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/
&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/
 
php-mysql-tutorial-part-3
php-mysql-tutorial-part-3php-mysql-tutorial-part-3
php-mysql-tutorial-part-3
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
 
10_introduction_php.ppt
10_introduction_php.ppt10_introduction_php.ppt
10_introduction_php.ppt
 
introduction_php.ppt
introduction_php.pptintroduction_php.ppt
introduction_php.ppt
 
10_introduction_php.ppt
10_introduction_php.ppt10_introduction_php.ppt
10_introduction_php.ppt
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
 

Recently uploaded

Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 

Recently uploaded (20)

Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 

forms.pptx

  • 1.
  • 2.  PHP Form Handling ◦ GET and POST  PHP Form Validation ◦ Validate names ◦ Validate E-mail ◦ Validate URL
  • 3. Superglobals Superglobals — Built-in variables that are always available in all scopes  These superglobal variables are:  $GLOBALS  $_SERVER  $_GET  $_POST  $_FILES  $_COOKIE  $_SESSION  $_REQUEST  $_ENV
  • 4.  (PHP 4, PHP 5, PHP 7, PHP 8)  $GLOBALS — References all variables available in global scope  An array containing references to all variables which are currently defined in the global scope of the script.  The variable names are the keys of the array.
  • 5.  $_SERVER  (PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)  $_SERVER — Server and execution environment information  Description  $_SERVER is an array containing information such as headers, paths, and script locations.
  • 6.  The PHP superglobals $_GET and $_POST are used to collect form-data.
  • 7.
  • 8.
  • 9. When the user fills out the form above and clicks the submit button:  The form data is sent for processing to a PHP file named "welcome.php".  The form data is sent with the HTTP POST method.
  • 10. To display the submitted data you could simply echo all the variables. The "welcome.php" looks like this:
  • 11. The same result could also be achieved using the HTTP GET method:
  • 12.
  • 13. GET vs. POST  Both GET and POST create an array ◦ e.g. array( key1 => value1, key2 => value2, key3 => value3, ...).  This array holds key/value pairs: ◦ Keys are the names of the form controls ◦ Values are the input data from the user.
  • 14.  Both GET and POST are treated as $_GET and $_POST.  These are superglobals: ◦ which means that they are always accessible, regardless of scope  - you can access them from any function, class or file without having to do anything special.
  • 15. But  $_GET is an array of variables passed to the current script via the URL parameters.  $_POST is an array of variables passed to the current script via the HTTP POST method.
  • 16. When to use GET?  Information sent from a form with the GET method is visible to everyone ◦ all variable names and values are displayed in the URL.  GET also has limits on the amount of information to send. ◦ The limitation is about 2000 characters.  GET may be used for sending non-sensitive data.
  • 17. When to use GET?  However, because the variables are displayed in the URL, it is possible to bookmark the page.  Note: GET should NEVER be used for sending passwords or other sensitive information!
  • 18. When to use GET?  A bookmark is a place holder for a web page that will allow you quick access to that page instead of having to browse to it or search for it.  Instead of typing a web page in Google, clicking the bookmark will direct you to that page immediately.
  • 19. When to use POST?  Information sent from a form with the POST method is invisible to others ◦ all names/values are embedded within the body of the HTTP request  Has no limits on the amount of information to send.  Moreover POST supports advanced functionality such as support for multi-part binary input while uploading files to server.
  • 20. When to use POST?  However, because the variables are not displayed in the URL, it is not possible to bookmark the page.  Developers prefer POST for sending form data.
  • 21.  Think SECURITY when processing PHP forms!  Example above does not contain any form validation, it just shows how you can send and retrieve form data.
  • 22. The HTML form contains various input fields:  Text fields ◦ Required ◦ Optional  Radio buttons,  A submit button:
  • 23. Suppose we have the following form
  • 24.  The validation rules for the form above are as follows:
  • 25.  First we will look at the plain HTML code for the form: Text Fields  text input elements ◦ The name ◦ Email ◦ Website  Textarea ◦ comment field is a.
  • 26.  The HTML code looks like this:
  • 27. Radio Buttons  The gender fields are radio buttons and the HTML code looks like this:
  • 28. The Form Element  The HTML code of the form looks like this: When the form is submitted, the form data is sent with method="post".
  • 29. What is the $_SERVER["PHP_SELF"] variable?  The $_SERVER["PHP_SELF"] is a super global variable that returns the filename of the currently executing script.  Sends the submitted form data to the page itself, instead of jumping to a different page.  This way, the user will get error messages on the same page as the form.
  • 30.  What is the htmlspecialchars() function?  A function converts special characters to HTML entities.  This means that it will replace HTML characters like < and > with &lt; and &gt;. 
  • 31.  This prevents attackers from exploiting the code by injecting HTML or Javascript code (Cross-site Scripting attacks) in forms.
  • 32.  Validate Form Data With PHP  The first thing we will do is to pass all variables through PHP's htmlspecialchars() function.
  • 33.  When we use the htmlspecialchars() function; then if a user tries to submit the following in a text field:  <script>location.href('http://www.hacked.com')</script>  this would not be executed, because it would be saved as HTML escaped code, like this:  &lt;script&gt;location.href('http://www.hacked.co m')&lt;/script&gt;  The code is now safe to be displayed on a page or inside an e-mail.
  • 34.  We will also do two more things when the user submits the form:  Strip unnecessary characters (extra space, tab, newline) from the user input data (with the PHP trim() function)  Remove backslashes () from the user input data (with the PHP stripslashes() function)
  • 35.  The next step is to create a function that will do all the checking for us (which is much more convenient than writing the same code over and over again).  Now, we can check each $_POST variable with the test_input() function
  • 36.
  • 37.
  • 38.
  • 39.  We check whether the form has been submitted using $_SERVER["REQUEST_METHOD".  If the REQUEST_METHOD is POST, then the form has been submitted - and it should be validated.  If it has not been submitted, skip the validation and display a blank form.
  • 40.  However, in the example above, all input fields are optional.  The script works fine even if the user does not enter any data.
  • 41. PHP Forms - Required Fields  In the previous example the validation rules are: But in the code above all input fields were optional.
  • 42. PHP - Required Fields  To make a required filed:  1-We have added some new variables: ◦ $nameErr, ◦ $emailErr, ◦ $genderErr, ◦ $websiteErr.  These error variables will hold error messages for the required fields
  • 43.  2-We have also added an if –else for each $_POST variable.  This checks if the $_POST variable is empty using empty() function ◦ If it is empty, an error message is stored in the different error variables ◦ If it is not empty, it sends the user input data through the test_input() function
  • 44.
  • 45. PHP - Display The Error Messages  3- Then in the HTML form, we add a little script after each required field ◦ which generates the correct error message if needed
  • 46.  The <span> HTML element is a generic inline container for phrasing content, which does not represent anything.  It can be used to group elements for styling purposes (using the class or id attributes), or because they share attribute values
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52. PHP - Validate Name  The code below shows a simple way to check if the name field only contains letters, dashes, apostrophes and whitespaces. If the value of the name field is not valid, then store an error message:
  • 53. $name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z-' ]*$/",$name)) { $nameErr = "Only letters and white space allowed"; } The preg_match() function searches a string for pattern, returning true if the pattern exists, and false otherwise.
  • 54. PHP - Validate E-mail  The easiest and safest way to check whether an email address is well-formed is to use PHP's filter_var() function. $email = test_input($_POST["email"]); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailErr = "Invalid email format"; }
  • 55. PHP - Validate URL  The code below shows a way to check if a URL address syntax is valid ◦ this regular expression also allows dashes in the URL.  If the URL address syntax is not valid, then store an error message:
  • 56.  $website = test_input($_POST["website"]); if (!preg_match("/b(?:(?:https?|ftp)://|www.)[-a- z0-9+&@#/%?=~_|!:,.;]*[-a-z0- 9+&@#/%=~_|]/i",$website))  { $websiteErr = "Invalid URL"; }
  • 57.
  • 58.  How to use the CREATE TABLE command to create a table  How to use the INSERT command to enter records  How to use the SELECT command to retrieve records  How to use basic functions, the WHERE clause, and the GROUP BY clause in SELECT expressions
  • 59.  How to select from multiple tables, using JOIN  How to use the UPDATE and REPLACE commands to modify existing records  How to use the DELETE command to remove records
  • 60. Table Creation Syntax ◦ The table creation command requires ◦ Name of the table ◦ Names of fields ◦ Definitions for each field  The generic table creation syntax is  CREATE TABLE table_name (column_name column_type);
  • 61.
  • 62.  Using the INSERT Command  After you have created some tables, you'll use the SQL command INSERT for adding new records to these tables.  The basic syntax of INSERT is INSERT INTO table_name (column list) VALUES (column values);