github/app/Services/TomTool/VisitorCount.php
2025-04-12 19:11:43 +08:00

56 lines
1.3 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
namespace App\Services\TomTool;
use App\Models\Visitor;
final class VisitorCount
{
public static function From()
{
// 如果以被设置,什么都不做
// 如果未被设置入库并保存cookie注册用
if (!isset($_COOKIE['VisitorFrom'])) {
if (isset($_SERVER['HTTP_REFERER'])) {
$sFrom = self::normalizeUrl($_SERVER['HTTP_REFERER']);
} else {
$sFrom = $_ENV['short_url'] . "/";
}
$oVisitorFrom = new Visitor();
$oVisitorFrom->from = $sFrom;
$oVisitorFrom->save();
setcookie('VisitorFrom', $sFrom, (time() + (86400 * 30)), "/");
}
}
public static function normalizeUrl($url) {
$parsedUrl = parse_url($url);
$host = isset($parsedUrl['host']) ? $parsedUrl['host'] : '';
if (strpos($host, 'www.') === 0) {
$host = substr($host, 4); // 去掉 'www.'
}
$path = isset($parsedUrl['path']) ? $parsedUrl['path'] : '';
return $host . $path;
}
}