Main PDF 5 - PHP Web Forms and Form Validation
Main PDF 5 - PHP Web Forms and Form Validation
Emerging Technologies
MODULE 5
PHP Web Forms and Form
Validation
PHP Predefined Functions
Objectives
• To apply the available super global variables for form
processing and validation.
• To differentiate the use of $_GET, $_POST, and
$_REQUEST super global variable in form processing and
know when to use it.
• To differentiate the use of Session and Cookies for form
security of a Web Site.
• To know the proper syntax for validating user inputs using
Regular Expression.
Superglobal variables
• $_SERVER
• is an array containing information such as headers, paths, and script
locations.
• entries were created by the web server
• Index ‘PHP_SELF’ contains the filename of the currently executing script.
• $_GET
• an associative array variables passed to the current script via the URL
parameters.
• $_POST
• an associative array of variables passed to the current script via the HTTP
POST method.
Superglobal variables
• $_REQUEST
• an associative array that by default contains the contents of $_GET,
$_POST, and $_COOKIE
• $_COOKIE
• an associative array of variables passed to the current script via HTTP
Cookies
• $_SESSION
• an associative array containing session variables available to the script
• $_ENV
• an associative array of variables passed to the current script via the
environment method
Superglobal variables
Example:
Superglobal variables
Output: before button was clicked Output: submit get button was clicked
• are mechanism for storing data in the remote browser and thus tracking or
identifying return users.
• small amount of information containing variable=value pair (user’s
computer).
• users can refuse to accepts cookies.
• Managing cookies can be done using setcookie() function
• syntax: setcookie()
bool setcookie ( string $name [, string $value [, int
$expire = 0 [, string $path [, string $domain [, bool
$secure = false [, bool $httponly = false ]]]]]] )
Cookies
Example: PHPSetSession.php
Example: PHPUnsetSession.php
Session
Example: PHPDisplaySession.php
Example: PHPDeleteSession.php
Session
Symbol Description
^ Marks the start of a string
$ Marks the end of a string
. Matches any single character
| Boolean OR
() Group elements
[abc] Item range (a,b or c)
[^abc] Not in range (every character except a,b, or c)
\s white-space character
a? Zero or one ‘a’ character. Equals to a{0,1}
a* Zero or more of ‘a’
Regular Expression
Symbol Description
a+ One or more of ‘a’
a{2} Exactly two of ‘a’
a{,5} Up to five of ‘a’
a{5,10} Between five to ten of ‘a’
\w Any alpha numeric character plus underscore. Equals to
[A-Za-z0-9_]
\W Any non alpha numeric characters
\s Any white-space character
\S Any non white-space character
\d Any digits. Equal to [0-9]
\D Any non-digits. Equal to [^0-9]
Regular Expression
Description
i Ignore Case
m Multiline Mode
S Extra analysis of pattern
u Pattern is treated as UTF-8
Regular Expression
Example
Example Description
‘/hello/’ It will match the word hello
‘/^hello/’ It will match hello at the start of a string. Possible matches
are hello orhelloworld, but not worldhello
‘/hello$/’ It will match hello at the end of a string.
‘/he.o/’ It will match any character between he and o. Possible
matches are heloor heyo, but not hello
‘/he?llo/’ It will match either llo or hello
‘/hello+/’ It will match hello on or more time. E.g. hello or hellohello
‘/he*llo/’ Matches llo, hello or hehello, but not hellooo
‘/hello|world/’ It will either match the word hello or world
‘/(A-Z)/’ Using it with the hyphen character, this pattern will match
every uppercase character from A to Z. E.g. A, B, C…
Regular Expression
Example: (continue)
Example Description
‘/[abc]/’ It will match any single character a, b or c
‘/abc{1}/’ Matches precisely one c character after the characters ab.
E.g. matchesabc, but not abcc
‘/abc{1,}/’ Matches one or more c character after the characters ab.
E.g. matches abcor abcc
‘/abc{2,4}/’ Matches between two and four c character after the
characters ab. E.g. matches abcc, abccc or abcccc, but
not abc
Regular Expression