0% found this document useful (0 votes)
55 views

Web Programming Ktu Notes

Ktu notes S7 sem

Uploaded by

gnanjitha
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)
55 views

Web Programming Ktu Notes

Ktu notes S7 sem

Uploaded by

gnanjitha
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/ 45

COMPUTER SCIENCE

SUBJECT NAME : WEB TECHNOLOGY

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

<form method="get" action=“server.php">


Name: <input type="text" name="fname"><br>
<input type="submit">
</form>

server.php

<?php
$name = $_GET['fname'];
echo $name;
?>
Example

<form method=“post" action=“server.php">


Name: <input type="text" name="fname"><br>
<input type="submit">
</form>

server.php

<?php
$name = $_POST['fname'];
echo $name;
?>
How HTTP submits form data?

HTTP GET Request

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

$_GET is an associative array

HTTP writes the form data in Super global variable $_GET automatically
<html> $_GET Example
<body>

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


Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form> <html>
<body>
</body>
</html> Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?>

</body>
</html>
<html> $_POST Example
<body>

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


Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit"> server.php
</form> <html>
<body>
</body> Welcome <?php echo $_POST["name"]; ?><br>
</html> Your email address is: <?php echo $_POST["email"];
?>
</body>
</html>
<html> $_REQUEST Example
<body>

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


Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit"> server.php
</form>
<html>
<body>
</body>
Welcome <?php echo $_REQUEST["name"]; ?><br>
</html>
Your email address is: <?php echo $_REQUEST["email"]; ?>
</body>
</html>
<form action="server1.php" method="post"> Reading Other Input Elements
Name: <input type="text" name="name"> <br><br>
Gender: <input type="radio" name="gender" value="Male"> Male
<input type="radio" name="gender" value="Female"> Female
<br><br>
Select your country: <select name="country">
<option>India</option>
<option>USA</option>
<option>Pakistan</option>
While creating multiple checkboxes add [] at the end of
<option>China</option>
name attribute e.g. skills[]. Here, [] denotes an Array.
<option>Singapore</option>
</select>
<br><br>
Select Skills:
<input type="checkbox" name="skills[]" value="Java">Java
<input type="checkbox" name="skills[]" value="JavaScript">JavaScript
<input type="checkbox" name="skills[]" value="HTML">HTML
<input type="checkbox" name="skills[]" value="PHP">PHP
<br><br>
<input type="submit" value="Submit">
</form>
<html>
<head> server1.php
<meta charset="UTF-8">
<title></title>
</head>
<body>
<p>Name : <?php echo $_POST['name'] ?></p>
<p>Gender: <?php echo $_POST['gender'] ?> </p>
<p>Country: <?php echo $_POST['country'] ?> </p>
<!-- reading skills -->
<p>
Skills:
<?php
$skillarr = $_POST['skills'];
foreach($skillarr as $data){
echo $data . ",";
}
?>
</p>
</body>
</html>
Output
isset() function in PHP
 The isset() function is an inbuilt function in PHP which checks
whether a variable is set and is not NULL
<?php
$number = 10;

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

 isset() method can be used to check the given form


data is set or not.
Another Usage of isset()
Another Usage of isset()
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.
<?php
session_start();
?>

<?php
$_SESSION['userid'] = "abc123";
$_SESSION['theme'] = "black";
$_SESSION['backgroundcolor'] = "blue";
?>
Example: Session
SessionFirst.php

<form action=“SessionSecond" method="POST">


Name : <input type="text" name ="name"><br><br>
<input type="submit" value="submit">
</form>
SessionSecond.php
<body>
<?php
$user=$_POST['name'];
echo "Welcome $user <br>";
session_start();
//writing to session variable
$_SESSION['userid'] = $user;

?>
<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!";
}

writeMsg(); // call the function


?>
PHP Function Arguments
 Information can be passed to functions through arguments.
<?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
?>
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;

 the functions it uses are called 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; Example Class
// Methods
function set_name($name) {
$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();
?>
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;
}
}

$apple = new Fruit("Apple");


echo $apple->get_name();
?>
The __destruct Function
 A destructor is called when the object is deleted or the script
is stopped or exited.
<?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: Access Modifiers
<?php
class Fruit {
public $name;
protected $color;
private $weight;
}

$mango = new Fruit();


$mango->name = 'Mango'; // OK
$mango->color = 'Yellow'; // ERROR
$mango->weight = '300'; // ERROR
?>
Inheritance
 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 {
Example
public $name, $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}. <br>";
}
}

// Strawberry is inherited from Fruit


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

You might also like