56 lines
1.3 KiB
PHP
Executable File
56 lines
1.3 KiB
PHP
Executable File
<?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;
|
||
|
||
}
|
||
|
||
}
|