PHP Warning: Cannot modify header information - headers already sent in - caching

having difficulty trying to change the code below for PHP 8.0 which has the above error message:
public function sendNoCacheHeaders()
{
header_remove('Last-Modified');
header('Expires: Wed, 11 Jan 1984 05:00:00 GMT');
header('Cache-Control: no-cache, must-revalidate, max-age=0');
header('Pragma: no-cache');
}
}

Related

What is stopping browser to download pdf file

I am using spring boot application to download a jasper file, which is downloaded in my system, on browser. The code that I have written is:
#GetMapping("/download")
public ResponseEntity downloadFileFromLocal(HttpServletResponse response) throws JRException, SQLException, IOException {
Path path = Paths.get(System.getProperty("user.home") + "//downloads//" + "fileName.pdf");
Resource resource = null;
try {
resource = new UrlResource(path.toUri());
} catch (MalformedURLException e) {
e.printStackTrace();
}
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/download"))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
.body(resource);
}
This code is working perfectly fine and my file is being downloaded on browser when I try this code in a simple application i.e. practice approach. However, when I try to download the file in my main application with the same code then nothing happens. I am not sure what is stopping browser to download my pdf file in browser. The expected result is:
I think there is some problem with the response Headers.
Following is the response header for my Practice application file:
Accept-Ranges: bytes
Connection: keep-alive
Content-Disposition: attachment; filename="Daily%20Report%20For%20Site_ID%201.pdf"
Content-Length: 2869
Content-Type: application/download
Date: Mon, 27 Dec 2021 10:48:24 GMT
Keep-Alive: timeout=60
And for main application, response headers are:
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Length: 0
Date: Mon, 27 Dec 2021 11:28:33 GMT
Expires: 0
Pragma: no-cache
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

Laravel/Nginx - Customize Header request

I have simple laravel project. I recive data from GET url. Datas is sending from small device and i need set small/short header response for him. It's very important for optimization.
In laravel i don't use view, only Controller function.
Below is my full response:
Cache-Control: no-cache, private
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/html; charset=UTF-8
Date: Wed, 03 Oct 2018 06:58:24 GMT
Server: nginx/1.14.0 (Ubuntu)
Set-Cookie: XSRF-TOKEN=eyJpdiI6IkpZNEllbGt0YmtsVll6ajdCSVkrNHc9PSIsInZhbHVlIjoiOFhOakxDNUVka0tIRFwvTHdidXRwdjRHMWlFeEl5QzJIcUQ4VTJXTmkzXC9VZm5rRzI3ZjI3S2N2SEZ5aE1jVEluIiwibWFjIjoiNDkwMDU2MmI3NzdkMjA2MmVkNmUyMWZkMWNhN2YzNDA3ZDZlOGMwYzIyMDU1N2Q5ZjdlYzcxODg0NDJjZTkzZiJ9; expires=Wed, 03-Oct-2018 08:58:24 GMT; Max-Age=7200; path=/
Set-Cookie: laravel_session=eyJpdiI6ImpYZG5ZNk1yWnNQb1Y1RlRSOWQ3aUE9PSIsInZhbHVlIjoieW8ySFwvQWsySG1pSEFabGZRNkR5U0t5TUhSQzZsN3lFbG1zT29TSFdhdnFVQTczVWxSWXZsZnVPR216a2N3cE8iLCJtYWMiOiIyYmI3M2Q2YWUxMDAzOTYwNjRlOWMwYWVlZGRmMWI5NWY4N2EyNGQwZTVmODRkMThhOTQ0MGIzYzVlNGI0MWNjIn0%3D; expires=Wed, 03-Oct-2018 08:58:24 GMT; Max-Age=7200; path=/; httponly
Transfer-Encoding: chunked
What is the best solution? I use laravel 5.7, first i would like remove "Set-Cookie:" from one view only .

PHPExcel showing unreadable character

function districtexcelpdf(){
ob_start();
require_once('districtexcel.php');
$objPHPExcel = new PHPExceldis();
$objPHPExcel->districtExcel();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="District_report.xls"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
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');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
ob_clean();
$objWriter->save('php://output');
exit();
}
In above code I got a unreadable character . Required help to solve this problem.
ob_clean(); at the start because it sending a junk value then after use ob_start(); and at the end ob_clean();

File is not being downloaded on Laravel5 using Response

I use Laravel 5.1.
A jQuery ajax call is made like that:
$('#export_selected').click(function(){
var checked = $('.select_rows:checked');
var ids = [];
$.each(checked, function(index, value){
ids.push(value.id);
})
$.ajax({
method : "POST",
url : "{{URL::to('/spot/exportTable')}}",
data : {ids:ids}
});
});
And then the php method is defined that way:
public function exportTable(Request $req) {
$spots = array_flatten($req->all());
$res = Spot::whereIn('id', $spots)->get();
Excel::create('Spots', function($excel) use($res) {
$excel->setTitle('Title goes here');
$excel->setCreator('Creator Goes Here')->setCompany('Company Goes Here');
$excel->sheet('Excel sheet', function($sheet) use($res) {
$sheet->setOrientation('landscape');
$sheet->fromArray($res);
});
})->store('csv', storage_path('/exports', true));
$file = base_path() . '/storage/exports/Spots.csv';
$headers = ['Content-Type: application/csv', 'Content Description: File Transfer', 'Cache-Control: must-revalidate, post-check=0, pre-check=0'];
return response()->download($file, 'Spots.csv' , $headers);
}
Chrome developer console prints the results as raw lines.
The file is successfully exported and created in the disk.
The path to file is correct.
But the download is never started.
Echoing the response gives:
HTTP/1.0 200 OK
0: Content-Type: application/csv
Cache-Control: public
Content-Disposition: attachment; filename="Spots.csv"
Date: Mon, 26 Oct 2015 16:08:26 GMT
Last-Modified: Mon, 26 Oct 2015 16:08:26 GMT
1: Content-Description: File Transfer
2: Cache-Control: must-revalidate, post-check=0, pre-check=0
I put the answer here for everybody having the same issue.
#manix figured it out: I'm trying to download via Ajax, which needs to be done in another way, not the way I wrote my code

mediaelements.js MP3 can not wind

I have a problem with the playback of my MP3 files with mediaelements.js.
In my AJAX request I get my MP3 file and set the audio src.
If I get the real MP3 by URL like:
http://host.com/real/path/tp/mp3
and set this as my audio src the player works well.
Now...
When I request my MP3 through a script which gets the MP3 from my storage folder like this:
$headers = array(
'Content-Type' => File::mime(File::extension($mp3)),
'Content-Length' => filesize($mp3),
'Content-Transfer-Encoding'=>'',
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
);
return Response::make(readfile($mp3), 200, $headers);
The player start to play the MP3 file, but I can't wind it.: (
Response header of the real MP3 path:
HTTP/1.1 200 OK
Date: Tue, 26 Mar 2013 18:47:27 GMT
Server: Apache/2.2.16 (Debian)
Last-Modified: Tue, 26 Mar 2013 14:37:28 GMT
ETag: "8d4b852-7b7342-4d8d4dc53fa00"
Accept-Ranges: bytes
Content-Length: 8090434
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: audio/mpeg
Response header of my Laravel script which sends the MP3:
HTTP/1.1 200 OK
Date: Tue, 26 Mar 2013 19:02:33 GMT
Server: Apache/2.2.16 (Debian)
Content-Length: 6330826
content-transfer-encoding: binary
cache-control: must-revalidate, post-check=0, pre-check=0, private
Set-Cookie: laravel_session=16f4d83c0ce0262a90df3a0229f8b8ba261eaad7%2BDZIBuW8nbRbBMT7Nvu0NGQ9Rfqaiu6SAS 1wGjYse; expires=Tue, 26-Mar-2013 20:02:33 GMT; path=/; httponly
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: audio/mpeg
I had to set the following header:
"Accept-Range" => "bytes"
to work. Problem is fixed now. :)

Resources