Hope everyone on the list had a happy holidays & great new year ..
I recently got some requirements for a new project that involved a shopping
cart application. Aside from building the standard shopping cart portion of
the site (all C::A driven .cgi files), the site needed other regular .html
pages to display a running total of the current shopping contents.
Without getting into too much server wizardry, changing my programming
paradigm completely or switching to .php I came across what I believe to be
a fairly simple solution that draws on the strengths of php's inline parsing
combined with all of the C::A code I already had written for session
management, configuration, templating, etc.
Anyways, I'm providing my solution here both in the hopes that it might help
other people with similar requirements and so that I can receive feedback
from other people as to whether or not they've been faced with a similar
challenge, how they solved it and whether or not they can improve on what
I've done.
Caveat:
First of all, I'll point out that the majority of the sites I build use php
in some minor capacities (creating images on the fly, simple varaible test
functionality, etc), therefore I've configured apache to parse all .html
files as .php files. Just so that on the occasions when I need to use php, I
don't have to run around renaming file extensions. That being said, it isn't
a neccesity for anyone to do the same to get this solution to work, it is
however the reason why php will run in a standard .html file on my server.
Solution:
The first line (above <html>) of any page that needs to call this "Inline
C::A" looks like:
<?php include $_SERVER['DOCUMENT_ROOT'] . '/inc/ca_inline.php'; ?>
The contents of the /inc/ca_inline.php file look like this:
____________________________________
<?php
# start the standard php sessions
session_name('CSID');
session_start();
# function to call a cgi script from inside an html page and display
# the result inline on the page. sort of like an inline perl parser.
function call_cgi( $url, $args )
{
# collect local variables
$sid = 'phpsid=' . session_id();
$query = $_SERVER['QUERY_STRING'];
$host = 'http://' . $_SERVER['HTTP_HOST'];
# place ampersands in the correct locations
if( $args != '' && $query != '' ) { $query = '&' . $query; }
if( $args != '' || $query != '' ) { $sid = '&' . $sid; }
# echo the results of the cgi script
echo file_get_contents( $host . $url . '?' . $args . $query . $sid );
}
?>
____________________________________
You could pretty much copy and paste that code into a similar file and
everything would be fine. The one change you might want to change is the
session_name('CSID') line. 'CSID' is the name of the cookie that we use for
our session management, that should be changed to the name of the cookie
that you use. SESSIONID, USERID or what-not.
Note: we need to pass the cookie value in the query string because
technically it's not the visitors' browser that's calling the .cgi script;
it's the server. And the server is calling the script with no cookie jar set
up.
Finally inside the .html (or .php) file that I want to display the current
shopping cart total / contents I add the line:
<?php call_cgi( '/cgi-bin/cart.cgi', 'rm=runningtotal' ); ?>
This line calls the call_cgi function in /inc/ca_inline.php, which in turn
packages up the entire query string and session_id together as one big query
string to send to cart.cgi as run mode 'runningtotal', which is configured
to output only a small block of html code from a template with headers
turned off.
How it works:
Using the example code above, a visitor landing on the page
www.domain.com/products.html would generate a call to cart.cgi script that
looked like:
/cgi-bin/cart.cgi?rm=runningtotal&phpsid=fc4cc9e4f913f76b42b2186e492f0e34
where phpsid is the value that I can then pass to CGI::Session (or what have
you) to look up the proper session information (also, for your purposes, you
could change the name of this varaible from phpsid to something more
memorable or useful for you).
I also added the ability for the script to pass along any additional
information given to the .html (or .php) page as secondary query string
information. So for example if the same page was called like
www.domain.com/products.html?name=value1, it would generate a call to
cart.cgi like:
/cgi-bin/cart.cgi?rm=runningtotal&phpsid=fc4cc9e4f913f76b42b2186e492f0e34&na
me=value1
Conclusion:
Simple? I hope so.
A little extra processor time jumping between two languages? Definitely.
Worth while to be able to use small blocks of C::A code inside an HTML page?
I think so, but I'll leave that up to you.
---
Steve Comrie
---------------------------------------------------------------------
Web Archive: http://www.mail-archive.com/suppressed/
http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed
Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.