david - 2009-11-26 17:01:49
This script worked fine for the past 2 years and then suddenly stopped working. I have a wordpress blog with a member area and I want the content (pdf files, swf files and so on) to be available only for the subscribers.
What the script does is simple
1) The require commands load the wordpress functions
2) The function is_user_logged_in (from wordpress) check if the user
is logged in
3) If the answer is yes, than the script will fetch the url from the
address bar and open it as a pdf file (or swf or whatever depending
on the content type).
4) If the user is not logged in, than the script will write an html
line on the screen asking the user to log in to see the content.
This script is called by .htaccess where I added the following line
AddHandler protect1 .pdf
Action protect1 /..../namescript.php
So, all the files in that directory with extension. pdf will be open
using the script namescript.php.
It has always worked in the past 24 months and now it doesn't work any
longer. What happens instead is that
1) If the user is logged in, the browser opens a new tab but no pdf
appears on screen (Google Chrome opens acrobat reader with an error
message saying that the file is not a pdf).
2) If the user is not logged in the browser says "page not found".
Playing with the script I tried to
- check if the user is logged in and if he is, it will
than open a specified pdf file. This script works fine alone from the
address bar but has problems together with htaccess.
- don't check if the user is logged in. It uses the pdf file
called in the address bar (with the fetch function) and it works fine
with htaccess!
We can conclude that
1) Wordpress function checking if user is logged in works
2) The fetch function works
3) The fetch function with htaccess works
4) checking if user is logged in together with htaccess DOES NOT work (ANYMORE).
I also tried to split the script into 2 parts in order to have the header command right at the top but with no luck
David
I think the problem is with the header command having some output written before it (but why did it work fine on all browser for the past 2 years?)
PS: This script still works with Safari.
Here is the script (I tried with readfile and with fread(fopen)
<?php require('/...../wp-blog-header.php'); ?>
<?php if (is_user_logged_in()) {
header('Content-type: application/pdf');
$url = '/...../' . $_SERVER['REQUEST_URI'];
readfile($url);
} else {
echo '<html><body>not logged in</body></html>';
} ?>
or split into 2
<?php
header('Content-type: application/pdf');
include('/...../namecode.php');
echo(fread(fopen($url, "r"),filesize($url)));
// readfile($url);
?>
namecode.php
<?php
require('/...../wp-blog-header.php');
if (is_user_logged_in())
{
$url = '/....../' . $_SERVER['REQUEST_URI'];
}
else
{
echo '<html><body>not logged in</body></html>';
} ?>