设备指纹有两种方案,取决于你的场景: b ┌───────────────────────────────────┬───────┬────────────────┐ │ 方案 │ 精度 │ 场景 │ ├───────────────────────────────────┼───────┼────────────────┤ ────────────────────────────── │ FingerprintJS Pro(客户端+服务端)│99.5% │ 反欺诈、风控 │ ├───────────────────────────────────┼───────┼────────────────┤ │ DeviceDetector(纯服务端 UA 解析)│ 中等 │ 设备分类、统计 │ └───────────────────────────────────┴───────┴────────────────┘ --- 方案一:FingerprintJS Pro(推荐,高精度风控) 客户端采集 → 服务端验证,精度最高。composerrequire fingerprintjs/fingerprint-pro-server-api-php-sdk 前端(一行引入):<scriptsrc="https://fpjscdn.net/v3/YOUR_PUBLIC_KEY/iife.min.js"></script><script>FingerprintJS.load().then(fp=>fp.get()).then(r=>{// 将 r.visitorId 和 r.requestId 发给后端 fetch('/api/verify',{method:'POST', body: JSON.stringify(r)});});</script>Hyperf 服务端验证:<?php namespace App\Controller;use Fingerprint\ServerAPI\Api\FingerprintApi;use Fingerprint\ServerAPI\Configuration;use GuzzleHttp\Client;use Hyperf\HttpServer\Annotation\Controller;use Hyperf\HttpServer\Annotation\PostMapping;#[Controller(prefix: '/api')]class FingerprintController{private FingerprintApi$client;publicfunction__construct(){$config=Configuration::getDefaultConfiguration(env('FINGERPRINT_SECRET_KEY'));$this->client=new FingerprintApi(new Client(),$config);}#[PostMapping(path: 'verify')]publicfunctionverify(): array{$requestId=$this->request->input('requestId');[$event,$error]=$this->client->getEvent($requestId);if($error){return['risk'=>true];}$identification=$event->getProducts()->getIdentification()->getData();return['visitorId'=>$identification->getVisitorId(),'confidence'=>$identification->getConfidence()->getScore(),'incognito'=>$identification->getIncognito(),'ip'=>$identification->getIp(),];}}--- 方案二:DeviceDetector(纯服务端,免费) 适合设备分类、UA 解析,无需客户端 JS。composerrequire matomo/device-detector 封装为 Hyperf Service:<?php namespace App\Service;use DeviceDetector\DeviceDetector;use DeviceDetector\Cache\PSR6Bridge;use Psr\SimpleCache\CacheInterface;class DeviceFingerprintService{publicfunction__construct(private CacheInterface$cache){}publicfunctiondetect(string$userAgent): array{$dd=new DeviceDetector($userAgent);$dd->setCache(new PSR6Bridge($this->cache));$dd->parse();return['fingerprint'=>md5($userAgent.request()->ip()),'device_type'=>$dd->getDeviceName(), // desktop/tablet/smartphone'brand'=>$dd->getBrandName(),'model'=>$dd->getModel(),'os'=>$dd->getOs('name'),'browser'=>$dd->getClient('name'),'is_bot'=>$dd->isBot(),];}}控制器调用:#[GetMapping(path: 'device')]publicfunctiondevice(): array{return$this->deviceService->detect($this->request->header('User-Agent'));}--- 自建轻量指纹(无第三方依赖) 如果只需要简单的请求指纹(IP + UA + Accept-Language 组合): publicfunctionfingerprint(RequestInterface$request): string{$raw=implode('|',[$request->getHeaderLine('User-Agent'),$request->getHeaderLine('Accept-Language'),$request->getHeaderLine('Accept-Encoding'),$request->server('remote_addr'),]);returnhash('xxh3',$raw);// PHP8.1+ 内置,极快}--- 选型建议: - 反欺诈/风控 → FingerprintJS Pro(付费但精度无敌) - 设备统计/适配 → DeviceDetector(免费,纯服务端) - 简单会话绑定 → 自建 hash(零依赖)