CSC 2720 Building Web Applications Getting and Setting HTTP Headers (With PHP Examples)

Slides:



Advertisements
Similar presentations
TCP/IP Protocol Suite 1 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 22 World Wide Web and HTTP.
Advertisements

HTTP HyperText Transfer Protocol. HTTP Uses TCP as its underlying transport protocol Uses port 80 Stateless protocol (i.e. HTTP Server maintains no information.
HTTP – HyperText Transfer Protocol
CS320 Web and Internet Programming Generating HTTP Responses
16-Jun-15 HTTP Hypertext Transfer Protocol. 2 HTTP messages HTTP is the language that web clients and web servers use to talk to each other HTTP is largely.
HTTP Hypertext Transfer Protocol. HTTP messages HTTP is the language that web clients and web servers use to talk to each other –HTTP is largely “under.
How the web works: HTTP and CGI explained
What’s a Web Cache? Why do people use them? Web cache location Web cache purpose There are two main reasons that Web cache are used:  to reduce latency.
The World Wide Web and the Internet Dr Jim Briggs 1WUCM1.
Definitions, Definitions, Definitions Lead to Understanding.
Hypertext Transfer Protocol Information Systems 337 Prof. Harry Plantinga.
2/9/2004 Web and HTTP February 9, /9/2004 Assignments Due – Reading and Warmup Work on Message of the Day.
Hypertext Transport Protocol CS Dick Steflik.
CSC 2720 Building Web Applications Servlet – Getting and Setting HTTP Headers.
Nikolay Kostov Telerik Corporation
 What is it ? What is it ?  URI,URN,URL URI,URN,URL  HTTP – methods HTTP – methods  HTTP Request Packets HTTP Request Packets  HTTP Request Headers.
PL-IV- Group A HTTP Request & Response Header
FILE UPLOADS CHAPTER 11. THE BASIC PROCESS 1.The HTML form displays the control to locate and upload a file 2.Upon form submission, the server first stores.
Web technologies and programming cse hypermedia and multimedia technology Fanis Tsandilas April 3, 2007.
Web Server Design Week 5 Old Dominion University Department of Computer Science CS 495/595 Spring 2010 Martin Klein 2/10/10.
Java Technology and Applications
SUNY Polytechnic Institute CS 490 – Web Design, AJAX, jQuery Web Services A web service is a software system that supports interaction (requesting data,
Krerk Piromsopa. Web Caching Krerk Piromsopa. Department of Computer Engineering. Chulalongkorn University.
HTTP Protocol Specification
Web Caching: Replication on the World Wide Web Jonathan Bulava CSC8530 – Distributed Systems Dr. Paul Schragger.
FTP (File Transfer Protocol) & Telnet
HyperText Transfer Protocol (HTTP).  HTTP is the protocol that supports communication between web browsers and web servers.  A “Web Server” is a HTTP.
CP476 Internet Computing Lecture 5 : HTTP, WWW and URL 1 Lecture 5. WWW, HTTP and URL Objective: to review the concepts of WWW to understand how HTTP works.
TCP/IP Protocol Suite 1 Chapter 22 Upon completion you will be able to: World Wide Web: HTTP Understand the components of a browser and a server Understand.
2: Application Layer1 CS 4244: Internet Software Development Dr. Eli Tilevich.
Application Layer 2 Figures from Kurose and Ross
Rensselaer Polytechnic Institute Shivkumar Kalvanaraman, Biplab Sikdar 1 The Web: the http protocol http: hypertext transfer protocol Web’s application.
Maryam Elahi University of Calgary – CPSC 441.  HTTP stands for Hypertext Transfer Protocol.  Used to deliver virtually all files and other data (collectively.
Copyright (c) 2010, Dr. Kuanchin Chen1 The Client-Server Architecture of the WWW Dr. Kuanchin Chen.
Sistem Jaringan dan Komunikasi Data #9. DNS The Internet Directory Service  the Domain Name Service (DNS) provides mapping between host name & IP address.
WWW, HTTP, GET, POST, Cookies Svetlin Nakov Telerik Corporation
1 Introductory material. This module illustrates the interactions of the protocols of the TCP/IP protocol suite with the help of an example. The example.
Proxy Lab Recitation I Monday Nov 20, 2006.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
1 HTTP EECS 325/425, Fall 2005 September Chapter 2: Application layer r 2.1 Principles of network applications m app architectures m app requirements.
Web Server Design Week 8 Old Dominion University Department of Computer Science CS 495/595 Spring 2010 Martin Klein 3/3/10.
HyperText Transfer Protocol (HTTP) RICHI GUPTA CISC 856: TCP/IP and Upper Layer Protocols Fall 2007 Thanks to Dr. Amer, UDEL for some of the slides used.
HTTP1 Hypertext Transfer Protocol (HTTP) After this lecture, you should be able to:  Know how Web Browsers and Web Servers communicate via HTTP Protocol.
Web Server Design Week 4 Old Dominion University Department of Computer Science CS 495/595 Spring 2010 Martin Klein 2/03/10.
CIS679: Lecture 13 r Review of Last Lecture r More on HTTP.
1-1 HTTP request message GET /somedir/page.html HTTP/1.1 Host: User-agent: Mozilla/4.0 Connection: close Accept-language:fr request.
Web Server Design Assignment #2: Conditionals & Persistence Due: 02/24/2010 Old Dominion University Department of Computer Science CS 495/595 Spring 2010.
Appendix E: Overview of HTTP ©SoftMoore ConsultingSlide 1.
Operating Systems Lesson 12. HTTP vs HTML HTML: hypertext markup language ◦ Definitions of tags that are added to Web documents to control their appearance.
2: Application Layer 1 Chapter 2: Application layer r 2.1 Principles of network applications  app architectures  app requirements r 2.2 Web and HTTP.
CITA 310 Section 2 HTTP (Selected Topics from Textbook Chapter 6)
Web Server Design Week 7 Old Dominion University Department of Computer Science CS 495/595 Spring 2010 Martin Klein 2/24/10.
Web Technologies Lecture 1 The Internet and HTTP.
HTTP Here, we examine the hypertext transfer protocol (http) – originally introduced around 1990 but not standardized until 1997 (version 1.0) – protocol.
27.1 Chapter 27 WWW and HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
EE 122: Lecture 21 (HyperText Transfer Protocol - HTTP) Ion Stoica Nov 20, 2001 (*)
Overview of Servlets and JSP
LURP Details. LURP Lab Details  1.Given a GET … call a proxy CGI script in the same way you would for a normal CGI request  2.This UDP perl.
COMP2322 Lab 2 HTTP Steven Lee Jan. 29, HTTP Hypertext Transfer Protocol Web’s application layer protocol Client/server model – Client (browser):
WEB1P webarch1 Web architecture Dr Jim Briggs. WEB1P webarch2 What is the web? Distributed system Client-server system Characteristics of clients and.
INTRODUCTION Dr Mohd Soperi Mohd Zahid Semester /16.
© Janice Regan, CMPT 128, Jan 2007 CMPT 371 Data Communications and Networking HTTP 0.
Web Server Design Week 6 Old Dominion University Department of Computer Science CS 495/595 Spring 2006 Michael L. Nelson 2/13/06.
What’s Really Happening
HTTP – An overview.
Hypertext Transfer Protocol
Hypertext Transfer Protocol
Hypertext Transfer Protocol
Kevin Harville Source: Webmaster in a Nutshell, O'Rielly Books
Hypertext Transfer Protocol
Presentation transcript:

CSC 2720 Building Web Applications Getting and Setting HTTP Headers (With PHP Examples)

Outline  What kinds of data are embedded in the HTTP request/response headers?  How useful could these data be?  What can we achieve by setting HTTP response header?  PHP APIs for getting headers from HTTP request  PHP APIs for setting HTTP response headers  Examples

Introduction  The data in the HTTP header contains info about client/server and the data embedded in the HTTP body. HTTP/ OK Date: Mon, 23 May :38:34 GMT Server: Apache/ (Unix) (Red-Hat/Linux) Last-Modified: Wed, 08 Jan :11:55 GMT Etag: "3f80f-1b6-3e1cb03b" Accept-Ranges: bytes Content-Length: 438 Connection: close Content-Type: text/html; charset=UTF-8 Body of the contents goes here … The header section of a HTTP response

HTTP Request Headers  You can find out more about your client.  For examples  accept, accept-encoding, accept-language, accept-charset : Content types, compression schemes, languages, and character sets that the web client can handle.  user-agent : Info about the web client and operating system  referer : The URL of the webpage that "brings" the client to the requested page  cookie : Cookies

Obtaining HTTP Header Fields  Header fields of the current request is stored in $_SERVER.  For a complete list of predefined names, please refer to  Some of the useful info include  $_SERVER['HTTP_USER_AGENT']  Contains info about the web client  $_SERVER['HTTP_REFERER']  URL of the page (if any) which referred the web client to the current page. (May not be reliable)  Cookies should be access through $_COOKIE instead.

HTTP Response Headers  You can modify the HTTP response header to  Redirect the web client to another URL  Send a different HTTP status code  Tell the web client whether to cache the current document or not  Tell web client what language is used in the current document  Change the content type of the current document  You can use PHP to dynamically create text file, CSV file, image, etc.  Requesting the web client to download another file.  Set cookies (but in PHP, cookies should be set through $_COOKIE instead.)

Examples of HTTP 1.1 Response Headers  Cache-Control  Tells all caching mechanisms from server to client whether they may cache this object.  To tells a client not to cache the requested resource, set the its value to no-cache.  Content-Language  The language the content is in  Content-Type  The MIME type of the content being returned

Examples of HTTP 1.1 Response Headers  Expires  The time at which document should be considered as out- of-date  Last-Modified  The time in which the requested resource was last modified.  Location  To redirect the web client to a new URL  Set-Cookie  The cookies that browser should remember.

Functions for Dealing with Header Fields in the HTTP Response  header()  Set a raw HTTP header  headers_list()  Return a list of headers to be sent to the web client  The list is a numerically indexed array  headers_sent()  Return FALSE if no HTTP headers have already been sent or TRUE otherwise

<?php header('Location: exit(); // Return immediately ?> Redirecting the web client to another URL  header() must be called before any actual output is sent! When a web client access the above file, it will go to immeidately. <?php header('Location: exit(); ?> Even with only one empty space sent, subsequent calls to header() will fail.

<?php header("HTTP/ Not Found"); ?> Send a different HTTP status code Creating a custom-made page to display the error message when a requested resource cannot be found by the server. You also need to configure the web server to use your custom-made page.

<?php // HTTP/1.1 header("Cache-Control: no-cache, must-revalidate"); ?> Tell the web client whether to cache the current document or not For the format of HTTP header fields, please refer to HTTP/1.1: Header Field Definitions The above example is extracted from:

<?php $imagename = 'test.jpg'; $info = getimagesize($imagename); $fs = filesize($imagename); // Setting the mime-type of the file header("Content-type: {$info['mime']}\n"); // Send as attachment (request client to download) header("Content-Disposition: attachment;". " filename=\"$imagename\"\n"); header("Content-Length: $fs\n"); readfile("$imagename"); ?> Requesting the web client to download a file

References  Wiki: List of HTTP headers   HTTP/1.1: Header Field Definitions   PHP Manual 