0% found this document useful (0 votes)
30 views22 pages

WT Unit-V (C23)

Uploaded by

Satyasai
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)
30 views22 pages

WT Unit-V (C23)

Uploaded by

Satyasai
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/ 22

Web Technologies (CM-402)

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

2. List various web servers


➢ Apache HTTP server
➢ IIS (Internet Information Services) web server
➢ Sun Java System web server
➢ Jigsaw server web server
➢ Lighttpd web server
➢ Lite speed server web server
➢ Node.js web server

3. various HTTP request types


➢ GET
➢ POST
➢ PUT
➢ CONNECT
➢ HEAD
➢ DELETE
➢ OPTIONS
➢ TRACE
➢ PATCH

4. Differences between Http GET and Http POST methods


Http GET Http POST
1 Back button Reloadable Alert to user for re-submission
/ Reload
2 Book Can be book marked Cannot be book marked
marked
3 cached Can be cached Cannot be cached
4 history Parameters saved in browser history Parameters are not saved in
browser history
5 Visibility Data is visible to everyone in the URL Data is not visible
6 Security Less secure than POST method Safer than GET method
5. Compare the properties of IIS and Apache
➢ IIS is packaged with Windows while Apache is free and Open source
➢ IIS only runs on Windows, Apache can run on almost any Operating system
➢ The security features of IIS make it a safer option than Apache
➢ IIS has a help desk to handle most issues while support for Apache comes from the
user community.

6. State the importance of PHP


PHP is an important scripting language for web development because it’s
• Fast
• Open source and free
• Easy to learn
• Platform independent
• Compatible with many databases
• Secure

7. Explain how to combine HTML and PHP


• There are various methods to integrate PHP and HTML
• We can add PHP tags to our HTML Page. We simply need to enclose the PHP code
with the PHP starts tag <?php and the PHP end tag ?>.
• A PHP file normally contains HTML tags and some PHP scripting code

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)

9. List Data types in PHP


➢ Pre-defined data types
• Integer
• Float / Double
• String
• Boolean
➢ User-Defined data types
• Array
• Object
➢ Special Data Types
• NULL
• Resources

10. Explain Data types in PHP with examples


➢ Pre-defined data types
• Integer
• Float / Double
• String
• Boolean
➢ User-Defined data types
• Array
• Object
➢ Special Data Types
• NULL
• Resources

Pre-defined data types:


1. Integer: Integers hold only whole numbers including positive and negative numbers, i.e., numbers without
fractional part or decimal point.
<?php
$a = 10;
$b = 20;
$c = $a + $b;
echo “Total = “ . $c . “\n”;
var_dump($c);
?>

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

User-defined data types:

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

Special data types:


1. NULL: These are special types of variables that can hold only one value i.e., NULL

<?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

Syntax: $var_name = value;

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

12. List and explain string manipulation functions in PHP

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

13. Arrays in PHP


• an Array is a special variable, which can store multiple values in one single variable
• we can access the values by referring to the array name
• each element in the array has its own index. So that it can be easily accessed
• in PHP, there are three types of Arrays
i. Numeric Array
ii. Associative Array
iii. Multi-dimensional Array
14. Explain types of arrays
• in PHP, there are three types of Arrays
i. Numeric Array
ii. Associative Array
iii. Multi-dimensional Array
i. Numeric Array: a numeric array stores each array element with a numeric index.
There are two methods to create a numeric array
a. Automatically
b. Manually

a. Automatically: here the indexes are automatically assigned. The index starts at 0.

Example: $names = array(“mani”, “teja”, “hasini”);

b. Manually: here the indexes are manually assigned

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.

$ages = array(“mani” =>5, “hasini”=>3);


(or)
$ages[‘mani’] = 5;
$ages[‘hasini’] = 3;

Example:
<?php
$ages[‘mani’] = 5;
$ages[‘hasini’] = 3;
echo “Hasini is “. $ages[‘hasini’] . “ years old”;
?>
Output:
Hasini is 3 years old

iii. Multi-dimensional Arrays: in a multi-dimensional array, each element in the main


array can also be an array. And each element in the sub-array can be an array , and so
on.

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

15.Design small programs using arrays

<?php
$names = array(“mani”, “teja”, “hasini”);
echo $names[0] . $names[1];
?>
Output:
maniteja

16.Explain form handling in PHP using $_GET,$_POST

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

<form action = “welcome.php” method=”get”>


Name: <input type=”text” name=”sname”/><br>
Age: <input type=”text” name=”age” /> <br>
<input type=”submit”/>
</form>
welcome.php
<html>
<body>
Welcome <?php echo $_GET[“sname”]; ?> <br>
You are <?php echo $_GET[“age”]; ?> years old
</body>
</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:

17.List My SQL database functions in PHP


• Connect()
• Close()
• Commit()
• Autocommit()
• Rollback()
• Debug()
• Error()
• Fetch_all()
• Fetch_array()
• Query()
• Kill()

18. Explain My SQL database functions in PHP

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

19.Explain the steps of connecting to a Database in PHP


In PHP, we can connect to the database using XAMPP web server by using the following
path "localhost/phpmyadmin"

Steps in Detail:

• Open XAMPP and start running Apache, MySQL and FileZilla


• Now open your PHP file and write your PHP code to create database and a table in your database.

PHP code to create a database:

• Save the file as “data.php” in htdocs folder under XAMPP folder.

• Then open your web browser and type localhost/data.php

• Finally the database is created and connected to PHP.


If you want to see your database, just type localhost/phpmyadmin in the web browser and the
database can be found.

20. Inserting Data into a table in PHP

<?php

$host = 'localhost:3306';
$userid = 'kits';
$password = 'kits123';
$dbname = 'myDB';

$conn = mysqli_connect($host, $userid, $password,$dbname);

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';

$conn = mysqli_connect($host, $userid, $password,$dbname);

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);
?>

22. Delete data in a table in PHP

<?php

$host = 'localhost:3306';
$userid = 'kits';
$password = 'kits123';
$dbname = 'myDB';

$conn = mysqli_connect($host, $userid, $password,$dbname);

if(!$conn)
{
die('Could not connect: '.mysqli_connect_error());
}

$sql =DELETE from emp WHERE empid=1';


if(mysqli_query($conn, $sql))
{
echo "Record deleted successfully";
}
else
{
echo "Could not delete record: ". mysqli_error($conn);
}

mysqli_close($conn);
?>

23. retrieving data from a table

<?php
$host = 'localhost:3306';
$userid = 'kits';
$password = 'kits123';
$dbname = 'myDB';

$conn = mysqli_connect($host, $userid, $password,$dbname);

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

24. Define COOKIE


➢ a Cookie is a small piece of information which is stored at client browser.
➢ It is used to recognize the user
➢ Each time when client sends request to the server, cookie is embedded with
request.
➢ Cookies are used to carry information from website to website

25. How to create and delete a cookie


Creation of Cookie:
• The “setcookie()” function is used to set / create a cookie.
• Setcookie() function must appear before <html> tag

Syntax: Setcookie(name, value, expiration);

name: the name of our cookie


value: the value that is stored in our cookie
expiration: the date when the cookie will expire

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);
?>

26. Define Session


• a session is the total time spent for an activity
• in computer systems, a user session begins when a user login to (or) access a
particular computer, network or software service
• it ends when the user logs out of the service or shutdown the computer
• a session can temporarily store information related to the activities of the user
while connected

27. How to Create and Destroy a Session


Create a session:
• The first step is to start up a session
• After a session is started, session variables can be created to store information
• session_start() function is used to begin a new session

<?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”];
?>

Destroy / Delete a Session variable:


• To delete only a particular session data, “unset” function is used.
<?php
Session_start();
If(isset($_SESSION[“Name”]))
{
Unset($_SESSION[“Name”]);
}
?>
Destroy / Delete session:
• The “session_destroy()” function is used to destroy / delete a session completely.

<?php
Session_start();
Session_destroy();
?>

28. Differences between Sessions and Cookies


SESSION COOKIE
1 A session stores the variables and their values Cookies are stored on the user’s
within a file in a temporary directory on the computer as a text file
server
2 The session ends when the user logout from the Cookies ends on the life time set by
application or closes web browser the user
3 Sessions are server-side files that contain user Cookies are client-side files on a local
data computer that hold user information.
4 Sessions are more secured compare than Cookies are not secured.
cookies.
5 Session save data in encrypted form Cookies stored data in text file
6 Session stored a unlimited data. Cookies stored on a limited data.

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>

The query string above contains two key/value pairs:


subject=PHP
web=Kits.com

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>

You might also like