Page 1 of 1
newbie: php problem in protected directory
Posted: Thu Jul 04, 2002 7:50 am
by salto
In a via .htaccess password protected directory (Apache version 1.3.14/PHP 4.1.2)) I get error
'Supplied argument is not a valid File-Handle resource'
in this line of my php script:
$fd= fread(fopen($HTTP_REFERER, "r"), 100000);
What should I do to get this work in the password protected directory?
HTTP_REFERER echo is OK.
Script is in same directory. Works fine in any other (not protected) directory
Am newbie, help most wanted.
Thanks for your attention.
Posted: Thu Jul 04, 2002 8:09 am
by Wayne
what are the file permissions on the folder?
Posted: Thu Jul 04, 2002 8:19 am
by salto
Thanks for your reply
Permission is same as all dir either password protected or not: rwxr-xr-x
Script has no problem in any direcotry except for in the password protected one.
Posted: Thu Jul 04, 2002 9:02 am
by Wayne
The script should work even in a htaccess protected folder, can you give us an example of the URL you are using in the browser to call the page. And just make sure that the page exists in the folder.
Posted: Thu Jul 04, 2002 9:35 am
by salto
Thanks for your response.
OK, I put the same script and a testfile in a test dir @
http://www.purplespirit.com/test/test.htm
username: test
password:PHPDN
Click link 'printerfriendly version' and see the error on line 2.
Line 2 in my script is:
$fd= fread(fopen($HTTP_REFERER, "r"), 100000);
No such error when I put this file and script in a not protected dir
I guess I need a wrapper for this script to get it straight in the password protected dir
Posted: Thu Jul 04, 2002 9:46 am
by Wayne
Its a work around but why dont you strip the HTTP_REFERER header and perform an fopen on the filesystem instead of the URL.
Posted: Thu Jul 04, 2002 9:52 am
by salto
Because I am a newbie! I never thought of that and
I would not know how to do a fopen on the filesystem in order to accomplish the same effect.
Posted: Thu Jul 04, 2002 9:55 am
by Wayne
Try this:
Code: Select all
$file = substr($HTTP_REFERER, strrpos($HTTP_REFERER,"/")+1);
$fd= fread(fopen($file, "r"), 100000);
Posted: Thu Jul 04, 2002 10:00 am
by salto
Posted: Thu Jul 04, 2002 10:04 am
by Wayne
you are closing your quotes in the wrong place. check them.
And when you put the back link in just append
http://www.purplespirit.com to it.
Posted: Thu Jul 04, 2002 10:17 am
by salto
Thanks for your patiende. Yes, maybe I did mess up, but counting and checking did not give me any clue.
Here is the full script:
<?php
$file = substr($REQUEST_URI, strrpos($REQUEST_URI,"/")+1);
$fd= fread(fopen($file, "r"), 100000);
if ($fd)
{
$start= strpos($fd, "<!--tekst-->");
$finish= strpos($fd, "<!--/tekst-->");
$length= $finish-$start;
$code=Substr($fd, $start, $length);
$start= strpos($fd, "<title>");
$finish= strpos($fd, "</title>");
$length= $finish-$start;
$titel=Substr($fd, $start, $length);
$start= strpos($fd, "<!--updated-->");
$finish= strpos($fd, "<!--/updated-->");
$length= $finish-$start;
$datum=Substr($fd, $start, $length);
}
echo '<html>
<head>
'.$titel.'</title>
</head>
<body bgcolor="silver" leftmargin="30" topmargin="20" rightmargin="30" bottommargin="20" marginwidth="30" marginheight="20" link="#0099cc" vlink="#000066" alink="#9966cc">
<font face="Arial, Helvetica, sans-serif" color="purple">
'.$code.'
<p align="left"><i>back to original version: <a href="'.$HTTP_REFERER.'"> '.$HTTP_REFERER.' </a></i></p>
<p>'.$datum.'</p>
</font>
</body>
</html>';
?>
Posted: Thu Jul 04, 2002 10:37 am
by Wayne
Sorry my mistake replace this line
Code: Select all
$file = substr($REQUEST_URI, strrpos($REQUEST_URI,"/")+1);
with
Code: Select all
$file = substr($HTTP_REFERER, strrpos($HTTP_REFERER,"/")+1);
Posted: Thu Jul 04, 2002 10:42 am
by salto
Great! It works! Thank you so much, this has kept me busy for the last 20 hours. Thanks for you attention and kind help!