Applications Development and
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
Output: submit post button was clicked
Cookies
• 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: PHPSetCookies.php Output 1: cookies were set
Example: PHPDisplayCookies.php Output 2: after 10 secs
Example: PHPDeleteCookies.php Output 3: delete cookies
Session
• are mechanism for storing data on the server itself.
• is the time that a user spends at your Web site.
• more secure than cookies and can store much more information
• to open a session use session_start() function
• always set at the beginning of each Web page.
• to close the session use session_destroy() function
• gets rid of all the session variable information that’s stored in the session
file.
• the statement does not affect the variables set on the current page.
Session
• to unset session variables use unset() function
• syntax
unset($_SESSION[‘varname’]);
Example: PHPSetSession.php
Example: PHPUnsetSession.php
Session
Example: PHPDisplaySession.php
Example: PHPDeleteSession.php
Session
Output: user load PHPDisplaySession.php page
Output: user clicked the Login link
Output: user clicked the email link
Output: user clicked the logout link
Regular Expression
• were used to efficiently search for patterns in a given text.
• also known as regex or regexp.
• PHP implements Percl Compatible Regular Expression (PCRE)
• PCRE function starts with preg_
• preg_match() function
• Performs a regular expression match
• Syntax: int preg_match ( string $pattern , string
$subject [, array &$matches [, int $flags = 0 [, int
$offset = 0 ]]] )
Regular Expression
Regex Meta Characters
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
Regex Meta Characters (continue)
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
Regex Pattern Modifiers
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
Useful Regex Functions
Email validation