newbie: php problem in protected directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
salto
Forum Commoner
Posts: 31
Joined: Thu Jul 04, 2002 7:50 am

newbie: php problem in protected directory

Post 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.
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

what are the file permissions on the folder?
salto
Forum Commoner
Posts: 31
Joined: Thu Jul 04, 2002 7:50 am

Post 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.
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post 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.
salto
Forum Commoner
Posts: 31
Joined: Thu Jul 04, 2002 7:50 am

Post 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
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post 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.
salto
Forum Commoner
Posts: 31
Joined: Thu Jul 04, 2002 7:50 am

Post 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.
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

Try this:

Code: Select all

$file = substr($HTTP_REFERER, strrpos($HTTP_REFERER,"/")+1);

$fd= fread(fopen($file, "r"), 100000);
Last edited by Wayne on Thu Jul 04, 2002 10:41 am, edited 2 times in total.
salto
Forum Commoner
Posts: 31
Joined: Thu Jul 04, 2002 7:50 am

Post by salto »

Thanks, I tried, see what happens

(http://www.purplespirit.com/test/test.htm)
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post 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.
salto
Forum Commoner
Posts: 31
Joined: Thu Jul 04, 2002 7:50 am

Post 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:&nbsp;<a href="'.$HTTP_REFERER.'"> '.$HTTP_REFERER.' </a></i></p>
<p>'.$datum.'</p>
</font>
</body>
</html>';
?>
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post 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);
salto
Forum Commoner
Posts: 31
Joined: Thu Jul 04, 2002 7:50 am

Post 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!
Post Reply