0% found this document useful (0 votes)
3 views9 pages

Server Side Scripting

The document provides an overview of server-side scripting, specifically focusing on PHP as a programming language for web development. It explains the functionalities of server-side scripts, the basics of PHP, and how it interacts with databases like MySQL. Additionally, it covers the differences between server-side and client-side programming, and includes examples of executing PHP commands and creating simple web pages.

Uploaded by

amirthae.cs24
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)
3 views9 pages

Server Side Scripting

The document provides an overview of server-side scripting, specifically focusing on PHP as a programming language for web development. It explains the functionalities of server-side scripts, the basics of PHP, and how it interacts with databases like MySQL. Additionally, it covers the differences between server-side and client-side programming, and includes examples of executing PHP commands and creating simple web pages.

Uploaded by

amirthae.cs24
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

22CT404 & WEB TECHNOLOGY AND FRAMEWORKS

UNIT II - SERVER-SIDE SCRIPTING - PHP

PROGRAMMING FUNDAMENTALS

1. SERVER-SIDE SCRIPTING
Besides creating a personalized user experience, the server relies on the back-end code to
store login details, manage the source code, run relevant analyses on user behavior, and control who
has access to information in the database.

Highlighted below are several other functionalities of server-side scripts.

 Manage data in a database. To a large extent, the type of server-side script you intend to use
will determine how to build and manage the database. It also helps control who has access to
the data on the database and distributes relevant data accordingly.
 Send and receive cookies. Server-side codes are essential for building and sending cookies on
the website. Cookies help improve the website experience by storing data like logins and
passwords.
 Encrypt and validate data. Encryption is especially important for sensitive data, such as
financial information or medical records. Through data encryption and validation, back-end
scripts help keep information safe from unauthorized access and misuse.
 Run server-side code on a server embedded in the site’s code. When a user enters a URL into
their web browser, the browser sends a request to the server for the specific page. The server
then processes the request and sends the HTML code for the page back to the user’s browser.
 Build web apps unique for the database. Server-side codes allow users to build web
applications that interact with permanent back-end storage, like databases, and process
information from the server to access the database—like a direct line from a user to a
database.
 Create dynamic webpages. The code can generate the HTML code for the response page
based on user input or other conditions.
 Run on-call. Whether the web servers run an HTTP “GET” or “POST” command, the back-
end code helps process the data and send a response.
 Build Application Programming Interfaces (APIs). The server-side code is responsible for
creating the API, a set of functions and protocols that allow software programs to
communicate with each other.
3
2. PHP WEB DEVELOPMENT BASICS
Hypertext Preprocessor (PHP) is a general-purpose programming language widely used to
develop dynamic webpages. It began as a common gateway interface CGI script for taking basic
analysis on a website homepage. However, it can now be embedded into HTML code or used with
various web frameworks to develop web applications.

PHP has many features that make it excellent for back-end web development. It supports object-
oriented programming, has a built-in development server, and can be integrated with many
databases. PHP also has a large community of developers who contribute to the language, create
helpful resources, and develop extensions that add extra functionality.

Here are some popular websites powered by a PHP back-end system.

 Slack
 MailChimp
 Tumblr
 WordPress

What is server-side website programming?


Web browsers communicate with web servers using the HyperText Transfer Protocol (HTTP).
When you click a link on a web page, submit a form, or run a search, an HTTP request is sent from
your browser to the target server.

The request includes a URL identifying the affected resource, a method that defines the required
action (for example to get, delete, or post the resource), and may include additional information
encoded in URL parameters (the field-value pairs sent via a query string), as POST data (data sent by
the HTTP POST method), or in associated cookies.

Web servers wait for client request messages, process them when they arrive, and reply to the web
browser with an HTTP response message. The response contains a status line indicating whether or
not the request succeeded (e.g. "HTTP/1.1 200 OK" for success).

The body of a successful response to a request would contain the requested resource (e.g. a new
HTML page, or an image), which could then be displayed by the web browser.

Static sites

The diagram below shows a basic web server architecture for a static site (a static site is one that
returns the same hard-coded content from the server whenever a particular resource is requested).
When a user wants to navigate to a page, the browser sends an HTTP "GET" request specifying its
URL.
4
The server retrieves the requested document from its file system and returns an HTTP response
containing the document and a success status (usually 200 OK). If the file cannot be retrieved for
some reason, an error status is returned (see client error responses and server error responses).

Dynamic sites

A dynamic website is one where some of the response content is generated dynamically, only when
needed. On a dynamic website HTML pages are normally created by inserting data from a database
into placeholders in HTML templates (this is a much more efficient way of storing large amounts of
content than using static websites).

A dynamic site can return different data for a URL based on information provided by the user or
stored preferences and can perform other operations as part of returning a response (e.g. sending
notifications).

Most of the code to support a dynamic website must run on the server. Creating this code is known
as "server-side programming" (or sometimes "back-end scripting").

The diagram below shows a simple architecture for a dynamic website. As in the previous diagram,
browsers send HTTP requests to the server, then the server processes the requests and returns
appropriate HTTP responses.

Requests for static resources are handled in the same way as for static sites (static resources are any
files that don't change — typically: CSS, JavaScript, Images, pre-created PDF files, etc.).

5
Requests for dynamic resources are instead forwarded (2) to server-side code (shown in the diagram
as a Web Application). For "dynamic requests" the server interprets the request, reads required
information from the database (3), combines the retrieved data with HTML templates (4), and sends
back a response containing the generated HTML (5,6).

Are server-side and client-side programming the same?

Let's now turn our attention to the code involved in server-side and client-side programming. In each
case, the code is significantly different:

 They have different purposes and concerns.


 They generally don't use the same programming languages (the exception being JavaScript,
which can be used on the server- and client-side).
 They run inside different operating system environments.

Code running in the browser is known as client-side code and is primarily concerned with improving
the appearance and behavior of a rendered web page. This includes selecting and styling UI
components, creating layouts, navigation, form validation, etc. By contrast, server-side website
programming mostly involves choosing which content is returned to the browser in response to
requests. The server-side code handles tasks like validating submitted data and requests, using
databases to store and retrieve data and sending the correct data to the client as required.

Client-side code is written using HTML, CSS, and JavaScript — it is run inside a web browser and
has little or no access to the underlying operating system (including limited access to the file
system).

Web developers can't control what browser every user might be using to view a website — browsers
provide inconsistent levels of compatibility with client-side code features, and part of the challenge
of client-side programming is handling differences in browser support gracefully.

Server-side code can be written in any number of programming languages — examples of popular
server-side web languages include PHP, Python, Ruby, C#, and JavaScript (NodeJS). The server-side
code has full access to the server operating system and the developer can choose what programming
language (and specific version) they wish to use.

PHP (or PHP Hypertext Preprocessor) is a server-side scripting language that is used to create
dynamic web pages that can interact with databases. It is a widely-used open source language that is
specifically used for web application development and can be embedded within HTML. In addition
to the article on PHP & MySQL be sure to check out the important blog post on what is markdown.

Why PHP?

The distinguishing feature of PHP is that the scripting code is executed on the server, which
generates HTML that is sent back to the client. The client receives the result of executing the script
without knowing the underlying code. Developers can configure the web server to process all the
HTML files (containing the PHP script).

6
Using PHP with a database system

PHP, as a scripting language, is popular among web developers because of its ability to interact with
database systems including Oracle and MySQL.

This article discusses the use of PHP scripting language with the MySQL database. Any website can
require a variety of data or information to display and to retrieve them from the database. This can
include display of a simple list to the running of the website based on data stored in the database.

Listed below are some examples where PHP and MySQL can be used together:

 Digital Ad banners, where the PHP script can be used to retrieve a digital banner from the
database, which then selects a random banner from its table records and sends it back to the
calling script. The PHP script can also maintain a count of banner views and clicks from the
website.
 Internet forums or digital boards, which use PHP and MySQL to store and retrieve user
messages.
 Website designing, where the design of an entire website can be changed using a couple of
PHP scripts, instead of changing and uploading each web page. The PHP script can access
the MySQL database to retrieve all information about the web page.
Get to know more on microservices interview questions.

3. EXECUTING PHP COMMANDS

After configuring and connecting to the MySQL database, you can start executing PHP commands
on the server.

Following are the 2 methods of executing a PHP command:

 Entering the command in PHP using the following syntax:

Mysql_query($query)
Copy Code
This form of command can be used to repeat the command simply by changing the variable.

 Defining the command as a variable. The result of the operation will be assigned to the
variable.
Data input and output

Inserting data using PHP is identical to the procedure of data input using HTML pages. The
advantage of using PHP is that the script does not need to be changed for each new piece of input
7
data. Users can also input their own data on the web [Link] is an example of an HTML
page with textboxes that can be used to enter data in a form:

Alternatively, you can use variables to input information into the database. Example:

$first=$_POST[‘first’];

$last=$_POST[‘last’];

$phone=$_POST[‘phone’];

$mobile=$_POST[‘mobile’];

$fax=$_POST[‘fax’];

$email=$_POST[’email’];
8
$web=$_POST[‘web’];

$query = “INSERT INTO contacts VALUES


(”,’$first’,’$last’,’$phone’,’$mobile’,’$fax’,’$email’,’$web’)”;

mysql_query($query);

This script is saved in the [Link] file, which can be called from the HTML form. Using this
method, data entered in the web page form is stored in the defined variables, which are then passed
to the PHP.

To display (or output) the entered data using PHP, you can use the following MySQL command with
the result assigned to the variable.

$query=”SELECT * FROM contacts”;

$result=mysql_query($query);

PHP provides 2 submission methods, GET and POST to get the data submitted by the form into your
PHP script. GET method displays the variables and the data in the page address, while they are
invisible in the POST method. For example, a script can be created that will display different web
pages depending on the clicked link.

[Link]?user=david (to show David’s page)

[Link]?user=tom (to show Tom’s page)

9
4. Simple Web page creation using PHP

Your first PHP-enabled page

Create a file named [Link] and put it in your web server's root directory (DOCUMENT_ROOT)
with the following content:

Example #1 Our first PHP script: [Link]

<!DOCTYPE html>
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>
Use your browser to access the file with your web server's URL, ending with the /[Link] file
reference. When developing locally this URL will be something like [Link] or
[Link] but this depends on the web server's configuration. If everything is
configured correctly, this file will be parsed by PHP and the following output will be sent to your
browser:

<!DOCTYPE html>
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<p>Hello World</p>
</body>
</html>
This program is extremely simple and you really did not need to use PHP to create a page like this.
All it does is display: Hello World using the PHP echo statement. Note that the file does not need to
be executable or special in any way. The server finds out that this file needs to be interpreted by PHP
because you used the ".php" extension, which the server is configured to pass on to PHP. Think of
this as a normal HTML file which happens to have a set of special tags available to you that do a lot
of interesting things.

If you tried this example and it did not output anything, it prompted for download, or you see the
whole file as text, chances are that the server you are on does not have PHP enabled, or is not
configured properly. Ask your administrator to enable it for you using the Installation chapter of the
manual. If you are developing locally, also read the installation chapter to make sure everything is
configured properly. Make sure that you access the file via http with the server providing you the
output. If you just call up the file from your file system, then it will not be parsed by PHP. If the
problems persist anyway, do not hesitate to use one of the many » PHP support options.
10
The point of the example is to show the special PHP tag format. In this example we used <?php to
indicate the start of a PHP tag. Then we put the PHP statement and left PHP mode by adding the
closing tag, ?>. You may jump in and out of PHP mode in an HTML file like this anywhere you
want. For more details, read the manual section on the basic PHP syntax.

Note: A Note on Line Feeds

Line feeds have little meaning in HTML, however it is still a good idea to make your HTML look
nice and clean by putting line feeds in. A linefeed that follows immediately after a closing ?> will be
removed by PHP. This can be extremely useful when you are putting in many blocks of PHP or
include files containing PHP that aren't supposed to output anything. At the same time it can be a bit
confusing. You can put a space after the closing ?> to force a space and a line feed to be output, or
you can put an explicit line feed in the last echo/print from within your PHP block.

Note: A Note on Text Editors

There are many text editors and Integrated Development Environments (IDEs) that you can use to
create, edit and manage PHP files. A partial list of these tools is maintained at » PHP Editors List. If
you wish to recommend an editor, please visit the above page and ask the page maintainer to add the
editor to the list. Having an editor with syntax highlighting can be helpful.

Note: A Note on Word Processors

Word processors such as StarOffice Writer, Microsoft Word and Abiword are not optimal for editing
PHP files. If you wish to use one for this test script, you must ensure that you save the file as plain
text or PHP will not be able to read and execute the script.

Now that you have successfully created a working PHP script, it is time to create the most famous
PHP script! Make a call to the phpinfo() function and you will see a lot of useful information about
your system and setup such as available predefined variables, loaded PHP modules, and
configuration settings. Take some time and review this important information.

Example #2 Get system information from PHP

<?php phpinfo(); ?>

11

You might also like