PHPExcel conflict with session_start

Topics: Project Management Forum
Jan 12, 2009 at 2:58 AM
Is there a way to use PHP Excel with sessions?  I use session variables for security .   If I initialize a session, PHP Excel no longer works.  Is there an inherent conflict?

Stefan
Developer
Jan 12, 2009 at 3:04 AM
This sounds very strange!!? As far as I know there should be no conflict. What error messages are you getting?

Coordinator
Jan 12, 2009 at 5:47 AM
Can you re-download version 1.6.5 again? There was an errror in the downloaded package which may have to do with this.
Jan 12, 2009 at 9:17 PM
I reloaded 1.6.5 and still get the same problem.

When I add the following one line of code (containing session_start), the files returns opens a window to get file information and churns indefinitely (code included below).  If the session start is not included, the file  opens up normally as an excel file.
 
// Initialize a session.
session_start();
 
Here's a link to the php code without the session_start: https://www.sports-registration.net/ssl/admin/phpexcel/files/test.php
 
and another link with the problem: https://www.sports-registration.net/ssl/admin/phpexcel/files/test_fail.php
 
Stefan Koziolek
 
================================== remainder of code =================================
 
<?php
/**
 * PHPExcel
 *
 * Copyright (C) 2006 - 2009 PHPExcel
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category   PHPExcel
 * @package    PHPExcel
 * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
 * @version    1.6.5, 2009-01-05
 */
 
/** Error reporting */
error_reporting(E_ALL);

// Initialize a session.
session_start();
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../Classes/');
 
/** PHPExcel */
include 'PHPExcel.php';
 
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
 
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
 
// Set properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
$objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
$objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");
$objPHPExcel->getProperties()->setCategory("Test result file");
 

// Add some data
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Parent Names');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Primary Email Address');
$objPHPExcel->getActiveSheet()->setCellValue('C1', 'Secondary Email Address');
$objPHPExcel->getActiveSheet()->setCellValue('A2', 'Stefan and Maureen Koziolek');
 
// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');
 

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
 
  
// Save Excel 2007 file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
 

/* Here there will be some code where you create $objPHPExcel */
 
// redirect output to client browser
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="family_email_list.xlsx"');
header('Cache-Control: max-age=0');
 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
 

?>
Developer
Jan 12, 2009 at 11:17 PM
This is apparently a bug in Internet Explorer 6 and perhaps also later versions happening when under SSL when one uses certain cache directives in the headers.
http://support.microsoft.com/kb/316431

When I go to your second link, I first get the ordinary download dialog:
http://img261.imageshack.us/img261/4177/ie6promptbk8.png

When I click save, I get the following error:
http://img261.imageshack.us/img261/989/ie6errortb6.png

Try to remove the following line above and I think it should work
header('Cache-Control: max-age=0');

As far as I know, the browser will usually not cache the object under https.

There is some more discussion in the Drupal forum:
http://drupal.org/node/163298


Developer
Jan 12, 2009 at 11:27 PM
Sorry, removing the line will of course not work. I was forgetting that you said that this happened only when you were using sessions.

That is because when you call session_start(), some headers with cache directives are automatically added.

Instead, if you really need to use SSL, try solution from here:
http://joseph.randomnetworks.com/archives/2004/10/01/making-ie-accept-file-downloads/

Note: This is a general problem with Internet Explorer, not only with PHPExcel.
Coordinator
Jan 13, 2009 at 12:08 PM
I've found similar issues with SSL in IE, not just for downloads, but also for accessing XML data.

I use the following:

header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header ('Cache-Control: cache, must-revalidate');
header ('Pragma: public');