Tuesday, April 8, 2014

Renewal of session and the expected behaviour of CFLOGIN

Lately I used CFLOGIN to manage user's session and its so powerful. It comes up with so many luxury of setting up the user roles, and at any point of time, IsUserLoggedIn(), GetAuthUser() comes in handy.

CFLOGOUT makes life much more easier to logout a user. But then it doesn't do everything like clearing session values, etc. In fact, clearing the session values have to be handled by us for the right behaviour of the application.

The real glitch is the way cflogin and session handles the timeout. There are two rules.

1. If the session expires, cflogin expires.
2. If the cflogin expires, session doesn't have to expire and  rather it doesn't expire.

So careful steps have to be taken while setting the session time out in app.cfc and idletimeout in cflogin. 

Now, if a user is given an option to renew a session, a soft refresh using ajax post would do. ie an ajax post to the server would be considered as a call to the server and the session would be considered as renewed.

But cflogin doesn't recognize that. In that case, a hard refresh of the page is needed to make a call to the application/web server. That would let the cflogin renew its session too.



Tuesday, November 26, 2013

FTP made easier with CFFTP - Downloading zip file using cfftp

CFFTP is a powerful feature in coldfusion which allows file operations without hassles. To start with it might throw challenges but then it's not hard to master.

To get a file from a server using FTP. Adobe docs have very detailed explanation of this tag. You can find this here.

1. Open a connection.
                This is not a necessary option but then I'd choose this way.

                  <cfftp action="open" username="webuser" connection="ftpConnection"
                   password="secret" server="testftp.com" stoponerror="Yes" secure="true"
                   timeout="300"/>

                Timeout is optional, I'd rather specify it.

2. GetFile

                Let me give an example for GetFile operation.

                Get the current path using the GetCurrentDir action. This would help us to avoid errors like file not found.

                <cfftp connection=MyFTPConnection action="GetCurrentDir" stoponerror="Yes">


               <cfftp action="getFile" connection="ftpConnection"
                   localfile="/home/data/downloadedFile.zip"
                   remotefile="#cfftp.returnvalue#/gallery/images.zip"
                   transfermode="auto" failifexists="no" timeout="300" passive="true"/>

3. Close the connection

               <cfftp action="close" connection="MyFTPConnection" stoponerror="Yes"/>


Now the required operation can be performed on that. Let me know if any of you have any questions.




Monday, November 25, 2013

Add a Date/time Column with a default date to a table in MySQL

This is again a mundane task which any one of us might have been required to do. Please find the simple DDL.

If the requirement is to alter table Users with a pwdchangedate with a default date of today, here goes the DDL

ALTER TABLE Users
    ADD pwdchangedate
    DATETIME
    NOT NULL
    DEFAULT now();


Wednesday, November 13, 2013

Find the difference between 2 dates in days in coldfusion

This is a requirement which is faced in our day to day coding life.  The  DateDiff() function is used.

Let me give an example.
 
     There's a scenario in which the last login date/time is compared against today's date/time. If the difference is more than 7 days, we send an e-mail alert to the user saying the user has not done any activity in the site.

LastLoginDate: 2013/10/01 20:10:10

noOfDays = DateDiff("d",DateFormat(LastLoginDate, "mm/dd/yyyy"),DateFormat(now(), "mm/dd/yyyy"));

noOfDays = 12.

This one did the work for me :)