Website Design and Mobile Application Development in Austin, Texas

Export to CSV invalidates session on CodeIgniter

Export to CSV invalidates session on CodeIgniter

I ran into an interesting problem I’d never seen before this evening. I’m not sure if this is a problem isolated to the CodeIgniter framework because I’ve seen a few forum posts on Sencha/ExtJS reporting the same type of issue. But this was definitely happening for me on CodeIgniter.

The situation was that every time a user exported some data from one of my web applications to csv in Internet Explorer 8 and then opened that csv file, it would invalidate their session.  So, for example, they were running a report in the administrative console that provided results in an html table.  On all of the web applications that I write, I always give the user the ability to export the data to csv through a link above the table.  When a user clicked this link in IE and downloaded the csv file and then opened it, it essentially killed their session and forced them to log back in.  The key thing is that it would only do this if the user opened the file.  If they didn’t open it, their session was fine.

After doing some troubleshooting, I found that the issue was fixed by changing the Content-Type in the header.   In the method that exports the data to csv, it was set up as this:

header(“Cache-Control: must-revalidate, post-check=0, pre-check=0” );
header(“Content-Type: application/” );
header(“Content-Disposition:attachment; filename=”.$filename );
header(“Content-Transfer-Encoding: binary” );

print $out;

Where the “print $out” is actually printing out the details.

I was able to resolve this problem by changing the content-type header line above to:

header(“Content-Type: application/octet-stream”);

I didn’t see many forum posts about this, so hopefully this blog post will help someone out down the road who runs into this.


Leave a Reply

Your email address will not be published. Required fields are marked *