PHP Form
PHP Form Handling
We can create and use forms in PHP. To get form data, we need to use PHP superglobals $_GET and
$_POST.
The form request may be get or post. To retrieve data from get request, we need to use $_GET, for post
request $_POST.
PHP Get Form
Get request is the default form request. The data passed through get request is visible on the URL
browser so it is not secured. You can send limited amount of data through get request.
Let's see a simple example to receive data from get request in PHP.
File: form1.html
<form action="welcome.php" method="get">
Name: <input type="text" name="name"/>
<input type="submit" value="visit"/>
</form>
File: welcome.php
<?php
$name=$_GET["name"];//receiving name field value in $name variable
echo "Welcome, $name";
?>
PHP Post Form
Post request is widely used to submit form that have large amount of data such as file upload, image
upload, login form, registration form etc.
The data passed through post request is not visible on the URL browser so it is secured. You can send
large amount of data through post request.
Let's see a simple example to receive data from post request in PHP.
File: form1.html
<form action="login.php" method="post">
<table>
<tr><td>Name:</td><td> <input type="text" name="name"/></td></tr>
tr><td>Password:</td><td> <input type="password" name="password"/></td></tr>
<tr><td colspan="2"><input type="submit" value="login"/> </td></tr>
</table>
</form>
File: login.php
<?php
$name=$_POST["name"];//receiving name field value in $name variable
$password=$_POST["password"];//receiving password field value in $password variable
echo "Welcome: $name, your password is: $password";
?>
Output:
PHP Regular Expressions
Regular expressions are commonly known as regex. These are nothing more than a pattern or a
sequence of characters, which describe a special search pattern as text string.
Regular expression allows you to search a specific string inside another string. Even we can replace one
string by another string and also split a string into multiple chunks. They use arithmetic operators (+, -,
^) to create complex expressions.
By default, regular expressions are case sensitive.
Advantage and uses of Regular Expression
Regular expression is used almost everywhere in current application programming. Below some
advantages and uses of regular expressions are given:
1. Regular expression helps the programmers to validate text string.
2. It offers a powerful tool to analyze and search a pattern as well as to modify the text string.
3. By using regexes functions, simple and easy solutions are provided to identify the patterns.
4. Regexes are helpful for creating the HTML template system recognizing tags.
5. Regexes are widely used for browser detection, form validation, spam filtration, and password
strength checking.
6. It is helpful in user input validation testing like email address, mobile number, and IP address.
7. It helps in highlighting the special keywords in file based upon the search result or input.
8. Metacharacters allow us to create more complex patterns.
You can create complex search patterns by applying some basic rules of regular expressions. Many
arithmetic operators (+, -, ^) are also used by regular expressions to create complex patterns.
Operators in Regular Expression
Operator Description
^ It indicates the start of string.
$ It indicates the end of the string.
. It donates any single character.
() It shows a group of expressions.
[] It finds a range of characters, e.g., [abc] means a, b, or c.
[^] It finds the characters which are not in range, e.g., [^xyz] means NOT x, y, or z.
- It finds the range between the elements, e.g., [a-z] means a through z.
| It is a logical OR operator, which is used between the elements. E.g., a|b, which means either a OR
b.
? It indicates zero or one of preceding character or element range.
* It indicates zero or more of preceding character or element range.
+ It indicates zero or more of preceding character or element range.
{n} It denotes at least n times of preceding character range. For example - n{3}
{n, } It denotes at least n, but it should not be more than m times, e.g., n{2,5} means 2 to 5 of n.
{n, m} It indicates at least n, but it should not be more than m times. For example - n{3,6} means 3 to 6 of
n.
\ It denotes the escape character.
Special character class in Regular Expression
Special Character Description
\n It indicates a new line.
\r It indicates a carriage return.
\t It represents a tab.
\v It represents a vertical tab.
\f It represents a form feed.
\xxx It represents an octal character.
\xxh It denotes hexadecimal character hh.
PHP offers two sets of regular expression functions:
1. POSIX Regular Expression
2. PERL Style Regular Expression
POSIX Regular Expression
The structure of POSIX regular expression is similar to the typical arithmetic expression: several
operators/elements are combined together to form more complex expressions.
The simplest regular expression is one that matches a single character inside the string. For example -
"g" inside the toggle or cage string. Let's introduce some concepts being used in POSIX regular
expression:
Brackets
Brackets [] have a special meaning when they are used in regular expressions. These are used to find the
range of characters inside it.
Expression Description
[0-9] It matches any decimal digit 0 to 9.
[a-z] It matches any lowercase character from a to z.
[A-Z] It matches any uppercase character from A to Z.
[a-Z] It matches any character from lowercase a to uppercase Z.
The above ranges are commonly used. You can use the range values according to your need, like [0-6]
to match any decimal digit from 0 to 6.
Quantifiers
A special character can represent the position of bracketed character sequences and single characters.
Every special character has a specific meaning. The given symbols +, *, ?, $, and {int range} flags all follow
a character sequence.
Expression Description
p+ It matches any string that contains atleast one p.
p* It matches any string that contains one or more p's.
p? It matches any string that has zero or one p's.
p{N} It matches any string that has a sequence of N p's.
p{2,3} It matches any string that has a sequence of two or three p's.
p{2, } It matches any string that contains atleast two p's.
p$ It matches any string that contains p at the end of it.
^p It matches any string that has p at the start of it.
PHP Regexp POSIX Function
PHP provides seven functions to search strings using POSIX-style regular expression -
Function Description
ereg() It searches a string pattern inside another string and returns true if the pattern matches
otherwise return false.
ereg_replace() It searches a string pattern inside the other string and replaces the matching text with the
replacement string.
eregi() It searches for a pattern inside the other string and returns the length of matched string if
found otherwise returns false. It is a case insensitive function.
eregi_replace() This function works same as ereg_replace() function. The only difference is that the search for
pattern of this function is case insensitive.
split() The split() function divide the string into array.
spliti() It is similar to split() function as it also divides a string into array by regular expression.
Sql_regcase() It creates a regular expression for case insensitive match and returns a valid regular expression
that will match string.
PERL Style Regular Expression
Perl-style regular expressions are much similar to POSIX. The POSIX syntax can be used with Perl-style
regular expression function interchangeably. The quantifiers introduced in POSIX section can also be
used in PERL style regular expression.
Metacharacters
A metacharacter is an alphabetical character followed by a backslash that gives a special meaning to the
combination.
For example - '\d' metacharacter can be used search large money sums: /([\d]+)000/. Here /d will search
the string of numerical character.
Below is the list of metacharacters that can be used in PERL Style Regular Expressions -
Character Description
. Matches a single character
\s It matches a whitespace character like space, newline, tab.
\S Non-whitespace character
\d It matches any digit from 0 to 9.
\D Matches a non-digit character.
\w Matches for a word character such as - a-z, A-Z, 0-9, _
\W Matches a non-word character.
[aeiou] It matches any single character in the given set.
[^aeiou] It matches any single character except the given set.
(foo|baz|bar) Matches any of the alternatives specified.
Modifiers
There are several modifiers available, which makes the work much easier with a regular expression. For
example - case-sensitivity or searching in multiple lines, etc.
Below is the list of modifiers used in PERL Style Regular Expressions -
Character Description
i Makes case insensitive search
m It specifies that if a string has a carriage return or newline characters, the $ and ^ operator will
match against a newline boundary rather than a string boundary.
o Evaluates the expression only once
s It allows the use of .(dot) to match a newline character
x This modifier allows us to use whitespace in expression for clarity.
g It globally searches all matches.
cg It allows the search to continue even after the global match fails.
PHP Regexp POSIX Function
PHP currently provides seven functions to search strings using POSIX-style regular expression -
Function Description
preg_match() This function searches the pattern inside the string and returns true if the pattern exists
otherwise returns false.
preg_match_all() This function matches all the occurrences of pattern in the string.
preg_replace() The preg_replace() function is similar to the ereg_replace() function, except that the regular
expressions can be used in search and replace.
preg_split() This function exactly works like split() function except the condition is that it accepts
regular expression as an input parameter for pattern. Mainly it divides the string by a
regular expression.
preg_grep() The preg_grep() function finds all the elements of input_array and returns the array
elements matched with regexp (relational expression) pattern.
preg_quote() Quote the regular expression characters.
PHP Cookie
PHP cookie is a small piece of information which is stored at client browser. It is used to recognize the
user.
Cookie is created at server side and saved to client browser. Each time when client sends request to the
server, cookie is embedded with request. Such way, cookie can be received at the server side.
In short, cookie can be created, sent and received at server end.
PHP setcookie() function
PHP setcookie() function is used to set cookie with HTTP response. Once cookie is set, you can access it
by $_COOKIE superglobal variable.
Syntax
bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path
[, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )
Example
setcookie("CookieName", "CookieValue");/* defining name and value only*/
setcookie("CookieName", "CookieValue", time()+1*60*60);//using expiry in 1 hour(1*60*60 seconds or
3600 seconds)
setcookie("CookieName", "CookieValue", time()+1*60*60, "/mypath/", "mydomain.com", 1);
PHP $_COOKIE
PHP $_COOKIE superglobal variable is used to get cookie.
Example
$value=$_COOKIE["CookieName"];//returns cookie value
PHP Cookie Example
File: cookie1.php
<?php
setcookie("user", "Sonoo");
?>
<html>
<body>
<?php
if(!isset($_COOKIE["user"])) {
echo "Sorry, cookie is not found!";
} else {
echo "<br/>Cookie Value: " . $_COOKIE["user"];
}
?>
</body>
</html>
Output:
Sorry, cookie is not found!
Firstly cookie is not set. But, if you refresh the page, you will see cookie is set now.
Output:
Cookie Value: Sonoo
PHP Delete Cookie
If you set the expiration date in past, cookie will be deleted.
File: cookie1.php
<?php
setcookie ("CookieName", "", time() - 3600);// set the expiration date to one hour ago
?>
PHP Session
PHP session is used to store and pass information from one page to another temporarily (until user close
the website).
PHP session technique is widely used in shopping websites where we need to store and pass cart
information e.g. username, product code, product name, product price etc from one page to another.
PHP session creates unique user id for each browser to recognize the user and avoid conflict between
multiple browsers.
PHP session_start() function
PHP session_start() function is used to start the session. It starts a new or resumes existing session. It
returns existing session if session is created already. If session is not available, it creates and returns new
session.
Syntax
bool session_start ( void )
Example
session_start();
PHP $_SESSION
PHP $_SESSION is an associative array that contains all session variables. It is used to set and get session
variable values.
Example: Store information
$_SESSION["user"] = "Sachin";
Example: Get information
echo $_SESSION["user"];
PHP Session Example
File: session1.php
<?php
session_start();
?>
<html>
<body>
<?php
$_SESSION["user"] = "Sachin";
echo "Session information are set successfully.<br/>";
?>
<a href="session2.php">Visit next page</a>
</body>
</html>
File: session2.php
<?php
session_start();
?>
<html>
<body>
<?php
echo "User is: ".$_SESSION["user"];
?>
</body>
</html>
PHP Session Counter Example
A session mechanism can be used to store page views which increment on each refresh and show the
count on a webpage. A session is user specific and for every user, a separate session is created along
with a separate session variable which is associated with that session.
File: sessioncounter.php
<?php
session_start();
if (!isset($_SESSION['counter'])) {
$_SESSION['counter'] = 1;
} else {
$_SESSION['counter']++;
}
echo ("Page Views: ".$_SESSION['counter']);
?>