WEB DYNAMIC CONTENT SWU, Computer systems and technologies
Transcript
Slide 1
SWU, Computer systems and technologies
Slide 2
The Objective of This Lecture To give you a very high-level
overview of some of the tools for Web Programming Use several
typical tools to show the basics of Web Programming Not meant for
advanced Web developers The next in-class tutorial will be on DB
Programming We will assume minimal interaction with DBMS in this
lecture to focus on the Web programming part
Slide 3
Dynamic Content We are all used to fetching pages from a Web
server. Most are prepared by a human and put on the Web site for
others to read. Each reader sees the same page. The Web browser
(client) can perform client-side computations on the page to
present it differently. And the content can be dynamic via
Javascript code in the page that operates as the page is being
rendered (e.g. layout) and as it is being viewed (e.g. mouse
operations) We, however, are interested in server-side dynamic
content.
Slide 4
Server Side Coding CGI (standard protocol for server-client
communication) CGI PHP (open source) PHP ASP.NET (Microsoft
proprietary) ASP.NET JSP (JavaServer Pages) JSP Python, e.g. Django
(web framework) (open source) PythonDjango (web framework) Ruby,
e.g. Ruby on Rails (open source) RubyRuby on Rails
Slide 5
Server-side Dynamic Pages A request to the Web server returns a
new, machinegenerated page, e.g. google search result page,
travelocitys flight purchase page,... The server receives the
request and executes some code to create a new HTML document (or
other type) and returns that. What makes this interesting is that
the request can specify inputs that govern how the page is created.
This is very similar to calling an R function with different inputs
and generating output in the form of an HTML page
Slide 6
Client Side Coding HTML stands for HyperText Markup Language,
and is the predominant markup language for web pages. Javascript
Formally called EMCAScript, Javascript is a ubiquitous client side
programming tool, often implemented as part of a web browser in
order to provide enhanced user interfaces and dynamic websites. Its
typically used to enable programmatic access to computational
objects within a host environment. Applet and ActiveX.
AppletActiveX Applet (e.g. Java Applet): small application that
performs one specific task, sometimes running within a larger
program, perhaps as a plug-in. AJAX (Asynchronous Javascript And
XML) AJAX Group of technologies that provides new methods of using
Javascript, PHP and other languages to improve the user
experience
Slide 7
Client-Server components One application acts as a client and
connects to the server and sends a request; the server sends a
reply. Our client is a browser and specifically an HTML form that
provides ways to specify inputs to the server. Our server is an R
script on the Web server that gets the inputs and generates an HTML
page based these inputs. To provide dynamic access to R
functionality via the Web, you need to create both pieces.
Slide 8
Transactions The web is (as we already know) based on HTTP,
which is a simple request/response protocol. A browser requests a
web page, and the server supplies it. That's it. These events
together constitute a single web transaction. Unfortunately, a
transaction-based system is not, on its own, sufficiently powerful
to create the kinds of web- based systems that are needed for
modern applications.
Slide 9
The "welcome.php" file can now use the $_POST variable to catch
the form data (notice that the names of the form fields will
automatically be the ID keys in the $_POST array): Welcome. You are
years old! The $_POST Variable
Slide 10
Why Use $_POST Variables sent with HTTP POST are not shown in
the URL Variables have no length limit However, because the
variables are not displayed in the URL, it is not possible to
bookmark the page. The PHP $_REQUEST variable contains the contents
of both $_GET, $_POST, and $_COOKIE. The PHP $_REQUEST variable can
be used to get the result from form data sent with both the GET and
POST methods. Welcome. You are years old!
Slide 11
What is a Cookie? A cookie is often used to identify a user. A
cookie is a small file that the server embeds on the user's
computer. Each time the same computer requests a page with a
browser, it will send the cookie too. With PHP, it can both
creating and retrieving cookie values. The setcookie() function is
used to set a cookie. The setcookie() function must appear BEFORE
the tag. Setcookie (name, value, expire, path, domain) ;
Slide 12
How to Create a Cookie? - Example In the example below, we will
create a cookie named "user" and assign the value "Alex Ivanov" to
it. We also specify that the cookie should expire after one
hour:
Slide 13
How to Retrieve a Cookie Value? The PHP $_COOKIE variable is
used to retrieve a cookie value. In the example below, we retrieve
the value of the cookie named "user" and display it on a page:
Slide 14
How to Retrieve a Cookie Value? In the following example we use
the isset() function to find out if a cookie has been set:
Slide 15
How to Delete a Cookie? When deleting a cookie you should
assure that the expiration date is in the past. Delete
example:
Slide 16
PHP Session Variables A PHP session is used to store user
information on the server for later use (i.e. username, shopping
items, etc). However, session information is temporary and will be
deleted after the user has left the website. If you need a
permanent storage you may want to store the data in a database.
Sessions work by creating a unique id (UID) for each visitor and
store variables based on this UID. The UID is either stored in a
cookie or is propagated in the URL.
Slide 17
Starting a PHP Session Before you can store user information in
your PHP session, you must first start up the session. Note: The
session_start() function must appear BEFORE the tag: The code above
will register the user's session with the server, allow you to
start saving user information, and assign a UID for that user's
session.
Slide 18
Storing a Session Variable The correct way to store and
retrieve session variables is to use the PHP $_SESSION variable:
Output: Pageviews = 1
Slide 19
Storing a Session Variable In the example below, we create a
simple page-views counter. The isset() function checks if the
"views" variable has already been set. If "views" has been set, we
can increment our counter. If "views" doesn't exist, we create a
"views variable, and set it to 1:
Slide 20
Destroying a Session If you wish to delete some session data,
you can use the unset() or the session_destroy() function. The
unset() function is used to free the specified session variable:
You can also completely destroy the session by calling the
session_destroy() function: