0% found this document useful (0 votes)
17 views39 pages

Module3 Php Web Technology NOTES

The document provides an overview of server-side scripting using PHP, explaining its functionality, the differences between client-side and server-side scripting, and the various server software types including web, application, and database servers. It introduces XAMPP as a platform for setting up a local server environment for PHP development, detailing the installation process and the use of the Apache Netbeans IDE for creating PHP applications. Additionally, it covers PHP language elements, including variables, data types, operators, and how to incorporate PHP within HTML.

Uploaded by

ABHINAV JOY C
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)
17 views39 pages

Module3 Php Web Technology NOTES

The document provides an overview of server-side scripting using PHP, explaining its functionality, the differences between client-side and server-side scripting, and the various server software types including web, application, and database servers. It introduces XAMPP as a platform for setting up a local server environment for PHP development, detailing the installation process and the use of the Apache Netbeans IDE for creating PHP applications. Additionally, it covers PHP language elements, including variables, data types, operators, and how to incorporate PHP within HTML.

Uploaded by

ABHINAV JOY C
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/ 39

279

CHAPTER 10

SERVER SIDE SCRIPTING USING PHP

10.1. Server Side Scripting

Server-side scripting is a technique used in web development which involves employing


scripts on a web server which produce a response customised for each user's (client's) request to
the website. The alternative is for the web server itself to deliver a static web page. Scripts can be
written in any of a number of server-side scripting languages that are available.

The difference between the client side scripting and server side scripting is, the client side
scripting runs on a browser but server side scripting runs on a server softwares. There are a number
of server-side scripting languages available, such as PHP, JSP, Servlets, Node JS, and ASP.Net.
etc.

Server Softwares

Server software is a type of software that is designed to be used, operated and managed on
a computing server. It provides and facilitates the harnessing of underlying server computing
power for use with an array of high-end computing services and functions.
Server software is primarily built to interact with a server’s hardware infrastructure,
including the processor, memory, storage, input/output (I/O) and other communication ports.
Depending on the type or usage of the server, server software may be classified into various forms,
such as the following:
• Web Server software
• Application Server software
• Database server software

Web Server software

A web server is a system that delivers content or services to end users over the internet. A
web server consists of a physical server, server operating system (OS) and software used to
facilitate HTTP communication. A web server is also known as an internet server. The most simple
definition is that a web server runs a website by returning HTML files over an HTTP connection.
A better definition might be that a web server is any internet server that responds to HTTP requests
to deliver content and services. The following are the examples of web servers.
• Apache WebServer – (XAMPP) – PHP Support
• Microsoft Internet Information Server(IIS) – ASP Support
• Apache Tomcat Web Server – JSP Support
• Oracle iplanet Web Server
• Lighttpd
• IBM HTTP Server

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


280

Application Server software

Application servers provide a framework to build and deploy web applications and offer a
variety of services when running such applications. These services include security, transactions,
clustering for increased performance, and diagnostic capabilities, among others. These products
can also include servers that strictly host web applications.
Application servers are used by software developers who are trying to quickly build
applications and have them supported by the server environment in which they are deployed. These
products allow developers to focus on the application itself and spend less time focusing on outside
issues, such as performance or security. Application servers can run in conjunction with relational
databases and a variety of web frameworks. The following are the examples for Application
Servers.
• IBM WebSphere Application Server
• Oracle WebLogic
• Apache Tomcat
• Apache Geronimo
• appserver.io

Database server software


A database server is a server which uses a database application that provides database
services to other computer programs or to computers, as defined by the client–server model.
Database management systems (DBMSs) frequently provide database-server functionality, and
some database management systems (such as MySQL) rely exclusively on the client–server model
for database access (while others e.g. SQLite are meant for using as an embedded database).

10.2. Configuration of Apache, MySQL and PHP

What is XAMPP?
XAMPP start for Cross-Platform, PHP, Mysql, Perl, Apache. XAMPP is a very lightweight
and simple Apache distribution, So you can use it because this is available any platform and easily
as Windows, Linux, or Mac.

XAMPP has four main components.:


• Apache: is a web server process that converts PHP files into simple HTML and sends it to
the web browser of a user. Apache is the most usable and famous server today, with more
than 50% website using it.
• MySql: means database almost all websites that are dynamic needs to store data for that
pupose we uses a database. MySQL is most famous database management system and an
open source.
• PHP: is a server-side scripting language that is used by world 70% websites as Facebook
is also an example.
• Perl: is a family of two high-level, general-purpose, interpreted, dynamic programming
languages.

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


281

This is the first step for setup local hosts for server-side scripting. First go to google and type
XAMPP download and click the first link and download XAMPP and install. This is free and
easily install.
https://www.apachefriends.org/index.html

After that install you can look like as below image then you go to click start button of Apache and
MySql for start XAMPP server

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


282

Click on the Apache “Admin” button in the control panel

You will be getting the following page! Now, we can ensure that the PHP has been installed
successfully.

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


283

Also, Click on the MySQL “Admin” button in the control panel

The following page opens, which is a MySQL Database portal!. Now, you can ensure that the
MySQL has been successfully installed.

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


284

Next step is to install IDE for PHP application development, the Apache Netbeans IDE provides
a free Editor with HTML, CSS, JavaScript and PHP support. The procedure is as follows.

Step 1: Install JDK software from the following site.


https://www.oracle.com/java/technologies/javase-jdk16-downloads.html

Step 2: After installing JDK software, install the netbeans IDE from the following site.
https://netbeans.apache.org/download/nb120/nb120.html

Step 3: After installation of Netbeans IDE, open the software.

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


285

Step 4: Create a New Project

Step 5: Select “PHP” from categories and “PHP Application” from Projects.

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


286

Step 6: Give the project name and location and click “Finish”

Step 7:Now, the PHP project is created with default index.php page.

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


287

Step 8: Test the php Application by Right click and Select “Run File”

Step 9: Now, the browser automatically opens the PHP application and show the output as given
below.

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


288

10.3. PHP – language elements

⚫ PHP: Hypertext Preprocessor.


⚫ PHP is a server-side scripting language, and a powerful tool for making dynamic and
interactive Web pages.
⚫ It is a program resides in a web server and respond to client requests.
⚫ It was originally created by Rasmus Lerdorf in 1994.
⚫ He developed Perl/CGI Script Toolset, called as Personal Home Page(PHP).
⚫ Later, renamed as Hypertext Preprocessor.

Dynamic Client/Server Request/Response Sequence

PHP server side scripting language allows us to create dynamic Client and Server request
respnse model as given below. Client request a page on the server which contains the PHP script.
The PHP script is processed including if there is any datbase access and final response page will
be created and return to the client browser.

What is a PHP File ?


⚫ PHP files may contain text, HTML, CSS, JavaScript, and PHP code
⚫ PHP code are executed on the server, and the result is returned to the browser as a plain
HTML

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


289

⚫ PHP files have extension ".php"

Incorporating PHP Within HTML


The PHP code script can be embedded inside the HTML using the tag called <?php and ?> as
given below.

<?php
statements…
…………
…………
?>
Display Data in a web browser from PHP Script
• With PHP, there are two basic ways to get output: echo and print.

• The PHP echo Statement


<?php
echo "Welcome to PHP Programming!!"
?>

• The print statement


<?php
print "Welcome to PHP Programming!!"
?>

Creating Comments in PHP Program


A comment in PHP code is a line that is not executed as a part of the program. Its only purpose is
to be read by someone who is looking at the code.

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


290

Single Line Comments: Can be created using // or #

<!DOCTYPE html>
<html>
<body>

<?php
// This is a single-line comment

# This is also a single-line comment


?>

</body>
</html>

Multiline Comments

<!DOCTYPE html>
<html>
<body>

<?php
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>

</body>
</html>

10.3.1. Creating Variables in PHP

⚫ In PHP, a variable must starts with the $ sign, followed by the name of the variable:
⚫ It is a faster memory access operator like pointer in C programming.

<?php
$name = "John";
$company = 'Infosys';
$number = 10;
$pi = 3.14;
?>

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


291

After the execution of the statements above, the variable $name will hold the value “John”, the
variable $company will hold the value “Infosys”, and the variable $number will hold the value
10 and the variable $pi will hold the value “3.14”.

Difference between ‘ ‘ and “ “ for string data.


⚫ String data is most commonly used type which can be represented in two ways.
⚫ Using single quotes - ‘ ‘
⚫ It creates un interpretable text, where variables will not be resolved.
⚫ Using double quotes – “ “
⚫ It creates interpretable text where variables value will be resolved.

<?php
$price = 1000;
echo "The product price is $price";
echo 'The product price is $price';
?>

String concatenation
In PHP, the dot(.) operator can be used to concatenate the strings as well as other data
values.

<?php
$name = 'John';
$number = 10;
$pi = 3.14;

echo "Name: ". $name;


echo "Number: ". $number;
echo "PI Value: ". $pi;
?>

PHP Data Types

⚫ Variables can store data of different types, and different data types can do different things.
Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111
292

⚫ PHP supports the following data types:


⚫ String
⚫ Integer
⚫ Float (floating point numbers - also called double)
⚫ Boolean
⚫ Array
⚫ Object
⚫ NULL

• To find the type of data


o gettype() function used to know the type of variable

Default values of uninitialized variables


⚫ Uninitialized variables have a default value of their type depending on the context in which
they are used.
⚫ By default, you will be getting an empty variable data with type NULL
⚫ Also, you will be getting warning message

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


293

Type Casting

Typecasting is the explicit conversion of data type because user explicitly defines the
data type in which he wants to cast. For example the following code divide 10 by 6 , result will
be 1.666 .. float value.

To convert this float to a Integer value, need to use type casting operator (data type)

Creating constant
⚫ To create a constant, use the define() function.

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


294

10.3.2. PHP Operators

Operators are used to perform operations on variables and values. PHP divides the
operators in the following groups:

• Arithmetic operators
• Assignment operators
• Comparison operators
• Increment/Decrement operators
• Logical operators
• String operators
• Array operators
• Conditional assignment operators

PHP Arithmetic Operators

The PHP arithmetic operators are used with numeric values to perform common arithmetical
operations, such as addition, subtraction, multiplication etc.

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


295

Example 1: Exponentiation Operator

<?php
$x = 5;
$y = 3;
$z = $x ** $y;
echo $z;
?>

PHP Assignment Operators

The PHP assignment operators are used with numeric values to write a value to a variable.
The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of
the assignment expression on the right.

Example: Exponentiation Operator

<?php
$x = 5;
$x += 2; // $x = $x + 2
echo $x;
?>
Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111
296

PHP Comparison Operators

The PHP comparison operators are used to compare two values (number or string):

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


297

PHP Increment / Decrement Operators


The PHP increment operators are used to increment a variable's value. The PHP decrement
operators are used to decrement a variable's value.

Example:

<?php
$x = 5;
$x++; //$x increments by 1
++$x; //$x increments by 1
echo $x;
?>

PHP Logical Operators


The PHP logical operators are used to combine conditional statements.

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


298

<?php
$a = 10;
$b = 4;
$c = 15;
echo $a>$b and $b<$c;
?>

PHP String Operators

PHP has two operators that are specially designed for strings.

Example 1: Concatenation of two or more strings - dot operator (.) (.=)

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


299

Example 2: Concatenation using comma (,) operator

PHP Array Operators

The PHP array operators are used to compare arrays.

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


300

PHP Conditional Assignment Operators

The PHP conditional assignment operators are used to set a value depending on conditions:

Example:

<?php
$a = 10;
$b = 4;
$result = $a>$b ? $a : $c;
echo $result;
?>

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


301

10.3.3. PHP Control Statements

PHP supports the following control statements

• Conditional Statements
• Loop Statements

Conditional Statments

In PHP we have the following conditional statements:

• if statement - executes some code if one condition is true


• if...else statement - executes some code if a condition is true and another code if that
condition is false
• if...elseif...else statement - executes different codes for more than two conditions
• switch statement - selects one of many blocks of code to be executed

The if Statement

The if statement executes some code if one condition is true.

Syntax
if (condition) {
code to be executed if condition is true;
}

Example: -

<?php
$t = 21;
if ($t < "20") {
echo "Have a good day!";
}
?>

The if...else Statement


The if...else statement executes some code if a condition is true and another code if that
condition is false.

Syntax
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


302

<?php
$t = 10;
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>

The if...elseif...else Statement

The if...elseif...else statement executes different codes for more than two conditions.

Syntax
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if first condition is false and this condition is true;
} else {
code to be executed if all conditions are false;
}

<?php
$t = 9;

if ($t < "10") {


echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>

The PHP switch Statement

Use the switch statement to select one of many blocks of code to be executed.

Syntax
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111
303

case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}

<?php
$favcolor = "red";

switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>

PHP Loops
Often when you write code, you want the same block of code to run over and over again a
certain number of times. So, instead of adding several almost equal code-lines in a script, we can
use loops.

In PHP, we have the following loop types:


• while - loops through a block of code as long as the specified condition is true
• do...while - loops through a block of code once, and then repeats the loop as long as the
specified condition is true
• for - loops through a block of code a specified number of times
• foreach - loops through a block of code for each element in an array

The PHP while Loop

The while loop executes a block of code as long as the specified condition is true.

Syntax
while (condition is true) {
code to be executed;
}

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


304

<?php
$x = 1;

while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>

do...while Loop

The do...while loop will always execute the block of code once, it will then check the
condition, and repeat the loop while the specified condition is true.

Syntax
do {
code to be executed;
} while (condition is true);

<?php
$x = 1;

do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>

The PHP for Loop

The for loop is used when you know in advance how many times the script should run.

Syntax
for (init counter; test counter; increment counter) {
code to be executed for each iteration;
}

<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>

10.3.4. PHP Arrays

An array stores multiple values in one single variable:

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


305

⚫ Create an Array in PHP


⚫ In PHP, the array() function is used to create an array.
⚫ In PHP, there are three types of arrays:
⚫ Indexed arrays - Arrays with a numeric index
⚫ Associative arrays - Arrays with named keys

Indexed arrays

Empty Array
$fruits = array();
1. count() function used to get the length of the array.
2. for loop can be used to run through the array elements.

Accessing Array Values using For Loop

Iterate the PHP Arrays using foreach

⚫ The foreach loop - Loops through a block of code for each element in an array.

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


306

PHP Associative Arrays

⚫ Associative arrays are arrays that use named keys that you assign to them.

More Recommended way of creating Array

To run through an associative array.

To loop through and print all the values of an associative array, you could use a foreach loop.

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


307

To access only keys from associative array

10.4. PHP Functions

Besides the built-in PHP functions, it is possible to create your own functions.
• A function is a block of statements that can be used repeatedly in a program.
• A function will not execute automatically when a page loads.
• A function will be executed by a call to the function.
• In PHP functions are created using function keyword.

Create a User Defined Function in PHP

A user-defined function declaration starts with the word function:

Syntax
function functionName() {
code to be executed;
}

Example:

<?php
function writeMsg() {
echo "Hello world!";
}

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


308

writeMsg(); // call the function


?>

PHP Function Arguments

Information can be passed to functions through arguments. An argument is just like a variable.
Arguments are specified after the function name, inside the parentheses. You can add as many
arguments as you want, just separate them with a comma.

<?php
function add($a,$b) {
echo $a + $b;
}

add(4,5);
add(11,23);
?>

Default Argument Value

The function arguments can have a default value

Example:
<?php
function add($a=0,$b=0) {
echo $a + $b;
}

add(4); //Result = 4
add(2,2); //Result = 4
?>

PHP Functions - Returning values

To let a function, return a value, use the return statement:

<?php
function add($a,$b) {
return $a + $b;
}

$result1=add(4,1); //Result = 5
$result2=add(2,2); //Result = 4
?>

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


309

Passing Arguments by Reference


In PHP, arguments are usually passed by value, which means that a copy of the value is
used in the function and the variable that was passed into the function cannot be changed.
When a function argument is passed by reference, changes to the argument also change the
variable that was passed in. To turn a function argument into a reference, the & operator is used:

<?php
function add_five(&$value) {
$value += 5;
}

$num = 2;
add_five($num);
echo $num;
?>

10.5. Classes and objects

⚫ PHP is an object-oriented programming language, which means that you can create objects,
which can contain variables and functions. When creating a program to use objects, you
need to design a composite of data and code called a class.
⚫ Each new object based on this class is called an instance (or occurrence) of that class.
⚫ The data associated with an object are called its properties;
⚫ the functions it uses are called methods.
⚫ In defining a class, you supply the names of its properties and the code or its methods.

Declaring a Class

⚫ Before you can use an object, you must define a class with the class keyword.
⚫ Class definitions contain the class name (which is case-sensitive), its properties, and its
methods.

<?php
class Fruit {
// Properties
public $name;
public $color;

// Methods
function set_name($name) {

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


310

$this->name = $name;
}
function get_name() {
return $this->name;
}
}

$apple = new Fruit();


$banana = new Fruit();
$apple->set_name('Apple');
$banana->set_name('Banana');

echo $apple->get_name();
echo "<br>";
echo $banana->get_name();
?>

The __construct Function

A constructor allows you to initialize an object's properties upon creation of the object. If you
create a __construct() function, PHP will automatically call this function when you create an object
from a class.

Notice that the construct function starts with two underscores (__)!

<?php
class Fruit {
public $name;
public $color;

function __construct($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}

$apple = new Fruit("Apple");


echo $apple->get_name();
?>

The __destruct Function

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


311

A destructor is called when the object is destructed or the script is stopped or exited. If you create
a __destruct() function, PHP will automatically call this function at the end of the script.

Notice that the destruct function starts with two underscores (__)!

<?php
class Fruit {
public $name;
public $color;

function __construct($name) {
$this->name = $name;
}
function __destruct() {
echo "The fruit is {$this->name}.";
}
}

$apple = new Fruit("Apple");


?>

Access Modifiers

Properties and methods can have access modifiers which control where they can be accessed.
There are three access modifiers:
• public - the property or method can be accessed from everywhere. This is default
• protected - the property or method can be accessed within the class and by classes derived
from that class
• private - the property or method can ONLY be accessed within the class

Example

<?php
class Fruit {
public $name;
protected $color;
private $weight;
}

$mango = new Fruit();


$mango->name = 'Mango'; // OK
$mango->color = 'Yellow'; // ERROR
$mango->weight = '300'; // ERROR
?>

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


312

Inheritance

Inheritance in OOP = When a class derives from another class.

The child class will inherit all the public and protected properties and methods from the parent
class. In addition, it can have its own properties and methods.

An inherited class is defined by using the extends keyword.

<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}

// Strawberry is inherited from Fruit


class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
}
}
$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message();
$strawberry->intro();
?>

10.6. PHP Super Global Variables

⚫ Several predefined variables in PHP are "superglobals", which means that they are always
accessible, regardless of scope.
⚫ These variables are associative arrays by default. It maintains data as Name– Value pairs.
⚫ Few PHP superglobal variables are:
⚫ $_POST
⚫ $_GET
⚫ $_REQUEST
⚫ $_SERVER
⚫ $_COOKIE
⚫ $_SESSION
⚫ $GLOBALS

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


313

⚫ $_FILES

$GLOBALS

$GLOBALS is a PHP super global variable which is used to access global variables from anywhere
in the PHP script

<?php
$x = 75;
$y = 25;

function addition() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}

addition();
echo $z;
?>

$_SERVER

$_SERVER is a PHP super global variable which holds information about headers, paths, and
script locations.

<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['HTTP_REFERER'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>

$_SESSION

$_SESSION is a PHP super global variable which is used for Server Side Session Tracking.

$_COOKIES

$_COOKIES is a PHP super global variable which is used for Client Side Session Tracking.

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


314

$_FILES
$_FILES is a PHP super global variable which is used for File Uploading.

$_GET
$_GET is a PHP super global variable which is used for receiving data from HTML form that is
send by HTML Form method = GET

$_POST
$_POST is a PHP super global variable which is used for receiving data from HTML form that is
send by HTML Form method = POST

$_REQUEST
$_R EQUEST is a PHP super global variable which is used for receiving data from HTML form
that is send by HTML Form method = GET / method=POST

10.7. PHP Form Data Handling

The PHP superglobals $_GET, $_POST and $_REQUEST is used to collect form-data.

Super Global Variables used for Form handling


• $_GET
o To read form data send by HTTP method GET.
• $_POST
o To read form data send by HTTP method POST.
• $_REQUEST
o To read form data send by HTTP method both GET and POST

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


315

Reading HTML Form Data using $_POST

The example below displays a simple HTML form with two input fields and a submit button:
<html>
<body>

<form action="welcome.php" method="post">


Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>

When the user fills out the form above and clicks the submit button, the form data is sent for
processing to a PHP file named "welcome.php". The form data is sent with the HTTP POST
method.

To display the submitted data you could simply echo all the variables. The "welcome.php" looks
like this:

<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>


Your email address is: <?php echo $_POST["email"]; ?>

</body>
</html>

Reading HTML Form Data using $_GET

<html>
<body>

<form action="welcome_get.php" method="get">


Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


316

and "welcome_get.php" looks like this:

<html>
<body>

Welcome <?php echo $_GET["name"]; ?><br>


Your email address is: <?php echo $_GET["email"]; ?>

</body>
</html>

Reading HTML Form Data using $_REQUEST

It is used to read form data send by HTTP method both GET and POST

<html>
<body>

<form action="welcome_get.php" method="get">


Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>

and "welcome_get.php" looks like this:

<html>
<body>

Welcome <?php echo $_REQUEST["name"]; ?><br>


Your email address is: <?php echo $_REQUEST["email"]; ?>

</body>
</html>

Samething will work for method=POST.

10.8. PHP Session Handling

A session is a way to store information (in variables) to be used across multiple pages. A
session is started with the session_start() function.

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111


317

Session variables are set with the PHP global variable: $_SESSION.

Example:

The following example illustrate the usage of $_SESSION super global variable for tracking the
user id in all pages of the application.

Home.php [This PHP program collect the User Name using HTML form which will be
submitted to first.php.

<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
</head>
<body>
<form action="first.php" method="POST">
Name : <input type="text" name ="name"><br><br>
<input type="submit" value="submit">
</form>
</body>
</html>

In First.php program, the username is read by $_POST variable and it will be stored in a
Session variable using $_SESSION. In order to use this global variabe, the session_start()
method must be called.

<!DOCTYPE html>
<html>
<head>

Manifold Institute of Technical Education, M.G.Road, Thrissur Ph: 0487-2382342, 8590079111

You might also like