如何显示由http身份验证保护的图像


How to show an image which is secured by http authentication

如何显示一个图像从服务器与标准http保护而不显示身份验证窗口?

我现在使用标准的html

<img src="...">

,但是因为图像是受保护的,所以需要一个身份验证窗口。我有登录数据,如何显示图像?

问候,汤姆。

应该可以。只需将用户名和密码替换为您的身份验证详细信息。(警告:并非在所有浏览器中都有效)

<img src="http://username:password@server/Path" />

我建议把它放在服务器上的一个单独文件中。这样就可以在不暴露身份验证信息的情况下引用它。

我使用了IrishGeeks的提示来得到一个解决方案。它适用于所有浏览器。script.php是

<?php
$url    = $_GET['url'];
$c = curl_init($url);
$authString = 'user:pass';
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_USERPWD, $authString);
$content = curl_exec($c);
$contentType = curl_getinfo($c, CURLINFO_CONTENT_TYPE);
header('Content-Type:'.$contentType);
print $content;
?>

然后使用

<?php
print '<img src="script.php?url='.urlencode('http://www.example.com/image.bmp').'" />';
?>

获取图像