thebounder
thebounder

Reputation: 1

Images on my server inside a password protected directory

I have images inside a password protected directory, but I want to show some of these images on the normal un-protected site.

When I try and and display them with a simple:

<img src ='PASSWORD PROTECTED DIR/Image.jpg">

It is asking me to input the username and password.

However, I only want to input the username and password when I actually navigate to the content inside the password protected directory.

I'm using PHP.

Any ideas?

Upvotes: 0

Views: 1686

Answers (3)

NoLifeKing
NoLifeKing

Reputation: 386

Since it's PHP you can do like this in a separate file (imgHandler.php)

<?php
// Allowed files
$allowedfiles["image1"] = "./the_dir/Image.jpg";
$allowedfiles["image2"] = "./the_dir/Image2.jpg";

function getImage($path) {

    echo file_get_contents($allowedfiles[$path]);
}
getImage($_REQUEST['img']);
?>

And then for the images you want to show, just use:

<img src="imgHandler.php?img=image1" />

This would not prevent caching, and is much securer than the other solution I presented before.

Upvotes: 0

Pekka
Pekka

Reputation: 449613

Assuming you are using Apache.

You can exclude some files inside the .htaccess file that defines the password protection.

Untested, but this should work:

<Files "path/to/image.jpg"> 
  Allow from all
  Satisfy any
</Files>

but the much much preferable option is to simply place the image outside the protected directory.

Upvotes: 1

Nanne
Nanne

Reputation: 64419

without a lot of tricky code, this is basically unsolvable. The <img> command says that your user is going to request that information, which is not very different from requesting it directly.

You could start and check how the request was made, filter, make all the images loaded trough php file, etc, but with basic tools it is just not possible.

Requesting an image directly or trough an <img> tag is the same.

Upvotes: 0

Related Questions