用于链接到图像的简单间接方法

发布于 2024-11-11 15:00:03 字数 89 浏览 2 评论 0原文

我有一个网站,其中托管图像可以直接共享和链接。我在某处读到这是一个坏主意。我如何应用简单的间接方法,同时保留现有链接一段时间,直到它们从 Facebook 上消失?

I have a web site which hosts images are shared and linked directly. I've read somewhere that this is a bad idea. How could I apply simple indirection approach while perhaps keeping existing links up for a while until they disappear off Facebook?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瀟灑尐姊 2024-11-18 15:00:03

下面是旧答案。

创建无法直接链接到的链接的示例:

$expire = 60 * 5; // 5 minutes;
$time = $_SERVER['REQUEST_TIME'] + $expire;
$image_id = $image_id;
$secretpassword = "secretpassword";

function generate_link($image_id, $time, $secretpassword) {
    $hash = md5($secretpassword . $time . $image_id);
    return "image.php?" . http_build_query(array(
        'image_id' => $image_id,
        'time' => $time,
        'hash' => $hash,
    ));
}

function is_link_valid($image_id, $time, $hash, $secretpassword) {
    if ($hash !== md5($secretpassword . $time . $image_id)) {
        return false; // hash is invalid
    }
    if ($time < $_SERVER['REQUEST_TIME']) {
        return false; // link has expired
    }
}

使用如下:

generate_link($image_id, $time, $secretpassword);
is_link_valid($_GET['image_id'], $_GET['time'], $hash, $secretpassword);

在生成的链接中放入两位信息:图像 ID 和链接到期的时间(因此您无法直接链接)。此外,您还添加了一个哈希值,这样就没有人可以“弄乱”变量,只有您知道如何生成哈希值(取决于 $secretpassword)。

这些链接会在 5 分钟后过期,或者您设置的 $expire 后过期。

一个问题是这些链接看起来不太漂亮。我知道让它们看起来更漂亮的方法,但这超出了这个问题的范围。


旧答案

我假设您的意思是您共享其他域(例如 Facebook)上的图像。

您描述的问题是您通过使用其他域的图像(和带宽)来“窃取”其他域。我不知道 Facebook 关于直接链接的政策,但对于较小的网站来说,这是一个真正的负担。添加间接并不能解决这个问题。

解决此问题的唯一方法是将图像上传到图像服务器,可以是免费的,也可以是您自己控制的服务器。您可以让您的用户执行此操作,或者您也可以为他们执行此操作。
如果您不控制图像服务器,则很难实现自动化,但拥有图像服务器可能会非常昂贵(带宽很昂贵)。让用户这样做会给他们带来负担,并且可能会降低您的网站的吸引力,因为他们需要执行额外的步骤。

我认为这些是你可以选择的。

Old answer below.

Example for making links that you can't direct link to:

$expire = 60 * 5; // 5 minutes;
$time = $_SERVER['REQUEST_TIME'] + $expire;
$image_id = $image_id;
$secretpassword = "secretpassword";

function generate_link($image_id, $time, $secretpassword) {
    $hash = md5($secretpassword . $time . $image_id);
    return "image.php?" . http_build_query(array(
        'image_id' => $image_id,
        'time' => $time,
        'hash' => $hash,
    ));
}

function is_link_valid($image_id, $time, $hash, $secretpassword) {
    if ($hash !== md5($secretpassword . $time . $image_id)) {
        return false; // hash is invalid
    }
    if ($time < $_SERVER['REQUEST_TIME']) {
        return false; // link has expired
    }
}

Use like:

generate_link($image_id, $time, $secretpassword);
is_link_valid($_GET['image_id'], $_GET['time'], $hash, $secretpassword);

You put two bits of information in the links you generate: the image id and the time when the link should expire (so you can't direct link). Additionally you add a hash so that no one can 'mess' with the variables, only you know how to generate the hash (depends on $secretpassword).

These links expire after 5 minutes, or whatever you set $expire to.

One issue is that these links don't look very pretty. I know ways to make them look prettier but that's beyond the scope of this question.


Old answer

I assume you mean that you share images that are on other domains (like Facebook).

The problem you describe is that you're 'leeching' off the other domains by using their images (and bandwidth). I don't know Facebook's policy about direct links but for lesser sites it's a real burden. Adding indirection isn't going to solve this problem.

The only way to solve it is to upload the images to an image server, either a free one or one that you control yourself. You could have your users do this, or you can do it for them.
Automating it is hard if you don't control the image server, but having an image server can be very costly (bandwidth is expensive). Having users do it places the burden on them and might make your site less attractive because they need to perform an extra step.

I think these are the options you have.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文