Today I learned for the first time how to create a simple login system (with thanks StackExchange tips and THIS TUTORIAL. What i’m trying to do is use the PHPExcel framework to generate an Excel file ONLY for those who successfully login.
I’ve gotten very close, however I’m getting a crazy message about headers and below that is a bunch of jargon mixed up text.
Warning: Cannot modify header information - headers already sent by (output started at /home3/mydir/public_html/d23/members/loggedin.php:6) in /home3/mydir/public_html/d23/members/create_excel.php on line 92
Warning: Cannot modify header information - headers already sent by (output started at /home3/mydir/public_html/d23/members/loggedin.php:6) in /home3/mydir/public_html/d23/members/create_excel on line 93
Warning: Cannot modify header information - headers already sent by (output started at /home3/mydir/public_html/d23/members/loggedin.php:6) in /home3/mydir/public_html/d23/members/create_excel on line 94
ÐÏࡱá;þÿ þÿÿÿ ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
I know that the error is coming from these lines in the “create_excel.php” file, because when I comment them out, there is no error. But there is also no excel file creation.
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="userList.xls"');
header('Cache-Control: max-age=0');
I will outline my process step by step- i’m a beginner so I bet this is stupidly easy for someone.
STEP #1 (login.php)
Calls the “process_login.php” file to authenticate the password typed into the form.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Login Please</title>
</head>
<body>
<form action="process_login.php" method="post">
Password: <input type="password" name="password" /><br />
<input type="submit" value="Login" />
</form>
</body>
</html>
PAGE 2 (process_login.php)
If everything is fine,it will now re-direct you to “logged_in.php”, if not- you will get a denied message.
mysql_connect($host, $user, $pass);
mysql_select_db($database);
//$username = mysql_real_escape_string($_POST['username']);
$username = "person";
$password = hash('sha512', $_POST['password']);
$result = mysql_query("SELECT * FROM $table WHERE password = '$password'
");
if(mysql_num_rows($result))
{
// Login
session_start();
$_SESSION['username'] = htmlspecialchars($username); // htmlspecialchars() sanitises XSS
// Redirect
header('Location: loggedin.php');
exit;
}
else
{
echo '<p><strong>Error:</strong> Invalid username or password.</p>';
}
?>
STEP 3 (logged_in.php)
Now we have been successfully logged in, with an approval notice as well as a request to run “create_excel.php” file.
<?php
session_start();
if(isset($_SESSION['username']))
{
// Logged in
echo '<p>You are logged in as '. $_SESSION['username']. '.</p>';
// require the PHPExcel file
require 'create_excel.php';
}
else
{
// Not logged in
echo '<p>You are not <a href="login.php">logged in</a>.</p>';
}
?>
STEP #4 (create_excel.php)
This is where the header problem happens. I’ve tried to put the session script on this page alone, but I get the same problem. I was thinking if it was pulled from the “logged_in” page everything would be fine- but no. (Currently if I access this page directly the Excel file is generated and downloaded no problem).
<?php
/** Error reporting */
error_reporting(E_ALL);
date_default_timezone_set('Europe/London');
/** PHPExcel */
require_once 'Classes/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!');
// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A4', 'Miscellaneous glyphs')
->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç');
// 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);
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
There lies your problem. You’re sending output to the client and then you want to change the headers and do something else. It doesn’t work that way.
Consider doing it like this:
Or just output the excel file without the login message: