Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Detour: Redirection

May 10, 1999

Not that it's rude or anything, but sometimes you just need to send the user to some other page. It's for their own good. Perhaps their form input contained an error and you have a standard error page you'd like to send them to. Or, perhaps they submitted some data which requires you to send them off to one of several possible pages; for instance, a submitted ZIP code might return one of three possible pages depending on whether your CGI script determines their location in the East, Central, or Western time zones.

The CGI object provides a method named redirect(), which accepts a URL string. As you probably infer, this method causes the user's browser to be redirected to the named URL.

print $cgiobject->redirect('http://www.somesite.com/page2.html');

Notice that the redirect() call is part of a print statement -- this is because this method produces a redirection header which is sent to the browser as output. Also note that the destination URL should be a fully qualified URL, as seen in the example, not simply a relative path such as "page2.html".

The main cause of confusion in using this redirect concerns the fact that it produces a header. Remember earlier that our Perl scripts often contained the line:

print $cgiobject->header;

But if you output a normal header to the page, you cannot then output a redirect! This means that you cannot output some HTML content to the page and then redirect it to another URL. The redirect has to be the first and only output sent to the page. So, put very briefly, our ZIP code-based redirect might look like:

#read submitted ZIP code and redirect to appropriate page
use CGI;
$cgiobject=new CGI;


$userZIP=$cgiobject->param("ZIP");
$redirURL="http://www.mysite.com/timezone-";


$redirURL.=timeZone(ZIP);
#Note: timeZone(ZIP) is a fictional function which returns a string
#representing the timezone given the ZIP, such as "EST" or "PST".

print $cgiobject->redirect($redirURL);

Again notice that we did not output a normal header ($cgiobject->header) in this page, or the redirect would have failed to work properly.

The natural follow-up question: "How do I redirect the page after having output some content?" The answer is that you don't, not using CGI itself. What you can do, if the browser supports JavaScript, is output some JavaScript to the page which in turn redirects the user's browser. In this case, you would output the normal header, since the JavaScript output is exactly like outputting any HTML content to the page. In this example, we output some text using the traditional CGI means and then output some JavaScript which, once it hits the browser, will cause the browser to redirect to a given URL.

#read submitted ZIP code and redirect to appropriate page
use CGI;
$cgiobject=new CGI;


print $cgiobject->header;
print "<html><body>".
      "Thanks for your submission ... ".
      "I will now send you to the main page.".
      "If your browser doesn't go anywhere, click ".
      "<a href=\"http://www.mysite.com/main.html\">here".
      "</a>";


print "<script language='JavaScript'>".
      "window.location='http://www.mysite.com/main.html'".
      "</script></body></html>";
 

The neat thing is, to create the above style of redirect, we've used one programming language -- Perl -- to write code in another programming language -- JavaScript!

A Peek at Environment Variables
The Perl You Need to Know
Conclusion


Up to => Home / Authoring / Languages / Perl / PerlfortheWeb




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers