如何显示图像隐藏其直接链接在php


How to show image hidding its direct link in php?

我如何创建一个php代码,将处理一个ID,例如,返回相应的图像,而不提供客户端实际的图像hotlink。

在下面的链接上实现,图像显示在html页面中,但是热链接对客户端是隐藏的。即使在新窗口打开图像,显示的链接也不是原来的链接。

这绝对不是在。htaccess文件中实现的,因为对于每个带有id的图像URL都呈现相应的图像,而不是重定向到单个页面。

http://www.imagesup.net/?di=15140291437113

一个基本的方法可能是这样的

<?php
    // query to database mabye?
    $myPath = getImagePath($_GET['id']);
    // what kind of image file is that?
    $contentType = getContentType($myPath);
    // read file content
    $img = file_get_contents($myPath);
    // here you tell the browser what kind of image is that e.g. jpeg,png,gif...
    header("Content-Type: $contentType");
    echo $img;
?>

您需要根据需要定义getImagePathgetContentType函数

每当一个冲浪者进入我的网站,我总是分配一个会话给他。会话包含一些信息,例如:代理、IP、语言、日期等,并通过cookie或GET(作为我的每个页面的参数)传递。

由于我处理大量的图像内容,我开始建立我的收藏数据库。这基本上意味着出于管理和集群目的,我将所有映像保存到一个SQL数据库中,该数据库是多宿主的,分布在多个服务器上。有人可能会争论这样做是否明智,但我们可以在另一天的另一篇文章中争论。

我写了一个小脚本,在我的网站上使用:

<img src="http://example.com/display.php?id=34" border="0" alt="" />

当然有一个不断变化的ID。这是在数据库中引用我的图像的部分。下面是我用来从数据库中检索图像的脚本代码:

<?php
$connection=@mysql_connect(...);
@mysql_select_db(...);
$query="SELECT mime, file FROM images 
WHERE id=".$_GET["id"];
$rawdb=@mysql_query ($query,$connection);
if($rawdb AND @mysql_num_rows($rawd-->0){
  $array=@ mysql_fetch_array($result);
  if (!empty($array["fileContents"])){
    // Output the MIME header
    header("Content-Type: ".$array["mime"]}");
    // Output the image
    echo $array["file"];
  }else{
    // something else...
  }
  @mysql_free_result($rawdb);
}else{
  // something else...
}
@mysql_close($connection);
?>

因为我已经为每个访问我网站的用户设置了一个会话,所以我只添加了以下内容:

<img src="http://example.com/display.php?id=34&sid=383829" border="0" alt="" />

并在脚本中实现一个小的会话检查:

<!--
session_start();
if($_SESSION["is_known"]){
  // do database calls
}else{
  header("Location:http://mydomain.tld/dontsteal.html");
}
-->

我的方法的主要优点是,会话完全是服务器端。用户无法摆脱它,或者伪造信息。因为我有一个超时并保存了所有必要的信息(IP!)来验证,所以对我来说它看起来非常完美,符合我的需求。

这里的挫折之一是资源和性能。但既然我没有强迫你,你可以测试和评估。希望这对你有帮助!

创建一个php脚本,在img标签中作为src使用。

在该脚本中,使用file_get_contents从图像中获取数据。然后用正确的mime类型发送报头。例如:header('Content-type: image/jpeg');然后输出数据