12 Redirection CGI/Perl Programming By Diane Zak 1 12 Objectives • In this chapter, you will: • Include the Location header in a script • Include the Refresh header in a script 2 12 Introduction • Documents often move on the Web from one location to another – Link to new URL so user must click to go to new location – Automatically forward the browser to a new address • Immediately • After a period of time (seconds) – META tag – HTML – Location Header – script – Refresh Header - script 3 12 Redirecting a browser • Location header: – Immediately redirects the browser – Syntax: • print “Location: url\n\n”; – Example: print “Location: http://www.rsp.com/public_html/chap12/error.html\n\n”; • The 1st newline character identifies the end of the Location header • The 2nd newline character inserts a blank line below the header to signal end of header section • Location header used instead of Content-type header 4 12 Redirecting a Browser • Refresh header: – Waits a specified number of seconds before redirection – Syntax: • print “Refresh: seconds; URL=url\n”; – Example: print “Refresh: 3; URL=“http://www.rsp.com/public_html/chap12/error.html\n”; print “Content-type: text/html\n\n”; • Contains one newline character because it is used with the Content-type header 5 Using a Refresh Header 12 6 12 Using a Refresh Header • Uses Refresh header to redirect the page in 3 seconds 7 12 Using a Location Header 8 12 Using a Location Header If no Name was typed into the cara1.html page 9 Summary 12 • You can use the Location header or the Refresh header in a CGI script to redirect the user’s browser to another location. • The Location header redirects the browser immediately. – Syntax: print “Location: url\n\n”; • The Refresh header waits for a specified number of seconds until redirection. – Syntax: print “Refresh: seconds; URL=url\n”; – Is used together with the Content-type header 10