Tricks with mod include

From Web Developer Network Wiki
Jump to: navigation, search

Showing content at a given date/time:

<!--#config timefmt="%Y%m%d" -->
<!--#if expr="$DATE_LOCAL < 20051211" -->
    <p class="sserifstoryheadnavy">Surplus Auction - Saturday, Dec. 10, 2005</p>       
    <p class="sseriftext"><a href="auctionflyer121005.pdf">Click here for flyer.</a></p>
<!--#else -->
<!--#endif -->

What happens here:

The Apache server running mod_include is told to use the format YYYYMMDD for DATE_LOCAL variables.

The next line uses SSI's limited conditional operators to compare the current date to the supplied date (the date, in this case, on which you'd like the display of some snippet of content to end). Important, obviously, to use the YYYYMMDD format.

You could put some alternate snippet of content between the ELSE and ENDIF.

If you want to make the time sensitivity more granular, you can add an hour variable. You could also add minutes and seconds, but really now.

An opening snippet of code (you'll still need the optional else and endif statements) that includes hour data is as follows:

<!--#config timefmt="%Y%m%d%H" -->
<!--#if expr="$DATE_LOCAL < 2008020610" -->

Note the addition of the "%H," which generates hours from 00-23, in military time.




Showing the file size

<!--#config sizefmt="Mb"-->
<!--#fsize virtual="your_file_name_goes_here"-->


So what happens here:

The first line sets the format of the size to be reported, we set it for Megs.

The second line you point it to your file you wish to report the file size of.



Restricting access by IP Address or cookie:

<!--#if expr="$HTTP_COOKIE = /livepage/" --> 
	<!--#include virtual="/unlpub/unltoday.shtml" -->
<!--#elif expr="$HTTP_COOKIE = /splashpage/" -->
	<!--#include virtual="/unlpub/2004unlsplash.shtml" -->
<!--#elif expr="
	($REMOTE_ADDR = /^129\.93/) || 
	($REMOTE_ADDR = /^65\.123\.(3[2-9]|[4-5][0-9]|6[0-3])\./) || 
	($REMOTE_ADDR = /^64\.39\.(24[0-9]|25[0-5])\./) || 
	($REMOTE_ADDR = /^216\.218\.(20[8-9]|21[0-9]|22[0-3])\./)
" -->
	<!--#include virtual="/unlpub/unltoday.shtml" -->
<!--#else -->
	<!--#include virtual="/unlpub/2004unlsplash.shtml" -->
<!--#endif -->

What happens here:

We have two homepages that can be delivered from the UNL webserver. UNL Today exists as a 'destination' page, one we use to generate repeat traffic, which we can then move along to the other subsites at UNL. The UNL 'splash' page is delivered to external audiences for whom a simplified presentation and visual appeal may be more important. Anyone can set a cookie on the UNL site to deliver either the UNL Today or UNL splash pages as their default UNL home page.

In this example, the Apache server first looks for a cookie carrying the value 'livepage.' If it finds that value, it supplies the UNL Today page content as index.shtml. If not, it goes on (ELIF=else if, or otherwise). If the cookie ('unldefaulthome', although the SSI variables don't need to know its name) carries the value 'splashpage,' the UNL splash page content is delivered.

By this point, we know that the user has not set the 'unldefaulthome' cookie. If the 'unldefaulthome' cookie has not been set, then we begin looking for IP addresses. There are many ways to look for IP addresses, many fairly simple. Because the unl.edu domain includes several address ranges, we're employing Regular Expressions to evaluate whether any particular IP address falls within that range.*

The supplied regular expressions describe all ranges that are part of the unl.edu domain. If a user's client computer is connected to a network address that falls within the unl.edu ranges, then they'll get the UNL Today page.

There's one more group of users, who by process of elimination we've identified as users external to UNL (they haven't set the 'unldefaulthome' cookie, and they are connected to the network from outside of unl.edu). We send them the splash page under the catch-all ELSE statement.

If you'd like to know more about conditionals within mod_include, a good place to start is at This ServerWatch article from 2000. The Apache Software Foundation maintains documentation, and provided a Tutorial Introduction to Server Side Includes for the 1.3 version of the Apache webserver (little has changed). As of December 2005, UNL's Frontier server is running Apache 2.0.40 on Red Hat Linux.

  • Regular Expressions are pretty hard to decipher simply by looking at them, but there are numerous guides available online for anyone who wants to wade in. One of the best is (believe it or not) Regular Expressions Info.


Showing content to secure connections

<!--#if expr="$HTTPS" -->
Your connection is secure.
<!--#else -->
Your connection is not secure.
<!--#endif -->

What happens here:

During a secure request, the HTTPS environment variable is set.


Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox