fwdkr.co/dist/goatcounter.php

41 lines
1.1 KiB
PHP

<?php
/**
* Sends a request to this website's GoatCounter server to record a hit.
*
* @param $path string the path to record a hit for
*/
function goatcounter_record_hit(string $path)
{
if (isset($_SERVER["HTTP_USER_AGENT"])
&& preg_match("/bot|crawl|slurp|spider|mediapartners/i", $_SERVER["HTTP_USER_AGENT"])) {
return;
}
$token = file_get_contents(".goat.token");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://stats.fwdkr.co/api/v0/count");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
json_encode(array(
"hits" => array(array(
"path" => $path,
"ip" => $_SERVER["REMOTE_ADDR"],
"ref" => $_SERVER["HTTP_REFERER"] ?? "",
"user_agent" => $_SERVER["HTTP_USER_AGENT"]
))
))
);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
"Content-Type: application/json",
"Authorization: Bearer $token"
)
);
curl_exec($ch);
curl_close($ch);
}