943,746 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 793
  • PHP RSS
Aug 25th, 2009
0

Sessions, Headers & PDF

Expand Post »
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:

PHP Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ANicholson is offline Offline
3 posts
since Aug 2009
Aug 25th, 2009
0

Re: Sessions, Headers & PDF

Try
php Syntax (Toggle Plain Text)
  1. <?php ob_start(); ?>
to turn on output buffering (should be the 1st line of your code) and
php Syntax (Toggle Plain Text)
  1. <?php ob_flush(); ?>
as the last line to flush the buffered output.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Aug 25th, 2009
0

Re: Sessions, Headers & PDF

Just to clarify, like so?

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

If so, no joy
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ANicholson is offline Offline
3 posts
since Aug 2009
Aug 25th, 2009
0

Re: Sessions, Headers & PDF

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.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Aug 25th, 2009
0

Re: Sessions, Headers & PDF

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ANicholson is offline Offline
3 posts
since Aug 2009
Aug 25th, 2009
0

Re: Sessions, Headers & PDF

Wohoo! Good luck..
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Aug 25th, 2009
0

Re: Sessions, Headers & PDF

this is the code,check it out

php Syntax (Toggle Plain Text)

1.
<?php ob_start(); ?>
Reputation Points: -22
Solved Threads: 4
Junior Poster in Training
vincent2085 is offline Offline
53 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: I need to go 7 days into the future
Next Thread in PHP Forum Timeline: Include, cant make it work





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC