CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Redirect URL as a variable

 



ros
Novice

Mar 3, 2003, 7:02 AM

Post #1 of 10 (664 views)
Redirect URL as a variable Can't Post

I am trying to redirect based on a variable in the script.

This is part of a shopping cart script which takes variables from the cart and shoves them into the checkout. Based upon the var $number I want to redirect to the appropriate page.

So far I get a blank as the variable should hold the file name:

#routine to determine link where $number comes from a shopping cart selection passed to the checkout.

if ($number eq"A4"){
$link="thankyou1.html";
}
if ($number eq"A8"){
$link="thankyou2.html";
}
if ($number eq"A16"){
$link="thankyou3.html";
}
if ($number eq"A1"){
$link="thankyou4.html";
}
if ($number eq"A2"){
$link="thankyou5.html";
}
if ($number eq"AL"){
$link="thankyou6.html";
}
if ($number eq"AP"){
$link="thankyou7.html";
}



### Print Thank You HTML.
print "Content-type: text/html\n\n";
open(THANKYOU,"$link");
print <THANKYOU>;
close(THANKYOU);



At the point that the data is still held in the checkout, the var $link has a value, but as soon as the "complete order" button is processed, the $link appears to go blank.


davorg
Thaumaturge / Moderator

Mar 3, 2003, 7:47 AM

Post #2 of 10 (658 views)
Re: [ros] Redirect URL as a variable [In reply to] Can't Post

Have you considered putting this lookup table into a hash. It could be far easier to change it that way.

Code
my %pages = (A4  => 'thankyou1.html', 
A8 => 'thankyou2.html',
A16 => 'thankyou3.html',
A1 => 'thankyou4.html',
A2 => 'thankyou5.html',
AL => 'thankyou6.html',
AP => 'thankyou7.html');

my $page = 'default.html'; # in case $number is invalid
$page = $pages{$number};


I'm not sure what you mean by "as soon as the "complete order" button is processed, the $link appears to go blank." Could you show us a bit more code to demonstrate the problem.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


ros
Novice

Mar 3, 2003, 8:33 AM

Post #3 of 10 (656 views)
Re: [davorg] Redirect URL as a variable [In reply to] Can't Post

Hmm,

Tried that (with and without the path to the SSL)

Sorry I must be a bit dozy here?? Any ideas??

here's a bit more of the script:



print "<INPUT TYPE=HIDDEN NAME='taxrate' VALUE=$FORM{taxrate}>";
print "<INPUT TYPE=HIDDEN NAME='shipcharge' VALUE=$FORM{shipcharge}>";

print "<INPUT TYPE=hidden NAME=bag Value='$FORM{bag}'>";


print "<INPUT TYPE='image' NAME='Send Order' BORDER=0 SRC='pay.gif'></FORM>";
print "<font size=5 color='red'>Upon completion of your payment, you will be able to enter your car advert</font>\n";



#######the value of $number is retained but dumps into bag

### Print footer HTML.
open(FOOTER, "footer3.html");
print <FOOTER>;
close(FOOTER);
}
else {
### Write order to file
$ordernum = time;
my $time = localtime(time());
$time =~s/\s+/_/g;


$filename = "$time.txt";
open(SECURE, "|/usr/local/bin/encryptit.pl $user>/path/$user/$filename");

print SECURE "ORDER NO. $ordernum\n\n";
print SECURE "ORDER DATE: $mydate\n\n";
print SECURE "Qty \tItem \tDescription \t \t \t Price\t Subtotal\n";
$total = 0;
foreach $item (@cart) {
($quantity,$number,$desc,$color,$size,$price) = split(/\|/, $item);

$quantity= $quantity." ";
$number= $number." ";

$desc= $desc." ";
$subtotal = $price*$quantity;
$total = $total + $subtotal;

$a = sprintf("%-4.4s\t", $quantity);
$b = sprintf("%-10.10s\t", $number);
$c = sprintf("%-40.40s\t", $desc);
$d = sprintf("%-10.10s\t", $color);
$e = sprintf("%-5.5s\t", $size);
$f = sprintf("%10.2f\t", $price);
$g = sprintf("%10.2f", $subtotal);

print SECURE "$a$b$c$d$e$f$g\n";
}
print SECURE "\nTax Rate: $FORM{taxrate} % \n";
$tax = $total * ($FORM{taxrate}/100);
$ptax = sprintf("Tax: %5.2f", $tax);
print SECURE "$ptax\n";
$total = $total + $tax;

$pship = sprintf("S&H: %5.2f", $FORM{shipcharge});
print SECURE "$pship\n";
$total = $total + $FORM{shipcharge};

$totl = sprintf("TOTAL: %5.2f", $total);
print SECURE "$totl\n\n";

print SECURE "Payment Method: $FORM{'method'}\n";
if ($FORM{'method'} eq "Credit Card") {
print SECURE "Credit Card: $FORM{'card'} Number: $FORM{'cardnumber'} Expires: $FORM{'expmonth'} $FORM{'expyear'}\n";
}
if ($FORM{'method'} eq "Debit Card") {
print SECURE "Debit Card: $FORM{'card'} Number: $FORM{'cardnumber'} Expires: $FORM{'expmonth'} $FORM{'expyear'} Issue number: $FORM{issueno}\n";
}

print SECURE "\n";
print SECURE "BILLING ADDRESS:\n";
print SECURE "$FORM{'bfname'} $FORM{'blname'}\n";
print SECURE "$FORM{'badd1'}\n";
if ($FORM{'badd2'} ne "") {
print SECURE "$FORM{'badd2'}\n";
}
print SECURE "$FORM{'bcity'} $FORM{'bstate'} $FORM{'bzip'}\n";
print SECURE "$FORM{'bcountry'}\n";
print SECURE "$FORM{'bemail'}\n";
print SECURE "Phone: $FORM{'bphone'}\n";
print SECURE "Fax: $FORM{'bfax'}\n\n";

print SECURE "COMMENTS\n";
print SECURE "$FORM{'comments'}\n";
print SECURE "-----------------------------------------------------------\n";
print SECURE "Remote IP address: $ENV{'REMOTE_ADDR'}\n";
close(SECURE);


### email notification of order
$to = $mymail;
$from = $mymail;
$reply = $mymail;
$smtp = $mysmtp;
$subject = 'Order Notification';
$message = "Filename: $filename\n";
$status = sendmail($from, $reply, $to, $smtp, $subject, $message );


### E-mail results to purchaser.
if ( $FORM{'bemail'} =~/\@/) {
$to = $FORM{'bemail'};
$smtp = $mysmtp;

$subject = 'Order Confirmation';
$from = $mymail;
$reply = $mymail;

$message = "Content-type: text/html\n\n";
$message = $message."ORDER NO. $ordernum\n\n";
$message = $message."Thank You! This is to confirm we will debit your card as follows your order as follows:\n\n";
$message = $message."Qty \tItem \tDescription \t \t \t Price\t Subtotal\n";
$total = 0;
foreach $item (@cart) {
($quantity,$number,$desc,$color,$size,$price) = split(/\|/, $item);
$quantity= $quantity." ";
$number= $number." ";
$desc= $desc." ";
$subtotal = $price*$quantity;
$total = $total + $subtotal;

$a = sprintf("%-4.4s\t", $quantity);
$b = sprintf("%-10.10s\t", $number);
$c = sprintf("%-40.40s\t", $desc);
$d = sprintf("%-10.10s\t", $color);
$e = sprintf("%-5.5s\t", $size);
$f = sprintf("%10.2f\t", $price);
$g = sprintf("%10.2f", $subtotal);

$message = $message."$a$b$c$d$e$f$g\n";
}
$message = $message."\nTax Rate: $FORM{taxrate} % \n";
$tax = $total * ($FORM{taxrate}/100);
$ptax = sprintf("Tax: %5.2f", $tax);
$message = $message."$ptax\n";
$total = $total + $tax;

$pship = sprintf("S&H: %5.2f", $FORM{shipcharge});
$message = $message."$pship\n";
$total = $total + $FORM{shipcharge};

$totl = sprintf("TOTAL: %5.2f", $total);
$message = $message."$totl\n\n";

$message = $message."Payment Method: $FORM{'method'}\n";

$message = $message."\n";
$message = $message."BILLING ADDRESS:\n";
$message = $message."$FORM{'bfname'} $FORM{'blname'}\n";
$message = $message."$FORM{'badd1'}\n";
if ($FORM{'badd2'} ne "") {
$message = $message."$FORM{'badd2'}\n";
}
$message = $message."$FORM{'bcity'} $FORM{'bstate'} $FORM{'bzip'}\n";
$message = $message."$FORM{'bcountry'}\n";
$message = $message."$FORM{'bemail'}\n";
$message = $message."Phone: $FORM{'bphone'}\n";
$message = $message."Fax: $FORM{'bfax'}\n\n";

$message = $message."COMMENTS\n";
$message = $message."$FORM{'comments'}\n";
$message = $message."-----------------------------------------------------------\n";
$status = sendmail($from, $reply, $to, $smtp, $subject, $message );
}

### Print a message to user placing the order
if ($status != 1) {
print "Content-type: text/html\n\n";
print "An error has occured while emailing your order<BR>";
print "Please email us and tell us you received an error code: $status<P>";
}
else {


###########this is your bit#################

#tried it as thankyou1.html and the fullpath

my %pages = (A4 => 'https://www.secure-website.com/myname/thankyou1.html',
A8 => 'https://www.secure-website.com/myname/thankyou2.html',
A16 => 'https://www.secure-website.com/myname/thankyou3.html',
A1 => 'https://www.secure-website.com/myname/thankyou4.html',
A2 => 'https://www.secure-website.com/myname/thankyou5.html',
AL => 'https://www.secure-website.com/myname/thankyou6.html',
AP => 'https://www.secure-website.com/myname/thankyou7.html');
my $page = 'https://www.secure-website.com/myname/thankyou.html'; # in case $number is invalid
$page = $pages{$number};

print "Content-type: text/html\n\n";
open(THANKYOU,"$page"); #is this how you call the URL?
print <THANKYOU>;
close(THANKYOU);

}
}
}


davorg
Thaumaturge / Moderator

Mar 4, 2003, 1:29 AM

Post #4 of 10 (648 views)
Re: [ros] Redirect URL as a variable [In reply to] Can't Post


In Reply To

Code
my %pages = (A4  => 'https://www.secure-website.com/myname/thankyou1.html', 
A8 => 'https://www.secure-website.com/myname/thankyou2.html',
A16 => 'https://www.secure-website.com/myname/thankyou3.html',
A1 => 'https://www.secure-website.com/myname/thankyou4.html',
A2 => 'https://www.secure-website.com/myname/thankyou5.html',
AL => 'https://www.secure-website.com/myname/thankyou6.html',
AP => 'https://www.secure-website.com/myname/thankyou7.html');
my $page = 'https://www.secure-website.com/myname/thankyou.html'; # in case $number is invalid
$page = $pages{$number};


The value in $page at this point will be a URL, not a file path.

In Reply To

Code
print "Content-type: text/html\n\n"; 
open(THANKYOU,"$page");


However here you pass $page to "open". "open" works on file paths and not URLs. You'll need to put the local path to the pages into your hash, not the URL.

Perl would have told you what was going on if you had asked it by checking the return value from "open".

Code
open(THANKYOU, $page) or die $!;


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


ros
Novice

Mar 4, 2003, 9:19 AM

Post #5 of 10 (643 views)
Re: [davorg] Redirect URL as a variable [In reply to] Can't Post

PirateOk I must be a very BIG bit thick here. I didn't realise you open was a var. How would I check the value of it ?

I have tried using a path rather than URL but neither a local path "thankyou.html" or "/home/httpd/html/secure/myname/thankyou.html" seems to work.

Is it because the secure server stops the call as a var but not if i use

open(THANKYOU, "thankyou.html
");

Oh suicide city is just around the corner..................



davorg
Thaumaturge / Moderator

Mar 4, 2003, 9:32 AM

Post #6 of 10 (641 views)
Re: [ros] Redirect URL as a variable [In reply to] Can't Post

It's not a variable, it's a function. But like many functions it returns a value indicating whether or not it succeeded.

I showed you how to test this in my last post.

Code
open(THANKYOU, $page) or die $!;

If "open" fails, then it returns false and the right operand to the "or" gets called. This kills the program and displays an error message (if you're writing a CGI program then that error message will be in your web server error log). Including the variable $! in the output from "die" will tell you exactly what the problem is.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


ros
Novice

Mar 4, 2003, 10:12 AM

Post #7 of 10 (640 views)
Re: [davorg] Redirect URL as a variable [In reply to] Can't Post

Sorry for being so thick.

The script I am running is on a shared secure server of which unfortunately I do not have access to the logs (it's a suck it and see job)

I have been testing variables by droping them into the script on screen so I can see the val of them.

I guess I could try chucking them into one of the emails it throws out before it dies.

I guess that even though I have been twiddling bits of perl for 3 or more years, I should really have posted as a beginner as I don't often get regular exposure to perl. I am able to modify many scripts and install most successfully, but some things are really foggy around the edges. Many apols for the ignorance.

Any good books you can suggest for a real simpleton?

I have a feeling that the secure server doesn't like the $page as it works when you actually type in the "thankyou.html" (in place of the var $page).

My logic (whats left of it) suggests that if the value of $page = thankyou.html as per the hash then why doesn't read it as so? Or am I clearly missing the point here?


davorg
Thaumaturge / Moderator

Mar 5, 2003, 2:00 AM

Post #8 of 10 (634 views)
Re: [ros] Redirect URL as a variable [In reply to] Can't Post


In Reply To
The script I am running is on a shared secure server of which unfortunately I do not have access to the logs (it's a suck it and see job)

It's a really bad idea to try and develop CGI programs when you can't see the error logs. I would consider that a basic requirement for a functional web server.

There are, however, ways around it. Try adding the code

Code
use CGI::Carp 'fatalsToBrowser';

to you program. That will then write any fatal errors to the browser window.


In Reply To
Any good books you can suggest for a real simpleton?


The Perl community's recommendations for books are all listed in perldoc perlfaq2. For learning Perl it basically bols down to Learning Perl if you have previous programming experience or Elements of Programming with Perl if you haven't. I also like Perl for Web Site Management which is more of a learners book than the title implies.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


(This post was edited by davorg on Mar 5, 2003, 2:44 AM)


Paul
Enthusiast

Mar 5, 2003, 2:40 AM

Post #9 of 10 (632 views)
Re: [davorg] Redirect URL as a variable [In reply to] Can't Post

>>
use CGI::Carp 'fatalsToBrowsers';
<<

That would be:

use CGI::Carp 'fatalsToBrowser';


davorg
Thaumaturge / Moderator

Mar 5, 2003, 2:46 AM

Post #10 of 10 (629 views)
Re: [Paul] Redirect URL as a variable [In reply to] Can't Post

Yep. Thanks. Fixed.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives