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 i开发者_如何学Pythons 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?
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.
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.
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.
精彩评论