search
HomeBackend DevelopmentPHP Tutorialphp统计图片使用,反向链接等。解决方案

php统计图片使用,反向链接等。
最近想做一个统计,谁用了我网站的图片、链接等数据。

图片统计大概可以这样。
/var/www/html/1.jpg
/var/www/html/tracker.php
/var/www/html/.htacess


RewriteEngine On
RewriteBase /
RewriteRule ^(.*).jpg$ tracker.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]


header('Content-type:image/jpeg');
readfile($_GET['id'].'.jpg');
//file_put_contents('log.txt',$_GET['id'].' '.$_SERVER['REMOTE_ADDR'].' '.var_dump(apache_request_headers()));
?>

上面的代码可以统计图片被打开过几次,访问者的IP和浏览器等数据。但是如何统计反向链接呢? 比如:另一个网站使用了这张图片php统计图片使用,反向链接等。解决方案,那么怎样统计到底有多少网站使用了我的图片?(而不是单纯的浏览器打开)

另外,比如我制作一个小插件。允许用户嵌入该插件到他们的网站   那么script.php应该写一些怎样的代码,可以统计哪些网站使用了我的插件?

我只想知道代码怎么写?返回的数据与数据库的联系可以另外设计。谢谢。

------解决思路----------------------
怎样统计到底有多少网站使用了我的图片?(而不是单纯的浏览器打开)

只要統計http請求,不需要知道是否用瀏覽器打開。

你可以在tracker.php中加上$_SERVER['HTTP_REFERER'] 來獲取來源地址,即請求你這張圖的頁面的地址。
然後通過正則,獲取url的domain入庫。統計直接 group by photo 就可以了
表結構
id photo domain

------解决思路----------------------
你的代码只能统计动态的请求,对于静态的 url 就无能为力了,比如 http://www.mydomain,com/1.jpeg
正确的做法是分析 web 服务器的日志文件
------解决思路----------------------
楼上讲的对,如果你的图片全部都是用动态php去显示的,你的程序已经可以了。加上$_SERVER['HTTP_REFERER']获取来源。
而静态图片则可以通过获取apache log来分析。
------解决思路----------------------
1、静态文件由 web 服务器直接读取,而php动态读取则需要先启动 php 解析程序,再由 php 程序读取
     板板脚趾头都会知道谁的效率高
2、日志文件是只增不减的,并且已存在的内容也不会被修改。所以你只需记住上一次读到哪里,这次接着读就可以了

------解决思路----------------------
靜態快很多,
php讀取會慢。
最好用靜態。
------解决思路----------------------
并不是所有的虚拟主机 日志存放路径 可以自行修改的。

所以你开发的这个项目,只能用于 云服务器的站长, 当然,大家对图片给谁动用了,并不觉得有什么,不是吗?

中国的网络是开放的,没多少人会去在乎注意这些东西,应该应该本着共享的精神。


如果有一天,发现图片给动用得非常多了。那么只需要一个伪静态代码,就可以把访问图片引用变成一个logo或者其它精心制作的广告图,

你有没有发现有很多图片有时候显示“该图片来自 XXX站,请进入***访问”,那些都是伪静态,一句话就搞定了。只要别人引用了网站图片,那图片就会变成伪静态设定好的另外一张图片。


------解决思路----------------------
只知道nginx服务器可以配置referer防盗链,并且自定义log,将referer写进去
apache也肯定有
需要php分析log的话...定时导入到sql,然后清空log如何?
------解决思路----------------------
如果通过日志文件去分析图片使用情况,我觉得不是很可靠,就想刚才群主说的,日志文件只增不减,分析的话,准确度不好把握。
------解决思路----------------------
可能与 Apache 的版本有关 http://apache.chinahtml.com/logs.html

关于读取日志文件,你好像没有理解我的意思
日志文件是不定长记录文件,在没有索引的情况下,是无法定位到指定行的
不过自己构建索引文件也是没有必要的,毕竟日志里都是“旧闻”,读过了也就没必要再读了
文件函数集中有:
ftell -- 返回文件指针读/写的位置
fseek -- 在文件指针中定位

你只需在每次 fgets 之后用 ftell 读取偏移位置,并保存
下次再读时,取回上次保存的偏移位置,用 fseek 定位
就可接着读了

一定会有人说 fgets 一次一行效率太低,但用 fread 一次一大块时,尾部的半截行,处理起来也并非易事

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Beyond the Hype: Assessing PHP's Role TodayBeyond the Hype: Assessing PHP's Role TodayApr 12, 2025 am 12:17 AM

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

What are Weak References in PHP and when are they useful?What are Weak References in PHP and when are they useful?Apr 12, 2025 am 12:13 AM

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

Explain the __invoke magic method in PHP.Explain the __invoke magic method in PHP.Apr 12, 2025 am 12:07 AM

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.

Explain Fibers in PHP 8.1 for concurrency.Explain Fibers in PHP 8.1 for concurrency.Apr 12, 2025 am 12:05 AM

Fibers was introduced in PHP8.1, improving concurrent processing capabilities. 1) Fibers is a lightweight concurrency model similar to coroutines. 2) They allow developers to manually control the execution flow of tasks and are suitable for handling I/O-intensive tasks. 3) Using Fibers can write more efficient and responsive code.

The PHP Community: Resources, Support, and DevelopmentThe PHP Community: Resources, Support, and DevelopmentApr 12, 2025 am 12:04 AM

The PHP community provides rich resources and support to help developers grow. 1) Resources include official documentation, tutorials, blogs and open source projects such as Laravel and Symfony. 2) Support can be obtained through StackOverflow, Reddit and Slack channels. 3) Development trends can be learned by following RFC. 4) Integration into the community can be achieved through active participation, contribution to code and learning sharing.

PHP vs. Python: Understanding the DifferencesPHP vs. Python: Understanding the DifferencesApr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: Is It Dying or Simply Adapting?PHP: Is It Dying or Simply Adapting?Apr 11, 2025 am 12:13 AM

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

The Future of PHP: Adaptations and InnovationsThe Future of PHP: Adaptations and InnovationsApr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool