我正在开发一个用户可以上传图片的网站。
只允许用户在登录时查看图像。
我可以看到Facebook通过photo.php渲染私人照片这样做:
<img src="https://www.facebook.com/photo.php?fbid=10231522709593244&set=a.10150407561663244.3514699.73368243&type=1">
我可以做同样的事情:
photo.php:
if ($user_logged_in)
{
$filename='/home/protectedfolder/'.$_GET['fbid'].'.jpg'; // you got the idea...
header("content-type: image/jpg");
header('Expires: 0');
readfile($filename);
}
......它有效,但它非常慢,即使服务器功能非常强大。
我怎样才能以更有效的方式做到这一点?我尝试过使用file_get_contents和cURL,但这没有用。我也试图启用兑现,但似乎被忽略了。
我是否需要更改apache配置或是否需要额外的apache模块?
答案 0 :(得分:0)
如果用户是或未登录,您应该考虑使用PHP将css值(图1)添加到您的头部,然后向您的.htaccess
添加脚本(图2)以防止其他网站显示您的图片。
这比尝试让PHP读取文件快得多。
图1:
<head>
...
<?php
if(!$is_logged_in) {
echo '<style>.private_image { display:none; }</style>';
}
</head>
图2:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
此外:
删除或替换页面上的图像标记而不是CSS值可能是个好主意。
答案 1 :(得分:0)
您可能已启用输出缓冲。
尝试此操作以禁用缓冲:
if ($user_logged_in)
{
$filename='/home/protectedfolder/'.$_GET['fbid'].'.jpg'; // you got the idea...
header("content-type: image/jpg");
header('Expires: 0');
ob_end_flush();
readfile($filename);
}
输出缓冲意味着,服务器在将整个数据发送到浏览器之前将其缓存在内存中。