Chapter 4.1-4.3
Chapter 4.1-4.3
Variables :
Server configuration and request information—including form parameters and cookies—are accessible
from PHP scripts. Collectively, this information is referred to as EGPCS (environment, GET, POST, cookies,
and server).
PHP creates six global arrays that contain the EGPCS information. The global arrays are:
$_COOKIE : Contains any cookie values passed as part of the request, where the keys of the array are the
names of the cookies
$_GET : Contains any parameters that are part of a GET request, where the keys of the array are the names
of the form parameters
$_POST : Contains any parameters that are part of a POST request, where the keys of the array are the
names of the form parameters
$_FILES : Contains information about any uploaded files
$_SERVER : Contains useful information about the web server.
$_ENV : Contains the values of any environment variables, where the keys of the array are the names of the
environment variables such as database connectivity variables
These variables are not only global, but are also visible from within function definitions.
The $_REQUEST array is also created by PHP automatically. The $_REQUEST array contains the elements
of the $_GET, $_POST, and $_COOKIE arrays all in one array variable.
Server Information : The $_SERVER array contains a lot of useful information from the web server. Much
of this information comes from the environment variables required in the CGI specification.
Processing Forms : With PHP, form parameters are available in the $_GET and $_POST arrays.
Methods : There are two HTTP methods that a client can use to pass form data to the server: GET and
POST. The method that a particular form uses is specified with the method attribute to the form tag.
A GET request encodes the form parameters in the URL and is referred as a query string. In the URL, the
text that follows the ? is the query string: /path/to/abc.php?word=despicable&length=3. Because all form’s
parameters are encoded in the URL with a GET request, users can bookmark GET queries. The HTTP
specification says that GET requests are idempotent—that is, one GET request for a particular URL,
including form parameters, is the same as two or more requests for that URL. Thus, web browsers can cache
the response pages for GET requests, because the response page doesn’t change regardless of how many
times the page is loaded. Because of idempotence, GET requests should be used only for queries such as
splitting a word into smaller chunks or multiplying numbers, where the response page is never going to
change. The GET method is designed for retrieving information, such as a document, an image, or the
results of a database query, from the server.
A POST request passes the form parameters in the body of the HTTP request, does not add data to URL.
The most visible difference between GET and POST is the URL line. POST requests are not idempotent.
This means that they cannot be cached, and the server is re-contacted every time the page is displayed. This
makes POST requests the appropriate choice for queries whose response pages may change over time—for
example, displaying the contents of a shopping cart or the current messages in a bulletin board. The POST
method is meant for posting information, such as a credit card number or information that is to be stored in a
database, to the server.
The following table lists the most important elements that can go inside $_SERVER:
Element/Code Description
$_SERVER['PHP_SELF'] Returns the filename of the currently executing script
$_SERVER['REQUEST_METH Returns the request method used to access the page (such as POST)
OD']
$_SERVER['QUERY_STRING'] Returns the query string if the page is accessed via a query string
$_SERVER['HTTP_REFERER'] Returns the complete URL of the current page (not reliable because
not all user-agents support it)
$_SERVER['REMOTE_ADDR'] Returns the IP address from where the user is viewing the current
page
$_SERVER['REMOTE_HOST'] Returns the Host name from where the user is viewing the current
page
$_SERVER['REMOTE_PORT'] Returns the port being used on the user's machine to communicate
with the web server
$_SERVER['SCRIPT_FILENAM Returns the absolute path name of the currently executing script
E']
$_SERVER['SERVER_ADMIN'] Returns the value given to the SERVER_ADMIN directive in the web
server configuration file (if your script runs on a virtual host, it will
be the value defined for that virtual host)
$_SERVER['SERVER_PORT'] Returns the port on the server machine being used by the web server
for communication (such as 80)
$_SERVER['SERVER_SIGNAT Returns the server version and virtual host name which are added
URE']
to server-generated pages
$_SERVER['PATH_TRANSLAT Returns the file system based path to the current script
ED']
Parameters (About data entered in form): Use the $_POST, $_GET, and $_FILES arrays to access form
parameters from your PHP code. The keys are the parameter names, and the values are the values of those
parameters.
Example : Form processing with html and php two separate files.
Output : Output :
Self-Processing Pages : One PHP page can be used to both generate a form and process it.
<html>
<head><title>Square of a number</title></head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
Enter a number: <input type="text" name="number" />
<br />
<input type="submit" value="Calculate Square" />
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$no= $_POST['number'];
$s = $no*$no;
echo "Square of $no = $s";
}
?>
</body>
</html>
Output:
Example :
<html>
<body>
<form name="student" method="post" action="st.php">
Enter Student Name : <input type="text" name="sname">
<br>Enter Roll Number : <input type="text" name="rno">
<br><input type="submit" name="f1sub">
</form>
<hr><hr>
<form name="staff" method="post" action="stf.php">
Enter Staff Name : <input type="text" name="stname">
<br>Enter employee Number : <input type="text" name="eno">
<br><input type="submit" name="f2sub">
</form>
</body>
</html>
Output:
Processing data from different forms can be done by checking name and value of submit button. When
control passes to script, the data submitted through post method is transferred to the script and script can
access that data. By checking value of submit button programmer can find out which form is submitted and
then process the data.
Example :
Code for HTML document
<html>
<body>
<form name="student" method="post" action="process.php">
Enter Student Name : <input type="text" name="sname">
<br>Enter Roll Number : <input type="text" name="rno">
<br><input type="submit" name="submit" value="Submit student data">
</form>
<hr><hr>
<form name="staff" method="post" action="process.php">
Enter Staff Name : <input type="text" name="stname">
<br>Enter employee Number : <input type="text" name="eno">
<br><input type="submit" name="submit" value="Submit staff data">
</form>
</body>
</html>
Output of script :
Welcome to student submit
Student Name = swara
Student Roll no = 1
Output of script :
Method 1 :By checking value of the button. We can give same name to all submit buttons and compare its
value in script before processing data.
Example :
Html code :
<html>
<body>
<form name="student" method="post" action="process1.php">
Enter Student Name : <input type="text" name="sname">
<br>Enter Roll Number : <input type="text" name="rno">
<br><input type="submit" name="submit" value="Display name">
<input type="submit" name="submit" value="Display roll no">
</form>
<hr><hr>
</body>
</html>
Process1.php code :
<html>
<body>
<?php
if($_POST['submit']=="Display name")
{
echo "Entered Name : ";
$sn=$_POST['sname'];
echo "<br>Student Name = $sn";
}
else if($_POST['submit']=="Display roll no")
{
echo "Entered roll no : ";
$r=$_POST['rno'];
echo "<br> Student Roll Number = $r";
}
else
{
echo "Invalid";
}
?>
</script>
</body>
</html>
Output :
Clicked on Display name : Output :
Entered Name :
Student Name = swara
Method 2 : By checking name of the button. We can give different names to button and compare it in script.
Example :
Code for form :-
<html>
<body>
<form name="student" method="post" action="process2.php">
Enter Student Name : <input type="text" name="sname">
<br>Enter Roll Number : <input type="text" name="rno">
<br><input type="submit" name="submit1" value="Display name">
<input type="submit" name="submit2" value="Display roll no">
</form>
<hr><hr>
</body>
</html>
?>
</script>
</body>
</html>