You are on page 1of 28

Advanced Internet Programming

By: Yitbarek Worku(MSc)

Academic Year 2015 E.C


Chapter : One

Introduction to Server Side Scripting Basics


Overview of web scripting

• Web page -is a single document display on the browser.

• Home page -is a default page that is loaded into a browser when it is opened.

• Web site -is a collection of related web pages. Has a globally unique name.

• HTTP- used to exchange request/response messages between web browse and web server.

• Web Browser- is a software which is used to browse and display pages available over
internet.
Cont.…
 Web server- is a software which provides these documents when requested by web browsers via HTTP.

 WWW- A collection of websites stored in web servers.

 URL- The exact address of a resource on the web. It have its own formats. (Protocol, hostname(Domain),

path ).

 DNS-Resolves a human friendly name to a machine friendly name.( www.amazon.com to 198.162.10.1)

 Hypertext -is a way o f moving from one web page to another.

 ISP- is a commercial company which sells Internet connections to users.

 Internet- an international linking millions of individual users.


Cont.…
Scripts- are a series of commands that are able to be executed without the need for

compiling.
It can be carryout on client side or server side .

Web scripting- is a process of creating and embedding scripts in a web page.

The content of the web page will not be the same every time you look at it.

It will change dynamically depending on certain factors such as the actions of the person

viewing the page.


Web site can be :
Static VS Dynamic Web Sites

Static Websites
 Written in HTML only – web pages
 Pages are separate documents
 No database that draws to it
 In a nutshell:
• Simple web pages with no interactivity
Cont...

Dynamic Websites
 Requires more complex coding
 Web pages can do a lot more – interactive!
 Examples – login page, search page, querying a backend database
 Has three major technologies:
• Markup language like HTML
• Scripting (both client and server side) language
• Style sheet (for presentation) like CSS
Static VS Dynamic Web Sites

Static Dynamic

Simple to code (HTML only) Complex coding (HTML + scripting +


style sheet + PHP)
No database connection Usually connects to either flat files or a
database
Quick to develop Takes longer to develop

No user interaction Emphasizes on user interaction (e.g. fill


up a form, login)
7
Client-Side Scripting VS Server-Side Scripting
Client-side Server-side

Scripts are stored on the client (engine is in Scripts are stored on the server (engine is on
browser) server)

Scripts can be modified by the end user Scripts cannot be modified by the end user

Browser-dependent Browser-independent

Source code can be viewed Source code can’t be viewed

Can’t communicate with a database Can communicate with a database

No network overhead Dependent on network bandwidth

Processing is done by the browser - fast Processing is done by the server - slower
8
Main Arguments for the Use of PHP
Main Reasons to Use PHP
Powerful and flexible

Easy to learn

C-like and therefore a generalizable skill

Extremely portable

Cheap

Easy to set up

Works with lots of databases

Availability of source code (you can even contribute!)


9
What is PHP ?

• PHP stands for hypertext preprocessor.

• PHP is a server-side scripting language, like ASP


• PHP scripts are executed on the server , not on the user's browser, so you do not need
to worry about compatibility issues.
• PHP supports many databases (MySQL, Informix, Oracle, Sybase , Solid,
PostgreSQL, Generic ODBC, etc.)
• PHP is an open source software (OSS)
• PHP is free to download and use.

10
What is a PHP file?

• PHP files are returned to the browser as plain HTML.


• PHP file scripts are executed on the server.
• PHP files have a file extension of ".php“, ".php3", or ".phtml"
• PHP files can contain text, HTML tags and scripts.
• PHP file is Interpreted rather than compiled like Java or C.

11
Cont.…

• An embedded scripting language, meaning that it can exist within HTML code.
• Cross-platform, meaning it can be run on Linux, Windows, Macintosh, etc.,
making PHP very portable.
• Compatible with almost all servers used today (Apache, IIS, etc.)
• Supports many databases (MySQL, Informix, Oracle, Sybase, Solid,
PostgreSQL, Generic ODBC, etc.).
• Easy to learn and runs efficiently on the server side

12
What Can PHP Do?

• PHP can generate dynamic page content


• PHP can create, open, read, write, delete, and close files on the server
• PHP can collect form data
• PHP can send and receive cookies
• PHP can add, delete, modify data in your database
• PHP can be used to control user-access
• PHP can encrypt data

13
PHP Does Not…

• Handle client-side tasks such as creating a new browser window, adding


mouse overs, determining the screen size of the user’s machine, etc. Such
tasks are handled by JavaScript .
• Do anything within the Web browser until a request from the client has been
made (e.g., submitting a form, clicking a link, etc.).

14
Why Use PHP?
• PHP has been described as being “better, faster, and easier to learn than the
alternatives”
• PHP runs on various platforms (Windows, Linux, Unix, Mac OS , etc.)
• PHP is compatible with almost all servers used today (Apache, IIS, etc.)
• PHP supports a wide range of databases (MySQL, Informix, Oracle, Sybase, Solid,
PostgreSQL, Generic ODBC, Microsoft SQL Server etc)

• PHP is free. Download it from the official PHP resource.

15
Cont..

• Allows easy storage and retrieval of information from supported database.


• Accessibility: You can reach the internet from any browser, any device, anytime,
anywhere.
• Manageability: It does not require distribution of application code and it is easy
to change code.
• Security: The source code is not exposed. Once user is authenticated, can only
allow certain actions. It also allows encryption.
• Scalability: Web-based 3-tier architecture can scale out
How do I start PHP code?
• Run Your First PHP Script
 Download and Install XAMPP server.
 Open and start Apache and MySQL server .

 Go to XAMPP server directory. I'm using Windows, so my root server directory is C:\
xampp
 Open htdocs\.
 Create folder-IT2015(folder name)

 Install text editor (notepad, notepad++,sublime text…) , open and write code

 save a file as “lab1.php” under “IT2015” folder.


 Finally run the code by writing “localhost/foldername“ on the
PHP Basic Syntax
• What is PHP syntax?
• Start your PHP code with <?php and end it with ?>.
• Alternatively, you can use <? and ?>.
• Every PHP line of code ends with a semicolon(;).
• If most of the page content is HTML, the PHP code is usually embedded.
• PHP code typically resides in a plaintext file with a .php extension.
• The code itself is defined within PHP delimiters: <?php and end ?>.
PHP Basic Syntax

The PHP parsing engine needs a way to differentiate PHP code from other elements
in the page. The mechanism for doing so is known as ‘escaping to PHP.’ There are four
ways to do this:
1. Canonical PHP tags
The most universally effective PHP tag style is:
If you use this style, you can be positive that your tags will always be correctly interpreted.

2. Short-open (SGML-style) tags: Short tags are, as one might expect, the shortest option.
We must do one of two things to enable PHP to recognize the tags:
PHP Basic Syntax
 Choose the --enable-short-tags configuration option when building PHP.
 Set the short_open_tag setting in php.ini file to on. This option must be disabled to parse
XML with PHP because the same syntax is used for XML tags.

3. ASP-style tags: ASP-style tags mimic the tags used by Active Server Pages to delineate
code blocks. To use ASP-style tags, we should set the configuration option in your php.ini
file.

4. HTML script tags: HTML script tags look like this


Cont…
• The PHP interpreter considers anything outside of these delimiters as plaintext to
be sent to the client.
• A PHP scripting block can be placed anywhere in the document.

• For maximum compatibility, it is recommended to use the standard form rather


than the shorthand form : 21
PHP Output Statement
• As shown above PHP has different syntaxes but for maximum
compatibility, it is recommended to use <?php…?> .
• Each code line in PHP must end with a semicolon. The semicolon is a
separator and is used to distinguish one set of instructions from another.
• There are two basic statements to output text with PHP:

1. echo
2. print.

22
PHP Output Statement
• echo has no return value whereas print has a return value.
• The returned value represents whether the print statement is succeeded or not.
• If the print statement succeeds, the statement returns 1 otherwise 0.
• echo can take multiple parameters but print can only take one argument.
echo is marginally faster than print.
 The echo or print statement can be used with or without parentheses:

echo or echo().

23
Cont..
• The general format of the echo statement is as follows:
echo outputitem1,outputitem2,outputitem3, . . .;
echo (output statement);
The parameterized version of echo does not accept multiple arguments.
• The general format of the print statement is as follows:
print outputstatement;
print(outputstatement);
echo 123; //output: 123
echo/print
echo “Hello World!”; //output: Hello world!
echo (“Hello World!”); //output: Hello world!
echo “Hello”,”World!”; //output: Hello World!
echo Hello World!; //output: error, string should be enclosed in quotes
print (“Hello world!”); //output: Hello world!
• print (“Hello”, “world!”); //output: _______???

• print “Hello”, “world!”; //output: _______???

• echo (“Hello”, “World!”); //output: _______???


PHP Comments
• Comment is the portion of a program that exists only for the human reader and
stripped out before displaying the programs result.
• There are two commenting formats in PHP:
• Single-line comments: They are generally used for short explanations or notes
relevant to the local code. Here are the examples of single line comments.
Cont..

• Multi-lines comments: They are generally used to provide pseudo code


algorithms and more detailed explanations when necessary. The multiline
style of commenting is the same as in C. Here is the
example of multiline comments.
Continue …….

You might also like