Introduction to Server Side Scripting-1
Introduction to Server Side Scripting-1
side scripting
INTRODUCTION
HTTP is a communication standard governing the requests and responses that take place between
the browser running on the end user’s computer and the web server.
Between the client and the server there can be several other devices, such as routers, proxies,
gateways, dns and so on.
A web server can usually handle multiple simultaneous connections and—when not communicating
with a client—spends its time listening for an incoming connection.
When one arrives, the server sends back a response to confirm its receipt.
The Request/Response
Procedure
The Request/Response Procedure
1. You enter http://server.com into your browser’s address bar.
2. Your browser looks up the IP address for server.com.
3. Your browser issues a request to that address for the web server’s home page.
4. The request crosses the Internet and arrives at the server.com web server.
5. The web server, having received the request, fetches the home page from its hard disk.
6. With the home page now in memory, the web server notices that it is a file incorporating
PHP scripting and passes the page to the PHP interpreter.
The Request/Response Procedure
7. The PHP interpreter executes the PHP code.
8. Some of the PHP contains MySQL statements, which the PHP interpreter now
passes to the MySQL database engine.
9. The MySQL database returns the results of the statements to the PHP interpreter.
10. The PHP interpreter returns the results of the executed PHP code, along with the
results from the MySQL database, to the web server.
11. The web server returns the page to the requesting client, which displays it.
Apache Web Server
Apache doesn’t serve up just HTML files—it handles a wide
range of files from images and Flash files to MP3 audio files, RSS
(Really Simple Syndication) feeds, and so on.
PHP running on Apache can even create images and other files
for you, either on the fly or in advance to serve up later.
<!DOCTYPE html>
<html> The output:
<body>
Hello world!
I'm about to learn PHP!
<?php
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
?>
</body>
</html>
PHP Comments
<!DOCTYPE html>
<html>
<body>
<?php
// This is a comment
/* This is a section of multiline comments
which will not be interpreted */
?>
</body>
</html>
PHP Variables
A variable starts with the $ sign, followed by the name of the variable.
A variable name must start with a letter or the underscore character, but cannot start with a number
Two-dimensional array
<?php
$oxo = array(array('x', ' ', 'o'),
array('o', 'o', 'x'),
array('x', 'o', ' '));
?>
PHP Array
Numerical indexed array – array contents are indexed numerically
<?php
$paper[] = "Copier"; This example prints out the following:
$paper[] = "Inkjet";
0: Copier
$paper[] = "Laser"; 1: Inkjet
$paper[] = "Photo"; 2: Laser
3: Photo
for ($j = 0 ; $j < 4 ; ++$j)
echo "$j: $paper[$j]<br>";
?>
PHP Array
Associative array – reference the items in an array by name
The output:
• PHP supports two types of strings, denoted by the type of quotation mark that you use.
i. A literal string preserves the exact contents; use the single quotation mark (apostrophe)
ii. To include the value of a variable inside a string; use double-quoted strings
variable
$text = "Hello"; The output:
$text .= " World!";
Hello World !
echo $text;
• String can concatenate with other data types. PHP will automatically converts non-string types to
• It provide essential access points to various types of data, including user input, server environment details,
cookies, session data, and uploaded files, making them fundamental for web development tasks
• Using the htmlentities function for sanitization is an important practice in any circumstance where user or other
third-party data is being processed for output, not just with superglobals.
$came_from = htmlentities($_SERVER['HTTP_REFERER']);
Variable Types
Superglobal Name Contents
$GLOBALS All variables that are currently defined in the global scope of the
script. The variable names are the keys of the array.
$_SERVER Information such as headers, paths, and script locations. The entries
in this array are created by the web server, and there is no guarantee
that every web server will provide any or all of these.
$_GET Variables passed to the current script via the HTTP Get method. It
stores key-value pairs where keys represent parameter names, and
values are the corresponding user data
$_POST Variables passed to the current script via the HTTP Post method. It
used when submitting form data that should not be visible in the URL,
such as passwords or large data sets
Variable Types
Superglobal Name Contents
$_FILES Items uploaded to the current script via the HTTP Post method,
including file name, type, size, temporary storage location, and error
status
$_COOKIE Variables passed to the current script via HTTP cookies. They are often
used for remembering user preferences, login credentials, and
tracking user behavior
$_SESSION Session variables available to the current script. This is commonly
used for user authentication.
$_REQUEST Contents of information passed from the browser; by default, $_GET,
$_POST, and $_COOKIE.
$_ENV Variables passed to the current script via the environment method.
Conditionals
• The if condition can be any valid PHP expression
• when a condition is not TRUE, but you want to continue, you need to use else
• With an if...else statement, the first conditional statement is executed if the condition is TRUE. But if it’s
FALSE, the second one is executed
• The else if statement in if...else if...else construct is used when you want a number of different possibilities
to occur
Conditionals
• The switch statement is useful in cases in which one variable
or the result of an expression can have multiple values
<?php
switch ($page)
{
case "Home":
echo "You selected Home"; break;
case "About":
echo "You selected About"; break;
case "News":
echo "You selected News"; break;
case "Login":
echo "You selected Login"; break;
case "Links":
echo "You selected Links"; break;
default:
echo "Unrecognized selection";break;
}
?>
Looping
Looping are fundamental in PHP to execute a block of code repeatedly based on given conditions. It allows PHP
programmers to handle repetitive tasks and process data collections smoothly.
• A while loop continuously executes and checks for the condition to stay TRUE
• Slight variation to the while loop is the do...while loop, used when you want a block of code to be executed at
least once and made conditional only after that
• The for loop, is also the most powerful, as it combines the abilities to set up variables as you enter the loop,
test for conditions while iterating loops, and modify variables after each iteration
Example : A while loop to print the 12 times table
Looping
<?php
$count = 0;
while (++$count <= 12)
echo "$count times 12 is " . $count * 12 . "<br>";
Example : The for loop with added curly braces ?>
<?php
for ($count = 1 ; $count <= 12 ; ++$count)
Example : A do...while loop for printing the times table for 12
{
<?php
echo "$count times 12 is " . $count * 12;
$count = 1;
echo "<br>";
do
}
{
?>
echo "$count times 12 is " . $count * 12;
echo "<br>";
}
while (++$count <= 12);
?>
PHP Session
• Sessions provide a solid way of keeping track of users
session_start();
$_SESSION['variable'] = $value;
session_destroy();