Sessions, Headers & PDF

Thread Solved

Join Date: Aug 2009
Posts: 3
Reputation: ANicholson is an unknown quantity at this point 
Solved Threads: 0
ANicholson ANicholson is offline Offline
Newbie Poster

Sessions, Headers & PDF

 
0
  #1
Aug 25th, 2009
Hi,

I'd be grateful for a little help. I have a script which generates invoices as PDF files and stores above just above www level (so they cannot be access directly via a URL).

I would like users to be able to access their own invoices but nobody elses.

My script as it stands is as follows:

  1. <?php
  2.  
  3. session_start();
  4.  
  5. if($_SESSION['auth']==false){
  6. header("HTTP/1.0 404 Not Found");
  7. exit();
  8. }
  9.  
  10. $invoice_id = $_GET['vid'];
  11. $user_id = $_SESSION['userid'];
  12.  
  13. if (is_numeric ($invoice_id))
  14. {
  15. require(db.php);
  16.  
  17. $q = $dbh->query(...);
  18. $n = $q->fetchColumn();
  19.  
  20. if($n==1){
  21.  
  22. //output pdf
  23.  
  24. $filename = "../invoices" . $invoice_id . ".pdf";
  25.  
  26. header('Content-type: application/pdf');
  27. header('Content-Disposition: attachment; filename="invoice.pdf"');
  28. readfile($filename);
  29.  
  30. }
  31.  
  32. }

I've posted a slimmed down the code here and changed a few variable names for security reasons but essentially its the same as what I'm working with.

The basic codes works. The problem arises when an authorised user calls the file. The PDF file is returned, it is not rendered correctly e.g. you see "%PDF-1.7 3 0 ...". I presume this is because session_start() acts like a header? Is there any way round this.
Last edited by ANicholson; Aug 25th, 2009 at 9:48 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Sessions, Headers & PDF

 
0
  #2
Aug 25th, 2009
Try
  1. <?php ob_start(); ?>
to turn on output buffering (should be the 1st line of your code) and
  1. <?php ob_flush(); ?>
as the last line to flush the buffered output.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 3
Reputation: ANicholson is an unknown quantity at this point 
Solved Threads: 0
ANicholson ANicholson is offline Offline
Newbie Poster

Re: Sessions, Headers & PDF

 
0
  #3
Aug 25th, 2009
Just to clarify, like so?

  1. <?php
  2. ob_start();
  3.  
  4. // .. all my other code
  5.  
  6. ob_flush();
  7.  
  8. ?>

If so, no joy
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Sessions, Headers & PDF

 
0
  #4
Aug 25th, 2009
Hmm.. I tried your code snippet and it worked fine. There is one thing that I'd always do after having a header, an exit. This might or might not fix the problem, but try having an exit after readfile function.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 3
Reputation: ANicholson is an unknown quantity at this point 
Solved Threads: 0
ANicholson ANicholson is offline Offline
Newbie Poster

Re: Sessions, Headers & PDF

 
0
  #5
Aug 25th, 2009
ah ha, that does fix it .

was appearing not to work due to another error - which I've now fixed - dodgy URL rewrite.

thank you.
Last edited by ANicholson; Aug 25th, 2009 at 10:06 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Sessions, Headers & PDF

 
0
  #6
Aug 25th, 2009
Wohoo! Good luck..
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 53
Reputation: vincent2085 can only hope to improve 
Solved Threads: 4
vincent2085 vincent2085 is offline Offline
Junior Poster in Training

Re: Sessions, Headers & PDF

 
0
  #7
Aug 25th, 2009
this is the code,check it out

php Syntax (Toggle Plain Text)

1.
<?php ob_start(); ?>
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC