This is a discussion on ColdFusion Tips & Tricks within the ColdFusion Programming forums, part of the Web Development category; Access is Restricted to this ColdFusion Service. This copy of Allaire ColdFusion is licensed for use by a single user ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
|
#1
| |||
| |||
| Access is Restricted to this ColdFusion Service. This copy of Allaire ColdFusion is licensed for use by a single user only. The ColdFusion server is currently configured to accept requests exclusively from IP address 127.0.0.1. (Your request was made from xxx.xxx.xx.xx.) The ColdFusion engine must be stopped and restarted to reset the permitted IP address. ------ Ever wonder how the single-user version of Cold Fusion determines which IP address it would work for? This has baffled many programmers (myself included) in trying to figure it out. It doesn't clearly state how it works but a hint is provided in the error message: "The ColdFusion engine must be stopped and restarted to reset the permitted IP address." The single-user version of the Cold Fusion server will only respond to the IP address that sends the first request to it. If you have an application on your computer that you want to demonstrate from another computer, restart the Cold Fusion server. As long as you do not run any templates on your computer, you should be able to execute it fine from any other computer. |
|
#2
| |||
| |||
| Allowing commas in a numeric CFINPUT text field Normally, you can not enter numbers with a comma in a numeric CFINPUT field, the Javascript function generated by Cold Fusion will reject it. Nice thing about Javascript, you can define a function more than once without it going kablooie all over your nice computer. The function defined last is the one used. Here's an example. I copy-n-pasted the generated Javascript into the template and added a comma to the list of approved numeric characters. Code:
<CFFORM name="MyForm">
<cfinput type="Text" name="i_value" validate="float" message="Bad value! Bad!">
</cfform>
<script language="Javascript">
// Override the numeric validation generated by Cold Fusion
function _CF_checknumber(object_value)
{
//Returns true if value is a number or is NULL
//otherwise returns false
if (object_value.length == 0)
return true;
//Returns true if value is a number defined as
// having an optional leading + or -.
// having at most 1 decimal point.
// otherwise containing only the characters 0-9.
var start_format = " .+-0123456789";
// Comma added to allowed characters
var number_format = " .,0123456789";
var check_char;
var decimal = false;
var trailing_blank = false;
var digits = false;
//The first character can be + - . blank or a digit.
check_char = start_format.indexOf(object_value.charAt(0))
//Was it a decimal?
if (check_char == 1)
decimal = true;
else if (check_char < 1)
return false;
//Remaining characters can be only . or a digit, but only one decimal.
for (var i = 1; i < object_value.length; i++)
{
check_char = number_format.indexOf(object_value.charAt(i))
if (check_char < 0)
return false;
else if (check_char == 1)
{
if (decimal) // Second decimal.
return false;
else
decimal = true;
}
else if (check_char == 0)
{
if (decimal || digits)
trailing_blank = true;
// ignore leading blanks
}
else if (trailing_blank)
return false;
else
digits = true;
}
//All tests passed, so...
return true
}
</script> |
|
#3
| |||
| |||
| Automatic FTP Utility This utility will allow you to transfer many files from many servers at the click of a button based on command files that gives a list of servers and files to transfer. In the zip file listed below, there are two files - ftp.cfm and test.ftp ftp.cfm is the FTP utility and test.ftp is an example command file. Contents of test.ftp: Code: ; FTP Transfer File ; ; These command files must have a .FTP file extension ; ; Commands: ; open ; Syntax: open [server:port] [UserID] [Password] ; ; close ; Syntax: close ; ; dl (download) ; Syntax: dl [protocol (B=Binary, A=ASCII)] [full directory and filename on remote server] [full directory path ONLY local server] ; ; ul (upload) ; Syntax: ul [method (B=Binary, A=ASCII)] [full directory and filename on local server] [full directory path ONLY on remote server] ; ; email (Email results) ; Syntax: email [email address] ; ; The Local and Remote file names for Uploading and Downloading can be dynamic using CF functions. ; Example: ; dl B /apps/www/logs/archive/apache.logs.#DateFormat(Now(),"mm.dd.yy")#.tar C:/Internet/Webalizer/iwof2.tar ; ; The remote file name will be evaluated out to /apps/www/logs/archive/apache.logs.12.22.04.tar ; ; WARNING: File names are CaSe SeNsAtIvE!!! Even if it's Windows, this tool expects the case to match. While transfers will still work ; otherwise, the transfer information will only display if it matches. ; ; Note: ";" identifies a comment line, don't use it for your actual command list. ; ; ====================================================================================================================================== ; ; Example of downloading a static text file with ASCII protocol open ftp.mozilla.org:21 anonymous anonymous dl A /pub/mozilla.org/README C:\ReadMe.txt close |
|
#4
| |||
| |||
| Automatically purging old generated files If your website generates reports on the fly and makes them available for the user to download by saving them to a temp directory on the server, it would be a good idea to delete old files to prevent all your disk space from being chewed up. The program listed below will purge files older than a specified number of days for an unlimited number of directories that you can specify. If you schedule it to be executed every day, you can rest assured that your temp directory doesn't get stuffed and a surprise "Out of Disk Space" error doesn't occur. Example HTML/CFML code: Code: <!---
Directory Purge, by John Bartlett
john.bartlett@attws.com
Ver 1.01
--->
<cfsetting enablecfoutputonly="Yes">
<!---
The Directory array contains a list of directories to scan and how many days old the files can be before they are purged.
If you set the history value to -1, all files will be deleted. Note that this is the number of days from the current time.
If you specify a history of 0 (kill files over 24 hours old), a file that was generated two hours ago will not be deleted.
<CFSET Directory[1][2]=2> Files over 2 days old (or 24 hours) will be deleted
<CFSET Directory[1][2]=3> Files over 3 days old (or 72 hours) will be deleted
--->
<CFSET Directory=ArrayNew(2)>
<CFSET Directory[1][1]="C:\WINNT\TEMP">
<CFSET Directory[1][2]=7>
<CFLOOP index="loop" from="1" to="#ArrayLen(Directory)#">
<CFIF Directory[loop][1] CONTAINS "/">
<CFSET PathChar="/"> <!--- Unix Directory --->
<CFELSE>
<CFSET PathChar="\"> <!--- NT Directory --->
</CFIF>
<cfdirectory action="LIST" directory="#directory[loop][1]#" name="dir">
<CFSET kill=0>
<CFSET nokill=0>
<CFSET cantkill=0>
<CFLOOP query="dir">
<CFSET cr=CurrentRow>
<CFIF dir.type[cr] EQ "File">
<CFSET tot=tot + 1>
<CFSET Age=DateDiff("d",dir.DateLastModified[cr],Now())>
<CFIF Age GT Directory[loop][2]>
<CFTRY>
<cffile action="DELETE" file="#Directory[loop][1]##PathChar##dir.Name[cr]#">
<CFOUTPUT>#dir.name[cr]# is #Age# days old, deleting<br>#Chr(10)#</CFOUTPUT>
<CFSET kill=kill+1>
<CFCATCH Type="Any">
<CFOUTPUT>#dir.name[cr]# is #Age# days old, *** UNABLE TO DELETE ***<br>#Chr(10)#</CFOUTPUT>
<CFSET cantkill=cantkill + 1>
<CFSET nokill=nokill + 1>
</CFCATCH>
</CFTRY>
<CFELSE>
<CFSET nokill=nokill + 1>
</CFIF>
</CFIF>
</CFLOOP>
</CFLOOP>
<CFOUTPUT>#tot# files total, #kill# files deleted, #nokill# files skipped, unable to delete #cantkill# files</CFOUTPUT> |
|
#5
| |||
| |||
| Blocking site rippers You spend weeks building a website, pour hours of your life into it, and discovered recently that someone site-ripped all of your generated HTML and published it elsewhere. Now that sucks. Here's a function that will help prevent against site rippers yet allow spiders and normal users to browse unrestricted. The way it works is to count the number of page views per IP address per minute and if that count exceeds a predefined value, block access. Note that if you set the max page hit to 30 and the user browses your site 29 times in a minute on the clock, they won't be blocked unless they view 31 pages in the next minute on the clock. While I understand that this is not ideal, it's a compromise between speed an functionality. Originally, I created a dynamic query that stored the time stamp of every page view. However, when I tried to clean up the query by selecting only records that occurred in the previous x amount of time, the Query of Queries can not properly handle time objects such as those created with the CreateODBCDateTime function. So instead of manually rebuilding the query row for row on every page view, I opted for this method. Example HTML/CFML code: Code: <!---
MaxHits: The maximum number of page views allowed in a given minute
While doing this much work inside an exclusive lock of the application scope is undesireable, it's the
only way to defeat multi-threaded site rippers.
--->
<CFSET MaxHits=30>
<!--- Only check if not identified as a spider, allow most spiders to browse unrestricted (remove if not wanted) http://www.psychedelix.com/agents.html --->
<CFIF ListContainsNoCase(CGI.HTTP_User_Agent,"bot,spider,spyder,agent,altavista,crawl,arachno,24x,seek,search,fetch,deadlink,index,diagem,google") EQ 0>
<CFSET UserIP="IP_" & Replace(CGI.Remote_Addr,".","_","ALL")>
<cflock timeout="30" throwontimeout="Yes" type="EXCLUSIVE" scope="APPLICATION">
<CFIF IsDefined("Application.IPHistory") EQ "No">
<CFSET Application.IPHistory=StructNew()>
<CFSET Application.IPHistory.Cnt=0>
</CFIF>
<CFIF IsDefined("Application.IPHistory.#UserIP#") EQ "No">
<CFSET "Application.IPHistory.#UserIP#"=StructNew()>
<CFSET History=StructNew()>
<CFSET History.TimeStamp=TimeFormat(Now(),"HH:mm")>
<CFSET History.Cnt=0>
<CFELSE>
<CFSET History=Duplicate(Evaluate("Application.IPHistory.#UserIP#"))>
</CFIF>
<CFSET CurrentTimeStamp=TimeFormat(Now(),"HH:mm")>
<CFIF CurrentTimeStamp NEQ History.TimeStamp>
<CFSET History.TimeStamp=CurrentTimeStamp>
<CFSET History.Cnt=0>
</CFIF>
<CFSET History.Cnt=History.Cnt + 1>
<CFSET "Application.IPHistory.#UserIP#"=Duplicate(History)>
<!--- Keep track of the overall page hits for all users and remove IP's that haven't accessed the site in the past 10 minutes --->
<CFSET Application.IPHistory.Cnt=Application.IPHistory.cnt + 1>
<CFIF Application.IPHistory.Cnt GT 1000>
<CFSET Application.IPHistory.Cnt=0>
<CFLOOP index="CurrIP" list="#StructKeyList(Application.IPHistory)#">
<CFIF CurrIP NEQ "CNT">
<CFSET TS=DateFormat(Now(),"mm/dd/yyyy") & " " & Evaluate("Application.IPHistory.#CurrIP#.TimeStamp") & ":00">
<CFIF DateDiff("n",TS,Now()) GT 10>
<CFSET tmp=StructDelete(Application.IPHistory,CurrIP)>
</CFIF>
</CFIF>
</CFLOOP>
</CFIF>
</cflock>
<CFIF History.Cnt GT MaxHits>
<CFOUTPUT>
<h1>Access Denied</h1>
Your access has been temporarly blocked due to excessive access.
</CFOUTPUT>
<CFABORT>
</CFIF>
</CFIF> |
|
#6
| |||
| |||
| Changing form fields in a different frame If you need to change form fields in one frame based of what a user selectes in another frame, here's a basic example on how to do so, all using plain html and Javascript. There are three files: 1. Frames.html Defines the layout of the frames 2. Left.html Creates a dropdown selection box and the Javascript that is used to populate the Right frame 3. Right.html Creates two text fields that are populated by the Javascript in the left frame Simply copy-n-paste these files onto your ISP and jump to Frames.html to see it in action or use it as a guide. Example HTML/CFML code: Code: <!--- Frames.html --->
<frameset cols="50%,*">
<frame name="Left" src="Left.html" marginwidth="10" marginheight="10" scrolling="auto" frameborder="0">
<frame name="Right" src="Right.html" marginwidth="10" marginheight="10" scrolling="auto" frameborder="0">
</frameset>
<!--- Left.html --->
<html>
<head>
<script language="Javascript1.2">
function PopulateRightForm()
{
// Get the index of the drop-down box
var idx=document.LeftForm.LeftDropdown.selectedIndex;
// Get the value of the selected item of the dropdown box
var n=document.LeftForm.LeftDropdown.options[idx].value;
// Get the text of the selected item of the dropdown box
var t=document.LeftForm.LeftDropdown.options[idx].text;
// Populate the values of the text boxes in the right frame
parent.frames['Right'].document.RightForm.RightText.value=t;
parent.frames['Right'].document.RightForm.RightValue.value=n;
}
</script>
</head>
<body bgcolor="FFFFFF">
<form name="LeftForm">
Select a number:
<select name="LeftDropdown" size="1" onChange="PopulateRightForm()">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
</select>
</form>
</body>
</html>
<!--- Right.html --->
<html>
<body bgcolor="FFFFFF">
<form name="RightForm">
Text passed from left form: <input type="Text" name="RightText"><br>
Value passed from left form: <input type="Text" name="RightValue">
</form>
</body>
</html> |
|
#7
| |||
| |||
| Changing the action field of a form tag If you want to have one form but have it jump to different pages based off of what button a user clicks on, you can use the following example to change the form.action value. Example HTML/CFML code: Code: <html>
<head>
<!--- Set up the javascript to change the action url --->
<script language="Javascript">
function SetURL(site)
{
if (site == '1') document.myform.action='http://www.allaire.com/';
if (site == '2') document.myform.action='http://www.allaire.com/developer/gallery/index.cfm?objectID=6800';
document.myform.submit();
}
</script>
</head>
<body>
<!--- No action field specified, will default to load the same document --->
<center>
<form name="myform" method="get">
<input type="Button" value="Allaire's Homepage" onClick="SetURL('1')"><br><br>
<input type="Button" value="Tag Gallery" onClick="SetURL('2')"><br>
</form>
</center>
</body> |
|
#8
| |||
| |||
| Cold Fusion ate my webserver! The only means that I know of that Cold Fusion can lock up IIS is if IIS is only configured to allow a certain number of tasks to be executed. For example, Personal Web Server will only allow two tasks to execute at any given time. The snag here is when you perform a call against the database. If a third-party call (such as a database query) does not respond back to Cold Fusion, CF will sit there and wait forever until the database sends something back to it. Timeouts do not apply here. If Cold Fusion never finishes a task and therefore never notifies IIS that it has finished, IIS will keep waiting for it and you'll eventually run out of free "sessions" and IIS will stop responding to new page requests Under Windows 95/98, the only option is to reboot. Under NT, you may be able to stop the Cold Fusion service to see if it'll free up the tasks. Otherwise, you'll have to reboot. But you need to look into your database access to find out where the problem lies. It's not with Cold Fusion, it's with your database. |
|
#9
| |||
| |||
| Creating a logfile of a programs execution If you want to create a log file of a programs execution (ideal for programs that run via the Scheduler), here is a tag that I wrote to perform the task for you. All you need to do is to copy-n-paste the code below into a file called "DumpQuery.cfm" located in your \CFUSION\CustomTags directory. Note that the program traps any file errors so that if you fubar something with the file name, it doesn't stop the program from executing. So if you run your program and get no log file, check your file path to ensure that it is valid. Example HTML/CFML code: Code: <cfsetting enablecfoutputonly="Yes">
<!---
Creates a log file
Passed Attributes
Action: Open Kills any existing log file allowing a fresh log file to be created
Initilizes session variable
Close Writes any pending information to the log file
<none> Append to the specified log file
file: Full path information and file name of the log file. If not specified, it
will check to see if a variable "logfile" was defined in the calling template.
If so, it will use the file information from the logfile variable. Otherwise,
it will create a log file as the same as the template calling it with an
".log" appended to the end of the cfm. Ie: C:\InetPub\www\index.cfm.log
timestamp: Y/N If Y, the current date and time is prepended to the text to record
text: The text to append to the log file
loghistorysize: A number in bytes to cache. Once the logfile history exceedes this amount, it
is appended to the log file. Defaults to 10k.
You can only write to one log file at a time. If you start to write to a 2nd log file, be sure to close the first one first.
If you wish to append to a already existing log file without deleting it first, do not specify the "open" action.
If you do not close a log file, any pending information to be written will be lost.
Examples:
Delete any existing logfile and open a fresh file:
<CF_Log file="C:\Inetput\WWW\History.log" action="open">
Append a string of text to a logfile defined with "logfile" with a timestamp
<CF_Log text="User viewed home page">
Same as above but without a timestamp
<CF_Log text="User viewed home page" timestamp="N">
Done with program, want to flush out the log cache
<CF_Log action="close">
--->
<CFPARAM name="attributes.action" default="">
<CFPARAM name="attributes.file" default="">
<CFIF attributes.file NEQ "">
<CFSET caller.logfile=attributes.file>
</CFIF>
<CFPARAM name="caller.logfile" default="#CF_TEMPLATE_PATH#.log">
<CFPARAM name="file" default="#caller.logfile#">
<CFPARAM name="attributes.timestamp" default="Y">
<CFPARAM name="attributes.text" default="">
<CFPARAM name="attributes.loghistorysize" default="10240">
<CFPARAM name="caller.loghistory" default="">
<CFSET attributes.action=LCase(attributes.action)>
<CFIF attributes.timestamp EQUAL "Y">
<CFSET ts=DateFormat(Now(),"mm-dd-yyyy") & " " & TimeFormat(Now(),"HH:mm:ss") & " ">
<CFELSE>
<CFSET ts="">
</CFIF>
<CFTRY>
<CFIF attributes.action EQUAL "open">
<cffile action="DELETE"
file="#file#">
<cffile action="WRITE" file="#file#" output="#ts#Log Opened" mode="777" addnewline="Yes">
<CFSET caller.loghistory="">
</CFIF>
<CFCATCH type="any">
<!--- Eat any file errors --->
</CFCATCH>
</CFTRY>
<CFTRY>
<CFIF attributes.action EQUAL "close">
<CFSET caller.loghistory=caller.loghistory & Chr(13) & Chr(10) & ts & "Log Closed">
<cffile action="APPEND"
file="#file#"
output="#caller.loghistory#"
addnewline="Yes"
mode="777">
<CFSET caller.loghistory="">
</CFIF>
<CFCATCH type="any">
<!--- Eat any file errors --->
</CFCATCH>
</CFTRY>
<CFTRY>
<CFIF Trim(attributes.action) NOT EQUAL "open" AND Trim(attributes.action) NOT EQUAL "close">
<!--- <CFOUTPUT>#ts##attributes.text#<br></CFOUTPUT> --->
<CFIF caller.loghistory EQUAL "">
<CFSET caller.loghistory=ts & attributes.text>
<CFELSE>
<CFSET caller.loghistory=caller.loghistory & Chr(13) & Chr(10) & ts & attributes.text>
</CFIF>
<CFIF Len(caller.loghistory) GREATER THAN attributes.loghistorysize>
<cffile action="APPEND"
file="#file#"
output="#caller.loghistory#"
addnewline="Yes"
mode="777">
<CFSET caller.loghistory="">
</CFIF>
</CFIF>
<CFCATCH type="any">
<!--- Eat any file errors --->
</CFCATCH>
</CFTRY> |
|
#10
| |||
| |||
| Creating arrays of more than three dimensions According to the CF Docs, you can only create an array of three dimensions max. So you can have a one dimensional array referenced as MyArray[a], a two dimensional array as MyArray[a][b], and a three dimensional array as MyArray[a][b][c]. But you could not create a four dimensional array as MyArray[a][b][c][d] because <CFSET MyArray=ArrayNew(4)> would generate an error. However, the one really sweet feature about Cold Fusion is that you can store any type of variable inside another variable. You can store a query or structure inside an array element. For example: Code: <CFSET MyArray=ArrayNew(1)>
<CFSET MyArray[1]=StructNew()>
<CFSET MyArray[1].MyQuery=QueryNew("field1,field2")> Now the trick with creating an array of more than three dimensions is to create an array inside an array. To give an example, you can create a 2D array with the following command: Code: <CFSET MyArray=ArrayNew(2)> <CFSET MyArray[1][1]="A"> Code: <CFSET MyArray=ArrayNew(1)> <CFSET MyArray[1]=ArrayNew(1)> <CFSET MyArray[1][1]="A"> Code: <!--- Assign six dimensional loop --->
<CFLOOP index="d1" from="1" to="3">
<CFSET MyArray[d1]=ArrayNew(1)>
<CFLOOP index="d2" from="1" to="3">
<CFSET MyArray[d1][d2]=ArrayNew(1)>
<CFLOOP index="d3" from="1" to="3">
<CFSET MyArray[d1][d2][d3]=ArrayNew(1)>
<CFLOOP index="d4" from="1" to="3">
<CFSET MyArray[d1][d2][d3][d4]=ArrayNew(1)>
<CFLOOP index="d5" from="1" to="3">
<CFSET MyArray[d1][d2][d3][d4][d5]=ArrayNew(1)>
<CFLOOP index="d6" from="1" to="3">
<CFSET MyArray[d1][d2][d3][d4][d5][d6]="#d1#,#d2#,#d3#,#d4#,#d5#,#d6#">
</CFLOOP>
</CFLOOP>
</CFLOOP>
</CFLOOP>
</CFLOOP>
</CFLOOP>
<CFDUMP var="#MyArray#"> |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C# .Net Tips & Tricks | oxygen | C# Programming | 85 | 01-08-2009 12:25 AM |
| SAP Tips & Tricks | leoraja8 | Operating Systems | 0 | 03-29-2008 12:11 AM |
| PHP Tips and Tricks | Sabari | PHP Programming | 20 | 12-18-2007 05:26 AM |
| .NET tricks & Tips | Karpagarajan | VB.NET Programming | 1 | 04-23-2007 08:17 AM |
| SEO Tips & Tricks | spid4r | Search Engine Optimization | 0 | 03-08-2007 11:03 PM |
Our Partners |