php统计图片使用,反向链接等。解决方案
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和浏览器等数据。但是如何统计反向链接呢? 比如:另一个网站使用了这张图片,那么怎样统计到底有多少网站使用了我的图片?(而不是单纯的浏览器打开)
另外,比如我制作一个小插件。允许用户嵌入该插件到他们的网站 那么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 一次一大块时,尾部的半截行,处理起来也并非易事

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
