Unit 2
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['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['SERVER_PORT'] Returns the port on the server machine being used by the
web server for communication (such as 80)
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>
$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>
<?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>
---------------------------------------------------------------------------------------------------------------