Advertisement

Tutorials

Home Tutorials Server Side Includes Tutorial

Overcoming SSI

2.2/5.0 (6 votes total)
Rate:

John Saya
April 05, 2006


John Saya

http://www.cgiconnection.com/
John Saya has written 2 tutorials for CGIDir.
View all tutorials by John Saya...

If you've ever wanted data displayed in realtime on your site, such as a counter, or stock quotes, you've probably tried using SSI (Server Side Includes), and have seen the drawbacks.

Two problems I've noticed with SSI is that you cannot pass values back and forth to the server, and other people cannot link to that page or script because SSI will not allow other domains to execute it. This causes many problems if you want to create counters, newsfeeds, or stock tickers. Since SSI generally will not allow other domains to execute a script, you can forget about giving away counters, or anything else like that.

How do we overcome this? Well, with modern browsers I see no point in SSI when you can use a very simple JavaScript technique. The whole process is just slightly different than SSI, but still requires that you put only one line of HTML on your web page!

For example, your standard SSI call in your web page may look like this:

<!--#exec cgi="/cgi-bin/script.cgi" -->

Using the JavaScript method will look like this:

<SCRIPT="/cgi-bin/script.cgi"></SCRIPT>

You can specify the entire URL if you want others to link to the script too. Not so difficult, huh?

Now, there's pretty much just one simple change you would make when sending any output to the browser. Let's say you want to output a simple table. Normally in your script, you would do something like this:

print "<TABLE BORDER=0><TR><TD>My Cell</TD></TR></TABLE>\n";

But now, you will use this:

print "document.write('<TABLE BORDER=0><TR><TD>My Cell</TD></TR></TABLE>');\n";

The only thing that was added was the JavaScript document.write object. Your HTML is simply placed between single quotes (apostrophes) and parenthesis.

Basically what happens is your browser sees the <SCRIPT> tag, calls the URL, and outputs all document.write statements to the page immediately. Keep in mind that you do not output the <SCRIPT> or </SCRIPT> tags in your script because you already called them in your web page.

You can also pass variables back to the server using this method, like this:

<SCRIPT="/cgi-bin/script.cgi?variable=value"></SCRIPT>

Let's say you wanted to display the entire query string that you passed in the line above. Your script would look like this:

#!/usr/bin/perl

print "Content-type: text/html\n\n";

$query_string = "QUERY STRING IS: $ENV{'QUERY_STRING'}";

print "document.write('<br /><br />$query_string<br /><br />');\n";

exit;


Be sure to CHMOD the script above to 755. Your web page would call the above script like this:
<HTML>
<BODY>
<CENTER>
<H1>Query String Demo</H2>
</CENTER>

<SCRIPT="/cgi-bin/script.cgi?variable=value"></SCRIPT>

</BODY>
</HTML>


Once you get used to this method, trust me, you'll never use SSI again!


Add commentAdd comment (Comments: 0)  

Advertisement

Partners

Related Resources

Other Resources

image arrow