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/vnd.ms-excel” );
header(“Content-Disposition:attachment; filename=”.$filename );
header(“Content-Transfer-Encoding: binary” );
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:
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.0