PHP 检查链接是否用作页面资产或直接访问

问题描述 投票:0回答:1

我有一个脚本image.php,它处理图像文件并返回结果。该链接可以如下所示:

https://example.com/image.php?id=123
,它会在数据库中找到相应的文件并将其返回以供使用:

<img src='https://example.com/image.php?id=123' alt='' />

image.php文件是否可以以某种方式区分在如上所示的网站上使用时以及用户单击“在新选项卡中显示图像”或只是将链接直接插入地址栏时的情况?

php image assets
1个回答
0
投票

<?php // Check if the HTTP referer header is set if (isset($_SERVER['HTTP_REFERER'])) { // The script is being accessed as part of a webpage // You can handle this case accordingly // For example, return an error message or redirect to a webpage header('HTTP/1.1 403 Forbidden'); exit('Direct access to this script is not allowed.'); } else { // The script is being accessed directly // You can process the image request here // For example, fetch the image data from the database based on the provided ID and output it // Sample code: $id = $_GET['id']; // Assuming you're using GET parameter 'id' to identify the image // Fetch image data from the database based on $id // Output the image data (e.g., image/jpeg, image/png, etc.) using appropriate headers } ?>

© www.soinside.com 2019 - 2024. All rights reserved.