Alexander has asked for the wisdom of the Perl Monks concerning the following question:
I have been looking at possible ways to do it with Apache, but have not yet found one which worked. (I cannot change httpd.conf - i can only use Directives which specify they can be used in the .htaccess Context.) So far i have looked into using mod-rewrite to redirect all requests to the script and send the original request as a custom environment variable and then have the script redirect to the custom environment variable location once it did it's thing. I have also looked into setting the script as a handler, but so far that has not worked yet either.
I was hoping the below would cause setcart.pl to act on each .shtml page and set the initial cookie. It had an affect, but set no cookie. The affect was to render the SSI in-operable with this Action set in the .htaccess file.
setcart.pl does set a cookie when run as a stand-alone script. But I am trying to incorporate it with pages which are otherwise static. I am trying to avoid using perl as an index with:
index.pl?page=not&the;way=that/x=I+want:my~urls|to%be
When I want to allow for deep-linking within the site externally (or internally). I am trying to keep it:
/nicely/formated/urls/like/so.shtml
I had been using the London Perl Monger's version of cookie.lib, but had enough monk questions about why/what and objections of displeasure that I figured out how to do without it.#BEGIN .htaccess #I have tried setting it as a Handler # -> All this did was cause the SSI # to not work. Action set-cart /cart/setcart.pl AddHandler setcart .shtml # I have also tried using mod-rewrite, # sending all .shtml requests to the # script 1st, then back to their original # destination. #RewriteEngine on # pre-process all requests with setcart.pl # RewriteCond %{ENV: REQ_URL} !$1 [NC] #RewriteRule ^(.*)$ /cart/setcart.pl [E=REQ_URL:$1,NS,L] # (Ofcourse I tried one at a time, hence # the commented-out Rewrite. #END .htaccess #BEGIN setcart.pl #!/usr/bin/perl -w # perl error reporting switches # -c = check the code for syntax errors # -w = "use warnings" and tells the interpreter to produce useful d +iagnostics # -T = "taint mode" which adds security internal to Perl # use strict; use vars qw($script_name $query $template $raw_cookie $totalvalue $itemt $subt $grandt $ouncest $shippingt @Cookie &CookieCheck &GetCookies &SetCookies); use CGI qw/-no_xhtml :standard/; use CGI::Cookie; use CGI::Carp qw(fatalsToBrowser); $CGI::POST_MAX=1024 * 100; # max 100K posts $CGI::DISABLE_UPLOADS = 1; # no uploads ############### COOKIE STRATEGY ######################### # 4 cookies manage cart info # "total" is the main cookie set right away, it has # "itemt" = number of total items in cart # "subt" = total cost minus shipping cost and tax # "grandt" = total cost plus shipping cost and tax # "ouncest" = total shipping weight in ounces # "shippingt" = total cost of shipping for order # "itemprice" holds the numerical data of the order # "itemnum" = the item number itself # "itemqty" = the quantity requested for the item # "baseprice" = the retail price of the smallest item in its + set # "upsizeamt" = the cost added ontop the base price for larg +er items # "details" holds the descriptive information for a particular item # "isize" = the requested item size # "icolor" = the requested item color # "istyle" = the requested item style # "iounces" = the shipping weight for item in ounces # "itemtitle" holds the descriptive title of the requested items # "ititle" = the descriptive title of the requested items # Each cookie will have it's data packed into a semicolon seperated sc +alar # Note: I will later need to add handling for maximum cookie length to + branch # into multiple cookies if there is a very large order. ######################################################### ############### SCRIPT STARTS HERE ###################### $script_name = "setcart.pl"; $query = new CGI; &CookieCheck; sub CookieCheck { # check for default cookie "total" if (GetCookies('total')) { # if total exists, get it's current values for "itemt" and "gr +andt" my $rawvalue = pop(@Cookie); ($itemt, $subt, $grandt, $ouncest, $shippingt) = split(/;/, $r +awvalue); # This goes with the Rewrite attempt #print $query->redirect(-uri=>'$ENV{'REQ_URL'}'), "\n\n"; + } else { # Set total to default zero values # In this order: itemt, subt, grandt, ouncest, shippingt $totalvalue = "0;0.00;0.00;0;0.00;"; SetCookies('total',$totalvalue); # Now do a check to make sure the cookie was set if (GetCookies('total')) { # if total exists, get it's current values for "itemt" and + "grandt" my $rawvalue = pop(@Cookie); ($itemt, $subt, $grandt, $ouncest, $shippingt) = split(/;/ +, $rawvalue); # This goes with the Rewrite attempt #print $query->redirect(-uri=>'$ENV{'REQ_URL'}'), "\n\n"; } else { # Other wise redirect to "Cookies are Required" page print $query->redirect(-uri=>/cookiereq.html'), "\n\n"; } } } sub SetCookies { my (%input) = @_; while( my($name,$value) = each %input ) { my $c = CGI->cookie ( -name => $name, -value => $value); print "Set-Cookie: ", $c, "\n"; } } sub GetCookies { my @cookies = @_; my $exists = 0; foreach my $name (@cookies) { if (defined CGI->cookie($name)) { my $value = CGI->cookie($name); push(@Cookie,$name,$value); $exists = 1 if $value; } } return $exists; }
I am also aware of merlyn's take on using cookies like this, and I agree, but consider this as an exercise so I may learn the how's of some of the things I am trying to do here. And while the example used here is using cookies, I am sure there are other things which I (and other monks) may want to do with perl scripts that could be processed before the HTTP Headers of a static .html or .shtml page are served. (And please, dont bother recommending PHP.)
20030530 Edit by Corion: Added readmore tag
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Preprocessing with some.pl for some.shtml requests on Apache
by Alexander (Sexton) on May 30, 2003 at 23:53 UTC | |
|
Re: Preprocessing with some.pl for some.shtml requests on Apache
by BUU (Prior) on May 30, 2003 at 14:25 UTC |