943,808 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 4639
  • Perl RSS
Mar 5th, 2009
0

Perl Post Redirect

Expand Post »
I have created a simple HTML and Perl test to figure out how to properly get a browser to re-Post form data to a new URL. The raison d'etre for this test is to figure out the best way to re-Post the shopping cart order form data to PayPal after my Perl script has saved the order etc information to a database.

The test consists of three simple files: 1) an HTML file containing a form, 2) a Perl script called by the form which performs the redirection to 3) a Perl script which displays the form data.

Where I am at now is when I only use
print ("Location: http://briarhilldesign.com/cgi-bin/showdata.pl\n");
print ("Content-type: text/html\n\n");
I get redirected to the final Perl script but with no data. When I also use an HTTP redirection status line, e.g., print("HTTP/1.0 303 See Other\n"); I get an Internal Server Error:

Quote ...
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster@briarhilldesign.torontogardenbook.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Apache/2.2.11 (Unix) mod_ssl/2.2.11 OpenSSL/0.9.8i DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 Server at briarhilldesign.com Port 80
Here is the code for the three files.

redirect.html
Perl Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.  
  4. <head>
  5. <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
  6. <title>HTML redirect test</title>
  7. <style>
  8. input#submit {
  9. display: block;
  10. }
  11. </style>
  12. </head>
  13.  
  14. <body>
  15. <form id="redirect-form" action="http://briarhilldesign.com/cgi-bin/redirect.pl" method="post">
  16. <fieldset>
  17. <legend>Data to redirect</legend>
  18. <label for="first_name">First Name</label><input id="first_name" name="first_name" value="John" />
  19. <label for="last_name">Last Name</label><input id="last_name" name="last_name" value="Smith" />
  20. <h3>Choose the redirect method:</h3>
  21. <input type="radio" id="none" name="redirect-method" value="none" /><label for="location">No code</label>
  22. <input type="radio" id="300" name="redirect-method" value="300" /><label for="300 multiple choices">300 multiple choices</label>
  23. <input type="radio" id="301" name="redirect-method" value="301" /><label for="301 moved permanently">301 moved permanently</label>
  24. <input type="radio" id="302" name="redirect-method" value="302" /><label for="302 found">302 found</label>
  25. <input type="radio" id="303" name="redirect-method" value="303" /><label for="303 see other">303 see other</label>
  26. <input type="radio" id="307" name="redirect-method" value="307" /><label for="307 temporary redirect">307 temporary redirect</label>
  27. <input type="submit" id="submit"/>
  28. </fieldset>
  29. </form>
  30. </body>
  31.  
  32. </html>
In this HTML file one chooses from six possible kinds of redirection.


redirect.pl
Perl Syntax (Toggle Plain Text)
  1. #!/usr/local/bin/perl -w
  2. # redirect.pl -- redirects to showdata.pl using the indicated method
  3. BEGIN {my $homedir = ( getpwuid($>) )[7];my @user_include;foreach my $path (@INC) { if ( -d $homedir . '/perl' . $path )
  4. {push @user_include, $homedir . '/perl' . $path;}} unshift @INC, @user_include;}
  5. use warnings;
  6. use strict;
  7. use CGI qw(:standard);
  8. use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
  9.  
  10. my $form1 = new CGI;
  11. my $RedirectMethod = $form1->param('redirect-method');
  12.  
  13. for ($RedirectMethod) {
  14. if (/none/) {} # No code sent
  15. if (/300/) {print("HTTP/1.0 300 Multiple Choices\n");}
  16. if (/301/) {print("HTTP/1.0 301 Moved Permanently\n");}
  17. if (/302/) {print("HTTP/1.0 302 Found\n");}
  18. if (/303/) {print("HTTP/1.0 303 See Other\n");}
  19. if (/307/) {print("HTTP/1.0 307 Temporary Redirect\n");}
  20. }
  21.  
  22. # Rest of HTTP Response
  23. print ("Location: http://briarhilldesign.com/cgi-bin/showdata.pl\n");
  24. print ("Content-type: text/html\n\n");
  25.  
  26. # TRY am HTML body here later, if necessary
  27. print q~<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  28. <html xmlns="http://www.w3.org/1999/xhtml">
  29.  
  30. <head>
  31. <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
  32. <title>You are being redirected</title>
  33. </head>
  34.  
  35. <body>
  36. <h1>Redirection</h1>
  37. <p>You are being redirected. Version 11.</p>
  38. </body>
  39. </html>
  40. ~;
This Perl script performs one of six different kinds of redirection depending on the value of the form variable 'redirect-method'.


showdata.pl
Perl Syntax (Toggle Plain Text)
  1. #!/usr/local/bin/perl -w
  2. # showdata.pl -- displays form data
  3. BEGIN {my $homedir = ( getpwuid($>) )[7];my @user_include;foreach my $path (@INC) { if ( -d $homedir . '/perl' . $path )
  4. {push @user_include, $homedir . '/perl' . $path;}} unshift @INC, @user_include;}
  5. use warnings;
  6. use strict;
  7. use CGI qw(:standard);
  8. use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
  9.  
  10. my $form1 = new CGI;
  11. my $FirstName = $form1->param('first_name');
  12. my $LastName = $form1->param('last_name');
  13. my $RedirectMethod = $form1->param('redirect-method');
  14.  
  15.  
  16. print (qq~Content-Type: text/html
  17.  
  18. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  19. <html xmlns="http://www.w3.org/1999/xhtml">
  20.  
  21. <head>
  22. <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
  23. <title>HTML redirect test</title>
  24. </head>
  25.  
  26. <body>
  27. <h1>Data received</h1>
  28. <p>First Name: $FirstName</p>
  29. <p>Last Name: $LastName</p>
  30. <p>Redirect Method: $RedirectMethod</p>
  31. </body>
  32.  
  33. </html>
  34. ~);
This Perl script displays the form data passed to it.


Any help getting the HTTP status line to work so I can redirect and induce the browser to re-Post the data it has just posted would be greatly appreciated.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Silver Snail is offline Offline
6 posts
since Sep 2006
Mar 5th, 2009
1

Re: Perl Post Redirect

You can't get the data sent to redirect.pl parsed by showdata.pl the way you are trying.

You have to recieve the data in your redirect.pl script and save it to disk and have showdata.pl open the file and get the data from a file or send the data to showdata.pl the same way the html form does, using POST or GET methods. You can use a module like LWP::UserAgent to send data from one CGI script to another. But if both scripts run on the same server you probably want to look into CGI::Sessions or maybe there are newer modules by now.
Last edited by KevinADC; Mar 5th, 2009 at 6:14 pm.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Mar 5th, 2009
0

Re: Perl Post Redirect

There are also a number of paypal modules on CPAN.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Mar 6th, 2009
0

Re: Perl Post Redirect

Thank you very much, KevinADC. I will look at those CPAN PayPal modules.

I had been using LWP::UserAgent to Post data but I am ultimately doing this test to prepare a system for PayPal. What I found was Posting with LWP::UserAgent and printing the returned HTML results leaves my Perl script's URL in the browser address bar instead of the PayPal URL. That's why I was trying to use a redirect.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Silver Snail is offline Offline
6 posts
since Sep 2006
Mar 6th, 2009
0

Re: Perl Post Redirect

Maybe someone else will have a suggestion. I think I have offered all I can at this point.

Good luck
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Mar 11th, 2009
0

Re: Perl Post Redirect

It would seem to me that you would want a server process to post to paypal and have your client process wait for notification that the paypal server transaction completed. That's what I would do. The server process, which could run as a script that is CRONed, but it would be better IMO to have an internal listener that waits for your client to connect and requests the paypal transaction. You server would talk to paypal, get status and reply to your client program, which would in turn contact the browser with status. You can do this in any language and perhaps you can use the CPAN paypal modules on the server side and the LWP modules on the client side, either re-loading the page or making the browser wait until the status is returned.

I'm not trying to be a pain as an architect here, especially since you have already poured the foundation, but I think you might get more consistent results from an (internal) client/server setup.
Reputation Points: 26
Solved Threads: 38
Posting Whiz in Training
mitchems is offline Offline
293 posts
since Feb 2009
Mar 11th, 2009
0

Re: Perl Post Redirect

PayPall has all sorts of APIs, the PayPal site is where they can be found.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Mar 12th, 2009
0

Re: Perl Post Redirect

Thank you mitchems for that detailed suggestion. I must admit some of the technologies you mentioned I'm not familiar with.

As KevinADC mentions, and I am learning, PayPal has SOAP APIs that allow a developer to process credit cards from his own html page. This is something along the lines your were describing I think, mitchems.

So I think that is what I will do. I will learn the PayPal SOAP stuff and process credit cards and PayPal payments as part of my own checkout form. This seems to be the most elegant approach. A good skill to have that I can use on many sites once I know it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Silver Snail is offline Offline
6 posts
since Sep 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: Restrict random numbers genrated by rand()
Next Thread in Perl Forum Timeline: Perl script to organize files in directory structure





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC