Web Programming Ktu Notes
Web Programming Ktu Notes
CHAPTER NO : 8
CHAPTER NAME : Server Side Programming
with PHP
LECTURE NO: 3
1
PHP Global Variables – Superglobals
Some predefined variables in PHP are "superglobals", which means
that they are always accessible, regardless of scope - and you can
access them from any function, class or file without having to do
anything special.
The PHP superglobal variables are:
$GLOBALS
$_SERVER
$_POST
$_GET
$_REQUEST
$_FILES
$_COOKIE
$_SESSION
PHP Form Handling
Super Global Variables
$_GET
To read form data send by HTTP method GET.
$_POST
To read form data send by HTTP method POST.
$_REQUEST
To read form data send by HTTP method both GET and POST.
Example
server.php
<?php
$name = $_GET['fname'];
echo $name;
?>
Example
server.php
<?php
$name = $_POST['fname'];
echo $name;
?>
How HTTP submits form data?
http://localhost/PhpProject1/register.php?user=arul&pass=cse&email=arul%40gmail.com
How HTTP submits form data?
http://localhost/PhpProject1/register.php?user=arul&pass=c
se&email=arul%40gmail.com
PHP Program
HTTP writes the form data in Super global variable $_GET automatically
<html> $_GET Example
<body>
</body>
</html>
<html> $_POST Example
<body>
if(isset($number))
echo "number variable is set";
else
echo "number variable is not set";
?>
unset() function
The unset() function unsets(delete) a variable.
<?php
$number = 10;
unset($number);
if(isset($number))
echo "number variable is set";
else
echo "number variable is not set";
?>
isset() method in form handling
<?php
$_SESSION['userid'] = "abc123";
$_SESSION['theme'] = "black";
$_SESSION['backgroundcolor'] = "blue";
?>
Example: Session
SessionFirst.php
?>
<a href=“SessionThird.php">Go To Second Page</a>
</body>
Example: Session
SessionThird.php
<body>
<?php
session_start();
echo "Welcome ".$_SESSION['userid'];
?>
<br>
<a href=“SessionFourth.php">Go To Third Page</a>
</body>
SessionFourth.php
<body>
<?php
session_start();
echo "Welcome ".$_SESSION['userid'];
?>
</body>
Session Tracking
PHP Cookies
A cookie is often used to identify a user.
A cookie is a small file(Maximum size: 4 KB) that the server
embeds on the user's computer.
Each time the same computer requests a page with a
browser, it will send the cookie too.
With PHP, you can both create and retrieve cookie values.
Create Cookies With PHP
A cookie is created with the setcookie() function.
Cookie values can be accessed by super global variable
$_COOKIE
Example- Cookies
CFirst.php
<body>
<form action=“CSecond.php" method="POST">
Name : <input type="text" name ="name"><br><br>
<input type="submit" value="submit">
</form>
</body>
CSecond.php
<body>
<?php
$user=$_POST['name'];
echo "Welcome $user <br>";
//writing to cookie
setcookie(“name”, $user);
?>
<a href=“CThird.php">Go To Second Page</a>
</body>
Example- Cookies
CThird.php
<body>
<?php
echo "Welcome ".$_COOKIE['name'];
?>
<br>
</body>
View Cookies
In Google Chrome, go to settings and search cookies
Click on “Cookies and other site data”
View Cookies
Click on See All Cookies
View Cookies
Search for localhost
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 be 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
Syntax
function functionName() {
code to be executed; Example:
} <?php
function writeMsg() {
echo "Hello world!";
}
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
?>
Passing Arguments by Reference
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;
?>
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;
echo $apple->get_name();
echo "<br>";
echo $banana->get_name();
?>
Constructor in Class
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 (__)!
Constructor in class
<?php
class Fruit {
public $name;
public $color;
function __construct($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
function __construct($name) {
$this->name = $name;
}
function __destruct() {
echo "The fruit is {$this->name}.";
}
}