CGI Script example: redirection script

 

Most redirection scripts are called from regular html pages.  So let’s say you put a form in a page that looks like this:

 

<html>

<body>

<h2>Redirection Form</h2>

<p>Choose where you want to go:</p>

<form action=”/cgi-bin/redirect.cgi” method=”get”>

<select name=”location”>

<option value=”/somedirectory/somepage1.html”>Some Page 1

<option value=”/somedirectory/somepage2.html”>Some Page 2

<option value=”/somedirectory/somepage3.html”>Some Page 3

<option value=”/somedirectory/somepage4.html”>Some Page 4

<submit value=”Go there!”>

</form>

</body>
</html>

 

You could then write a cgi script to forward the user there like this:

 

Remember, don’t type in the line numbers!  They’re only included for reference.

 

1     #!/usr/bin/perl –w

2

3     use strict;

4     use CGI::Carp qw(:fatalsToBrowser);

5     use CGI qw(:standard);

6     print redirect(param(‘location’));

 

Line 1: Initialize the perl interpreter to run our program, and pass it the warn flag so we get good errors for debugging.

Line 2: Always put an empty line after calling perl.

Line 3: Enable more useful error messages for debugging.

Line 4: Load the module that turns cryptic 500 Internal Server Errors into useful error messages we can read in a web browser.

Line 5: Load the CGI module and import it’s functions, which saves us tons of time and effort in writing cgi scripts.

Line 6: Call the CGI.pm function redirect to redirect to another URL.  In this case, we simply redirect to the url you specified in your form.

 


<-- Previous: CGI Script example: basic form emailer (line by line code explanation) | Next: CGI Script example: tell a friend script (line by line code explanation) -->


Please rate this cgi tutorial on cgi-resources.com:

CGI-Resources Rating:


Copy the Shrug Emoji
Ecommerce Shopping Cart Software
ShopCMS Paypal Shopping Cart
Free CGI Scripts
CGI Tutorial
Software Engineering Consultant
Search Engine Optimization Tips
How To Choose Quality Web Hosting
Free Search Engine Ranking Software
HTTP Compression
Install CGI Scripts
Tell A Friend Script
LittleFTP Free FTP Client For Windows


Link To
This Page!


Copy the following code and paste it into your html file.
Ecommerce Shopping Cart Software | ShopCMS Paypal Shopping Cart | Software Engineering Consultant | Free Search Engine Ranking Software | HTTP Compression | Install CGI Scripts | Search Engine Optimization Tips | CGI Tutorial | CGI Scripts | How To Choose Quality Web Hosting | Tell A Friend Script