<!--- 
 Filename:   AdServer2.cfm
 Created by:  Nate Weiss (NMW)
 Purpose:   Responds with the binary content of a randomly selected image
--->

<!--- Ad Images are in "Ads" subfolder, within current folder --->
<cfset adDir = getDirectoryFromPath(getCurrentTemplatePath()) & "Ads\">

<!--- Get a listing of GIF image files in the Ads folder --->
<!--- Result is a query object with a row for each image --->
<cfdirectory directory="#adDir#" action="LIST" filter="*.gif" name="getAds">

<!--- Pick random number between one and number of images --->
<cfset adNum = randRange(1, getAds.recordCount)>

<!--- Grab the filename in chosen row of the GetAds query --->
<cfset adFileName = getAds.Name[adNum]>

<!--- Prepend directory to get full filesystem path to ad image --->
<cfset adFilePath = adDir & adFileName>

<!--- Record fact that this ad is now placed on this page --->
<!--- You could record the ad-showing in database instead --->
<cfset SESSION.adsOnPages[CGI.http_referer] = adFileName>

<!--- Send the chosen image back to the client --->
<cfcontent type="image/gif" file="#adFilePath#">
