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

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

图片统计大概可以这样。
/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来分析。

你的代码只能统计动态的请求,对于静态的 url 就无能为力了,比如 http://www.mydomain,com/1.jpeg
正确的做法是分析 web 服务器的日志文件



php分析access_log? 那个log每天都有近百MB,如果cron脚本每5分钟运行一次,怎么高效的读取log文件?(时间段:当前时间-5分钟,到当前,那么是不是也要完整读取整个log文件?)
还有怎么做foreach?按/r/n换行还是别的?然后再正则,再放进数据库?

大侠,能不能给段高效点的代码?谢谢。

楼上讲的对,如果你的图片全部都是用动态php去显示的,你的程序已经可以了。加上$_SERVER['HTTP_REFERER']获取来源。
而静态图片则可以通过获取apache log来分析。



哦,谢谢。小问一下,php动态读取和静态读取图片相比,是不是会花费更多的CPU,内存和IO?是不是会增加许多?

1、静态文件由 web 服务器直接读取,而php动态读取则需要先启动 php 解析程序,再由 php 程序读取
     板板脚趾头都会知道谁的效率高
2、日志文件是只增不减的,并且已存在的内容也不会被修改。所以你只需记住上一次读到哪里,这次接着读就可以了

??快很多,
php?取?慢。
最好用??。

并不是所有的虚拟主机 日志存放路径 可以自行修改的。

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

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


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

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

1、静态文件由 web 服务器直接读取,而php动态读取则需要先启动 php 解析程序,再由 php 程序读取
     板板脚趾头都会知道谁的效率高
2、日志文件是只增不减的,并且已存在的内容也不会被修改。所以你只需记住上一次读到哪里,这次接着读就可以了



明白了那么怎么写一段PHP代码分析日志文件呢?循环,正则读取,然后怎么才能记住上一次读取到哪儿呢?
SSH可以用类似 cat /var/log/httpd/access_log | grep "1.jpeg",打印出所有访问过1.jpeg的用户几百个MB的日志也就1秒钟
PHP不懂,还得讲究效率,占用资源少。

只知道nginx服务器可以配置referer防盗链,并且自定义log,将referer写进去
apache也肯定有
需要php分析log的话...定时导入到sql,然后清空log如何?

如果通过日志文件去分析图片使用情况,我觉得不是很可靠,就想刚才群主说的,日志文件只增不减,分析的话,准确度不好把握。

如果通过日志文件去分析图片使用情况,我觉得不是很可靠,就想刚才群主说的,日志文件只增不减,分析的话,准确度不好把握。



准确度的话,可以用 date_default_timezone_set () 为服务器的时区,然后用getdate();获取当前时间。crontabm每一分钟执行,只要正则出当前时间前一分钟的所有记录。

最主要的问题还是php如何高效的打开一个大文件的最后几条,可以多读取一点,设置每次读取为access_log的最后1000条记录。我的服务器有32GB内存,但是需要较低CPU开销。谢谢。

1、静态文件由 web 服务器直接读取,而php动态读取则需要先启动 php 解析程序,再由 php 程序读取
     板板脚趾头都会知道谁的效率高
2、日志文件是只增不减的,并且已存在的内容也不会被修改。所以你只需记住上一次读到哪里,这次接着读就可以了



找到一个方法,http://httpd.apache.org/docs/2.2/programs/rotatelogs.html
用rotatelogs每分钟生成一个新的log文件。但为什么重启apache后没有生成想要的日志呢?

<IfModule log_config_module>    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" com                                                                                        bined    LogFormat "%h %l %u %t \"%r\" %>s %b" common    <IfModule logio_module>      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I                                                                                         %O" combinedio    </IfModule>    CustomLog "logs/access_log" combined env=!dontlog    CustomLog "|sbin/rotatelogs -f logs/my_log 60" combined env=!dontlog    SetEnvIf Remote_Addr "127\.0\.0\.1" dontlog    SetEnvIf Remote_Addr "::1" dontlog</IfModule>

可能与 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
PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP: The Foundation of Many WebsitesPHP: The Foundation of Many WebsitesApr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

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.

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
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

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.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use