Web applications 加载图像时运行脚本

Web applications 加载图像时运行脚本,web-applications,Web Applications,在加载图像时,是否有方法运行代码 例如,如果我的图像位于www.mydomain.com/image.png,并且有人执行此链接,则图像将显示为图像文件。但是,当有人试图查看图像时,我可以运行一组代码吗 喜欢 当我打开并运行www.mydomain.com/image.png时,它应该执行如下操作 <?php mail("myemail@mydomain.com", "image opened", "image opened by".$Server['addr']); ?>

在加载图像时,是否有方法运行代码

例如,如果我的图像位于www.mydomain.com/image.png,并且有人执行此链接,则图像将显示为图像文件。但是,当有人试图查看图像时,我可以运行一组代码吗

喜欢

当我打开并运行www.mydomain.com/image.png时,它应该执行如下操作

<?php
  mail("myemail@mydomain.com", "image opened", "image opened by".$Server['addr']);
?>

我发现网上提供了多个这样的服务,但当试图执行图像文件时,我无法确定如何执行代码文件

谁能给我一个简单的想法,这将如何锻炼?我可以自己对图像元素的使用进行编码

客户端:

<img src="..." onload="JavaScript_Code">
服务器端(index.php):


浏览一番之后,最好的处理方法可能是使用(如果有的话)

建立一个像这样的规则

# Enable Rewriting  
RewriteEngine on  

# Rewrite user URLs  
#   Input:  images/image.png/  
#   Output: images.php?f=image.png  
RewriteRule ^images/(\w+\.png)/?$ images.php?f=$1  
然后,使用中给出的示例为图像提供服务:

<?php

$filename = $_GET['f'];

if (!file_exists($filename))
{
    header('HTTP/1.0 404 Not Found');
    include('image404.php'); //or something similar if you want a nice error message
    exit();
}
else
{
    mail("myemail@mydomain.com", "image opened", "image opened by".$Server['addr']);

    $fp = fopen($_GET['f'], 'rb');

    header("Content-Type: image/png");
    header("Content-Length: " . filesize($name));

    fpassthru($fp);
}

请参阅。您需要将Web服务器设置为使用php解释器处理
.png
文件。@RoadieRich-感谢您的回复。这是一个相关的问题,但我认为在我这一部分还有更多的东西。在链接的问题OP中询问file.php?img=1,而我的是运行一个图像文件。您可以设置。在这种情况下,请将php设置为handle
.png
s,为脚本提供.png扩展名,并将实际图像放在公众无法访问的地方。@RoadieRich-嗯,我需要对此做一些研究工作。这可能会奏效。如果您有许多图像需要这样做,我建议您使用类似
images.php?f=image.png
——否则您需要为每个图像复制脚本。另一个选项是使用类似于
mod_rewrite
的东西将
mydomain.com/images/image.png
翻译成
mydomain.com/images.php?f=image.png
。有关这方面的示例,请参见。如何将onload函数写入PNG文件?我说我想把这个文件称为www.mydomain.com/image.png。只有在启用javascript的浏览器中将图像加载到该网页上时,这个文件才有效。以任何其他方式加载图像(例如,如果它在社交媒体上共享)都会绕过代码。这取决于@Kerry是否想要这样做。刚才在Google上读到一篇文章,PHP代码可以使用GIF文件执行。只是想知道是否有人可以帮助我如何实现它。希望我需要问一个新问题。我以为您正在将图像加载到web文档中,所以我采用了客户端方式。您应该将图像放在服务器端脚本后面。@Kerry您可以在Apache配置文件中使用php application server分配png文件类型,但我认为使用php handler分配所有png文件不是一个好主意!
# Enable Rewriting  
RewriteEngine on  

# Rewrite user URLs  
#   Input:  images/image.png/  
#   Output: images.php?f=image.png  
RewriteRule ^images/(\w+\.png)/?$ images.php?f=$1  
<?php

$filename = $_GET['f'];

if (!file_exists($filename))
{
    header('HTTP/1.0 404 Not Found');
    include('image404.php'); //or something similar if you want a nice error message
    exit();
}
else
{
    mail("myemail@mydomain.com", "image opened", "image opened by".$Server['addr']);

    $fp = fopen($_GET['f'], 'rb');

    header("Content-Type: image/png");
    header("Content-Length: " . filesize($name));

    fpassthru($fp);
}