WT Unit-V (C23)
WT Unit-V (C23)
Unit-V
(5. Web servers and Server side scripting using PHP)
5.1 Web servers:
5.1.1 Understand the architecture of a Web server.
5.1.2 List various web servers.
5.1.3 Illustrate the various HTTP request types and their difference.
5.1.4 Compare the properties of IIS and Apache.
5.2 Fundamentals of PHP
5.2.1 State the importance of PHP
5.2.2 Explain how to combine HTML and PHP.
5.2.3 Explain how to access HTML, PHP documents from web servers.
5.3 Data types, Variables and Constants
5.3.1 List Data types
5.3.2 Explain Data types with examples
5.3.3 Explain how to declare Variables and Constants.
5.4 List and explain string manipulation functions.
5.5 Understand Arrays
5.5.1 Explain types of arrays.
5.5.2 Design small programs using arrays.
5.6 Explain form handling in PHP
5.6.1 Access elements of form using $_GET,$_POST
5.7 Know how to access My SQL Database
5.7.1 List and explain My SQL database functions in PHP.
5.7.2 Explain the steps of connecting to a Database.
5.7.3 Know about retrieving data from a table.
5.7.4 Know about inserting data into a table.
5.7.5 Know about updating the data in a table.
5.7.6 Know about deleting data from a table.
5.7.7 Design some simple programs to insert, delete, update and retrieve data from
database.
5.8 Cookies
5.8.1 Define Cookie.
5.8.2 Know how to create and delete a cookie.
5.8.3 Know the purpose of cookie.
5.9 Sessions
5.9.1 Define Session
5.9.2 Understand how to create a session.
5.9.3 Know how to destroy a session.
5.9.4 Know the purpose of session.
5.9.5 Differentiate Sessions and Cookies.
5.10 Passing data from one web page to other webpage using query string.
1. Understand the architecture of a Web server
➢ Web server architecture is the logical layout (or) design of a web server
➢ It defines the architectural layout and components of a web server
➢ Web server architecture consists of following parameters:
• Physical capacity of the server
• Performance and quality of service
• Application tiers
• Platform supported (.net, LAMP…..)
• Operating system (Windows, Linux, solaris……)
• Network / internet connectivity
Example:
<!DOCTYPE html>
<html>
<body>
<center>
<h2>
<?php
echo "This is PHP code inside html"
?>
</h2>
</center>
</body>
</html>
Output:
8. Explain how to access HTML, PHP documents from web servers
--- > (Refer Q.No: 19 - Explain the steps of connecting to a Database)
Output:
Total = 30
Int(30)
2. Float (Double): Can hold numbers containing fractional or decimal parts including positive and negative
numbers.
<?php
$a = 10.40;
$b = 20.50;
$c = $a + $b;
echo “Total = “ . $c . “\n”;
var_dump($c);
?>
Output:
Total = 30.90
float(30.90)
3. String: Hold letters or any alphabets, even numbers are included. These are written within double quotes
during declaration. The strings can also be written within single quotes, but they will be treated differently
while printing variables.
<?php
$name = “Hasini”;
echo “my name is $name \n“;
var_dump($name);
?>
Output:
my name is Hasini
string(6) “Hasini”
4. Boolean: Boolean data types are used in conditional testing. Hold only two values, either TRUE(1) or
FALSE(0). Successful events will return true and unsuccessful events return false.
<?php
if (TRUE)
echo “This condition is True”;
else
echo “This condition is False”;
?>
Output:
This condition is True
1. Array: Array is a data type that can store multiple values of the same data type.
<?php
$intArray = array(10,20,30);
echo “First Element = $intArray[0] \n”;
echo “Second Element = $intArray[1] \n”;
echo “Third Element = $intArray[2] \n”;
?>
Output:
First Element = 10
Second Element = 20
Third Element = 30
2. Objects: Objects are defined as instances of user-defined classes that can hold both values and
functions and information for data processing specific to the class.
<?php
class CME
{
function msg()
{
return “This is Hasini”;
}
}
$obj = new CME();
echo $obj->msg();
?>
Output:
This is Hasini
<?php
$num = NULL;
echo “ This is $num “;
?>
Output:
This is
2. Resources: Resources in PHP are not an exact data type. These are basically used to
store references to some function call or to external PHP resources. For example,
consider a database call. This is an external resource.
11. Explain how to declare Variables and Constants in PHP
Variables:
➢ Variables are used for storing values
➢ in PHP, a variable does not need to be declared before adding a value to it.
➢ PHP automatically convert the variable to the correct data type, depending on its
value.
➢ All variables in PHP start with a “$” symbol, followed by the name of the variable
Example:
<?php
$a = 3;
$b = “Hasini”;
echo $b . “:” . $a;
?>
Output:
Hasini : 3
Constants:
➢ A constant is an identifier (name) for a simple value. The value cannot be changed
during the script.
➢ A valid constant name starts with a letter or underscore
➢ In PHP, we can create a constant in two ways
• Using define() function
• Using const keyword
define():
Syntax: define(name, value);
Example: define(“PIE”,3.14);
const:
Syntax: const name = value;
Example: const CME = “Computer Engineering”;
PHP Example:
<?php
define(“PIE”,3.14);
const CME = “Computer Engineering”
echo PIE . “\n”;
echo CME;
?>
Output:
3.14
Computer Engineering
Function Description
1 strlen() It returns the length of a string
2 strtolower() It converts a string to lowercase
3 strtoupper() It converts a string to uppercase
4 strrev() It returns the reversed string of the given string
5 trim() It removes whitespace from the beginning and end of a string
6 substr() Extracts a substring from a string based on a specified start and end position
7 strcmp() It compares two strings
8 str_repeat() Repeats a string in specified number of times
9 str_split() It converts a string into an array of characters
10 str_replace() It replaces all occurrences of a substring with another substring within a
string
11 ucfirst() It converts the first character of a string to uppercase
12 ucwords() It converts the first character of each word in a string to uppercase
Example:
<?php
$string = “Hello Hasini!”;
echo “String Length:” . strlen($string);
echo “Uppercase :” . strtoupper($string);
echo “Lowercase :” . strtolower($string);
echo “Reversed string :” . strrev($string);
?>
Output:
String Length: 13
Uppercase: HELLO HASINI!
Lowercase: hello hasini!
Reversed string : !inisaH olleH
a. Automatically: here the indexes are automatically assigned. The index starts at 0.
Example: $names[0]=”mani”;
$names[1]=”teja”;
$names[2]=”hasini”;
PHP example:
<?php
$names = array(“mani”, “teja”, “hasini”);
echo $names[0] . $names[1];
?>
Output:
maniteja
ii. Associative Arrays: in Associative array, each ID key is associated with the value. With
associative arrays, we can use the values as keys and assign values to them.
Example:
<?php
$ages[‘mani’] = 5;
$ages[‘hasini’] = 3;
echo “Hasini is “. $ages[‘hasini’] . “ years old”;
?>
Output:
Hasini is 3 years old
Example:
<?php
$branches = array(“CME”=>array(“mani”,”teja”),
“MECH” => array(“hasini”,”rohitha”));
echo “First student in CME is :”. $branches[‘CME’][0];
?>
Output:
First student in CME is : mani
<?php
$names = array(“mani”, “teja”, “hasini”);
echo $names[0] . $names[1];
?>
Output:
maniteja
GET method:
• $_GET() function is used to collect values from a form sent with method=”get”
• Information sent from a form with the GET method is visible to everyone. (it will be
displayed in the browser’s address bar) and the maximum length of information is
100 characters.
Example:
Form.html
Output:
POST method:
• $_POST function is used to collect values from a form sent with method=”post”
• Information sent from a form with POST method is not visible to others and there is
no limit on information length.
Example:
Form1.html
<form action = “welcome1.php” method=”post”>
Name: <input type=”text” name=”sname”/><br>
Age: <input type=”text” name=”age” /> <br>
<input type=”submit”/>
</form>
Welcome1.php
<html>
<body>
Welcome <?php echo $_POST[“sname”]; ?> <br>
You are <?php echo $_POST[“age”]; ?> years old
</body>
</html>
Output:
Function Description
1 Connect() Opens a new connection to the MySQL server
2 Close() Closes a previously opened database connection
3 Commit() Commits the current transaction
4 Autocommit() Turns on or off auto-committing database modifications
5 Rollback() Rolls back the current transaction for the database
6 Debug() Performs debugging operations
7 Error() Returns the last error description for the most recent function call
8 Fetch_all() Fetches all result rows as an associative array, a numeric array, or both
9 Fetch_array() Fetches a result row as an associative, a numeric array, or both
10 Query() Performs a query against a database
11 Kill() Asks the server to kill a MySQL thread
Example:
<?php
$con = mysql_connect(“localhost”,”root”,””);
If(!$con)
{
Die(‘could not connect:’ . mysql_error());
}
Mysql_select_db(“my_db”,$con);
$result = mysql_query(“select pin_number from student”);
While ($row = mysql_fetch_array($result))
{
echo $row[‘pin_number’] ;
echo “\n”;
}
mysql_close($con);
?>
Output:
23374-CM-001
23374-CM-002
23374-CM-003
23374-CM-004
Steps in Detail:
<?php
$host = 'localhost:3306';
$userid = 'kits';
$password = 'kits123';
$dbname = 'myDB';
if(!$conn)
{
die('Could not connect: '.mysqli_connect_error());
}
$sql = 'INSERT INTO emp(empid,name,salary) VALUES (1,"Manasa", 90000)';
if(mysqli_query($conn, $sql))
{
echo "Record inserted successfully";
}
else
{
echo "Could not insert record: ". mysqli_error($conn);
}
mysqli_close($conn);
?>
21.updating the data in a table in PHP
<?php
$host = 'localhost:3306';
$userid = 'kits';
$password = 'kits123';
$dbname = 'myDB';
if(!$conn)
{
die('Could not connect: '.mysqli_connect_error());
}
$sql = ‘UPDATE emp SET salary=100000 WHERE empid=1';
if(mysqli_query($conn, $sql))
{
echo "Record updated successfully";
}
else
{
echo "Could not update record: ". mysqli_error($conn);
}
mysqli_close($conn);
?>
<?php
$host = 'localhost:3306';
$userid = 'kits';
$password = 'kits123';
$dbname = 'myDB';
if(!$conn)
{
die('Could not connect: '.mysqli_connect_error());
}
mysqli_close($conn);
?>
<?php
$host = 'localhost:3306';
$userid = 'kits';
$password = 'kits123';
$dbname = 'myDB';
if(!$conn)
{
die('Could not connect: '.mysqli_connect_error());
}
mysql_select_db($dbname,$ conn);
$result = mysql_query(“select pin_number from student”);
while ($row = mysql_fetch_array($result))
{
echo $row[‘pin_number’] ;
echo “\n”;
}
mysql_close($conn);
?>
Output:
23374-CM-001
23374-CM-002
23374-CM-003
Example:
<?php
$expire = time() + 50;
Setcookie(“user”,”hasini”,$expire);
?>
Retrieve a cookie:
• $_COOKIE[] method is used to retrieve a cookie.
Syntax: $_COOKIE[cookie_name]
Example:
<html>
<body>
<?php
If(isset($_COOKIE[“user”]))
echo “welcome “ . $_COOKIE[“user”];
else
echo “welcome guest!”;
?>
</body>
</html>
Deletion of Cookie:
• When deleting a cookie we assure that the expiration date is in the past.
Example:
<?php
Setcookie(“user”,” “, time()-3600);
?>
<?php
session_start();
?>
Storing Session data: (Session Variables)
• $_SESSION[ ] array is used to store data in session variables.
<?php
Session_start();
$_SESSION[“RollNum”]=”001”;
$_SESSION[“Name”] = “Hasini”;
?>
Accessing / Retrieving Session Data:
• By passing the corresponding key to the $_SESSION[ ] we can access the session
data.
<?php
Session_start();
echo “the name of the student is : ”. $_SESSION[“Name”];
?>
<?php
Session_start();
Session_destroy();
?>
29. Passing data from one web page to other webpage using query string
A query string is data added at the end of a URL. In the link below, everything after the ?
sign is part of the query string:
<a href="demo_phpfile.php?subject=PHP&web=Kits.com">Test $GET</a>
In the PHP file we can use the $_GET variable to collect the value of the query string.
Example:
The PHP file demo_phpfile.php:
<!DOCTYPE html>
<html>
<body>
<?php
echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
?>
</body>
</html>