0% found this document useful (0 votes)
18 views8 pages

Unit 2

This document explains PHP superglobals, which are predefined variables available in every scope, including $_SERVER, $_GET, $_POST, $_FILES, $_REQUEST, $_COOKIE, and $_SESSION. It details how to use these variables for server information and form processing, including examples of self-processing forms and sticky forms that retain user input. The document emphasizes the importance of data validation and the ease of handling form submissions in PHP.

Uploaded by

shraddhavinod1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views8 pages

Unit 2

This document explains PHP superglobals, which are predefined variables available in every scope, including $_SERVER, $_GET, $_POST, $_FILES, $_REQUEST, $_COOKIE, and $_SESSION. It details how to use these variables for server information and form processing, including examples of self-processing forms and sticky forms that retain user input. The document emphasizes the importance of data validation and the ease of handling form submissions in PHP.

Uploaded by

shraddhavinod1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Unit 2

Web Techniques
Web Variables-:
Php provides a large number of predefined variables. Php provides an additional set of
predefined arrays containing variables from the web server the environment and user input.
These new arrays are called superglobals. All the following variables are automatically
available in every scope.

Superglobals:-
1) $GLOBAL:-
Contains a reference to every variable which is currently available within
the global scope of the script.

2) $_SERVER:-
This is an array containing information such as header, paths and script
location.
The entries in this array are created by the web server.

3) $_GET-:
An associative array of variables passed to the current script via the http GET
method.

4) $_POST:-
An associative array of variables passed to the current script via the http
POST method.
5) $_FILES:-
An associative array of items uploaded to the current script via the HTTP
post method.
6) $_REQUEST -:
An associative array consisting of the contents of $_GET, $_POST and
$_COOKIE.
7) $_COOKIE-:
An associative array of variables passed to the current script via HTTP
cookies.
8) $_SESSION-:
An associative array containing session variables available to the current
script.

Example 1-:

<?php
$x=40;
$y=50;

function add()
{
$GLOBALS['z']=$GLOBALS['x']+$GLOBALS['y'];
}
add();
echo "Addition is :" .$z;
?>

OUTPUT=
Addition is : 90

-------------------------------------------------------------------------------------------------------

Server Variables-:
$_SERVER is an array containing information such as headers , path and
script location. The entries in this array are created by the web server.
1) $_SERVER[‘PHP_SELF’]-:
The filename of the currently executing script relative to the document
root.
2) $_SERVER[‘arg’]-:
Array of arguments passed to the script. When the script is run on the
command line this given c-style access to the command line parameters.

3) $_SERVER[‘SERVER_NAME’]-:
The name of the server host under which the current script is executing. If
the script is running on a virtual host, this will be the value defined for that virtual
host.
4) $_SERVER[‘SERVER_SOFTWARE’]-:
Server identification string given in the headers when responding to
requests.
5) $_SERVER[‘REQUEST_METHOD’]-:
Which request method was used to access the page i.e. ‘GET’, ‘HEAD’
,’POST’ ,
‘PUT’.
Example-:
<?php
echo $_SERVER['PHP_SELF'];
echo "<br/>";

echo $_SERVER['SERVER_NAME'];
echo "<br/>";

echo $_SERVER['HTTP_HOST'];
echo "<br/>";

echo $_SERVER['SERVER_SOFTWARE'];
echo "<br/>";

echo $_SERVER['REQUEST_METHOD'];
echo "<br/>";

echo $_SERVER['HTTP_USER_AGENT'];
echo "<br/>";

echo $_SERVER['SCRIPT_NAME'];
?>

Server Information-:
The $_SERVER array contains a lot of useful information about headers, paths, and script
locations from the web server. Most of this information comes from the environmental
variables required in the CGI (common gateway interface) specification.
Entries in $_SERVER-:
Following is a complete list of the entries in $_SERVER that comes from CGI.
Element/Code Description

$_SERVER['PHP_SELF'] Returns the filename of the currently executing script

$_SERVER['SERVER_ADDR'] Returns the IP address of the host server

$_SERVER['SERVER_NAME'] Returns the name of the host server (such as


www.w3schools.com)

$_SERVER['SERVER_SOFTWARE'] Returns the server identification string (such as


Apache/2.2.24)

$_SERVER['REQUEST_METHOD'] Returns the request method used to access the page (such as
POST)

$_SERVER['REMOTE_ADDR'] Returns the IP address 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_FILENAME'] Returns the absolute pathname of the currently executing


script

$_SERVER['SERVER_PORT'] Returns the port on the server machine being used by the
web server for communication (such as 80)

$_SERVER['SCRIPT_NAME'] Returns the path of the current script

Self Processing Form -:


HTML forms are used to send the user information to the server and returns the result back
to the browser. For example, if you want to get the details of visitors to your website, and
send them good thoughts, you can collect the user information by means of form
processing. Then, the information can be validated either at the client-side or on the server-
side. The final result is sent to the client through the respective web browser.
Forms can be submitted to the web page itself using PHP. The main purpose of submitting
forms to self is for data validation. Data validation means checking for the required data to be
entered in the form fields.
PHP_SELF is a variable that returns the current script being executed. You can use this
variable in the action field of the form. The action field of the form instructs where to submit
the form data when the user presses the submit button. Most PHP pages maintain data
validation on the same page as the form itself.
An advantage of doing this is in case of a change in the website structure, the data validation
code for the form, and the form remain together.
Sometimes, you want to include both form and logic for handling form submission in a single
PHP file. This form is often referred to as a self-processing form. To create a self-processing
form, you can use the $_SERVER['REQUEST_METHOD'] that returns the request method
e.g., GET or POST.
It is easy to process forms with php as the form parameters are available in the $_GET and
$_POST arrays. There are many techniques for working with from which are described as
follows-
Attributes of Form Tag:

Attribute Description

name or id It specifies the name of the form and is used to identify individual forms.

It specifies the location to which the form data has to be sent when the form
action
is submitted.

It specifies the HTTP method that is to be used when the form is submitted.
method The possible values are get and post. If get method is used, the form data
are visible to the users in the url. Default HTTP method is get.

Example-:
<html>
<head>
<title>Self Processing form </title>
</head>
<body>

<h2> SIMPLE INTREST CALCULATION </h2>


<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$principal=$_POST['principal'];
$rate=$_POST['rate'];
$time=$_POST['time'];

$simple_intrest=($principal*$rate*$time)/100;
print "SIMPLE INTREST =" .$simple_intrest;
}
?>
<table border="1">
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
<tr>
<td>Principal</td>
<td> <input type="text" name="principal"></td>
</tr>

<tr>
<td>Rate</td>
<td> <input type="text" name="rate"></td>
</tr>

<tr>
<td>Time</td>
<td><input type="text" name="time"></td>
</tr>

<tr>
<td><input type="submit" name="submit" value="SUBMIT"></td>
</tr>
</form>
</table>
</html>
Sticky Forms-:
A sticky form is simply a standard HTML form that remembers how you filled it out. This is
a particularly nice feature for end users, especially if you are requiring them to resubmit a
form.
A sticky form in PHP is a web form that retains and displays the user's previously entered
data, making it "stick" to the form fields even after a form submission. This is often done to
enhance the user experience by preventing users from re-entering all the form data in case
there are errors or missing information.
<html>
<head>
<title>Self Processing form</title>
</head>
<body>

<h2>SIMPLE INTEREST CALCULATION</h2>

<?php
$principal="";
$rate="";
$time="";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$principal = $_POST['principal'];
$rate = $_POST['rate'];
$time = $_POST['time'];

$simple_interest = $principal*$rate/100*$time;
echo "SIMPLE INTEREST = " . $simple_interest;
}
?>
<table>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
<tr>
<td>Principal</td>
<td><input type="text" name="principal" value="<?php echo $principal ?>"></td>
</tr>

<tr>
<td>Rate</td>
<td><input type="text" name="rate" value="<?php echo $rate ?>"></td>
</tr>

<tr>
<td>Time</td>
<td><input type="text" name="time" value="<?php echo $time ?>"></td>
</tr>

<tr>
<td><input type="submit" name="submit" value="SUBMIT"></td>
</tr>

</form>
</table>

</body>
</html>
---------------------------------------------------------------------------------------------------------------

You might also like