I'm having an issue with a server I inherited. I have two files. The first file queries a mysql database and returns a table to the user. The second file uses phpexcel and php html dom parser to read the table and export the results to excel.
I have it working with a small test page i created to test my code, but when I try to incorporate it into the inherited pages, it doesn't work. I think it's because of issues with validating the session or something of that nature, but I'm not sure, and I'm not sure how to fix it. The following are the two pages that I'm trying to get working together.
View Tables
<?php
session_start(); //Start the session.
//if no session value is present, redirect the user:
if(!isset($_SESSION['idUser'])) {
require_once('includes/login_functions.inc.php');//Need the functions to create an absolute URL:
$url = absolute_url();
header("Location: $url");
exit(); //Quit the script.
}
//*******************************************************
$page_title = 'View Tickets';
include ('includes/header.html');
include ('includes/tdformatting_functions.inc.php');
require_once('mysqli_connect.php');
include ('includes/calstyle.css');
...
code to generate table via mysql/php
...
//when clicked this link should open OutputToXL.php and create an Excel file with contents from current page.
echo '<td><a href="../Test/OutputToXL.php?host='.$_SERVER['HTTP_HOST'].'&page='.$_SERVER['PHP_SELF'].'" target="_blank"><img src="images/exexcel.png" width="80" height="17" title="Export to Excel."></a></td>';
?>
Output to Excel
<?php
session_start(); //Start the session.
//if no session value is present, redirect the user:
if(!isset($_SESSION['idUser']))
{
require_once('includes/login_functions.inc.php');//Need the functions to create an absolute URL:
$url = absolute_url();
header("Location: $url");
exit(); //Quit the script.
}
$page_title = 'New Excel';
include ('includes/header.html');//This Header comes out correctly, so I believe the authentication is working at this point
include ('includes/tdformatting_functions.inc.php');
require_once('mysqli_connect.php');
include ('includes/calstyle.css');
/** Include path **/
ini_set('include_path', ini_get('include_path').'../Utility/File/Excel/Classes/');
/** PHP Simple HTML DOM Parser */
include ('../Test/simplehtmldom/simple_html_dom.php');
/** PHPExcel */
include ('../Utility/File/Excel/Classes/PHPExcel.php');
/** PHPExcel_Writer_Excel2003 */
include ('../Utility/File/Excel/Classes/PHPExcel/Writer/Excel5.php');
//Create new PHPExcel object
echo date('H:i:s') . " Create new PHPExcel object <br>\n";
$objPHPExcel = new PHPExcel();
// Set properties
echo date('H:i:s') . " Set properties <br>\n";
$objPHPExcel->getProperties()->setCreator("Roy Jacob");
$objPHPExcel->getProperties()->setLastModifiedBy("Roy Jacob");
$objPHPExcel->getProperties()->setTitle("Office 2003 XLS Test Document");
$objPHPExcel->getProperties()->setSubject("Office 2003 XLS Test Document");
$objPHPExcel->getProperties()->setDescription("Test document for Office 2003 XLS, generated using PHP classes.");
//Thes variables are passed from the page I want to export
$host = $_GET['host'];
$page = $_GET['page'];
$urlfile = 'http://' . $host . $page;
echo $urlfile . '<br>';
$html = file_get_html($urlfile);
//this is a test echo which I find a problem with. The page that is put into $html is not the same one that is being processed below. Below it's processing login.php instead of the passed page above.
foreach($html->find('a') as $element)
echo $element->href . '<br>';
// Add some data
echo "<br>" . date('H:i:s') . " Adding Table data <br>\n";
$objPHPExcel->setActiveSheetIndex(0);
//Parse Entire Table into Rows
echo "<br>" . date('H:i:s') . " Parsing Table <br>\n";
//$tablematch = preg_match_all('/<tr(\s*?)(.*?)(\s*?)(.*?)(\s*?)<\/tr>/',$stringresults,$trs);
$nextrow = 1;
$nextcol = 0;
foreach($html->find('table#results') as $resultstable)
{
foreach($resultstable->find('tr') as $row)
{
foreach($row->find('th') as $header)
{
$hcell = $header->plaintext;
echo $hcell . ": " . $nextrow . ", " . $nextcol . "<br>\n";
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($nextcol,$nextrow, ($hcell));
$nextcol++;
}
$nextcol = 0;
foreach($row->find('td') as $Cells)
{
$dcell = $Cells->plaintext;
echo $dcell . ": " . $nextrow . ", " . $nextcol . "<br>\n";
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($nextcol,$nextrow, ($dcell));
$nextcol++;
}
$nextrow++;
}
}
// Rename sheet
echo date('H:i:s') . " Rename sheet <br>\n";
$objPHPExcel->getActiveSheet()->setTitle('HTMLTableOutput');
// redirect output to client browser
echo date('H:i:s') . " Write to Excel2003 format <br>\n";
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Output.xls"');
header('Cache-Control: max-age=0');
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save(str_replace('.php', '.xls', __FILE__));
//$objWriter->save('php://output');
// Echo done
echo date('H:i:s') . " Done writing file. <br>\r\n";
//}
?>
I don't know where the problem is originating, but I'm assuming the function is being redirected to the log in page of the site instead of the referring page. Anyone know how to fix this?