M
O
O
N
I
T
O
Integrate Analytics API with PHP Applications
Here is the documentation guide on integrating Analytics API into your PHP application or website. In this case, we assume that you have a PHP-based website application that you have uploaded to your server.
- First, sign up for the domain you want to protect and learn about its visitors on the Analytics page.
- Login into your server or hosting.
- Open your main web dir (e.g.,
/var/www/
) or the directory that you already specified. - Create a PHP file (e.g.,
analytics.php
) and then fill it with the following code:
<?php
/**
* Configuration
*/
$apiPublicKey = 'Your API Public Key'; // Replace with your API Public Key
$apiSecretKey = 'Your API Secret Key'; // Replace with your API Secret Key
/**
* Client IP Address Retrieval
* If Cloudflare header is present, use it as the client's IP address
*/
if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
$_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
}
$clientIp = filter_var($_SERVER['HTTP_CLIENT_IP'] ?? $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP) ?? '';
/**
* Analytics API Request
*/
// Initialize cURL session
$curl = curl_init();
// Set cURL options
curl_setopt_array($curl, [
CURLOPT_URL => 'https://moonito.net/api/v1/analytics?' . http_build_query([
'ip' => $clientIp,
'ua' => urlencode($_SERVER['HTTP_USER_AGENT']),
'events' => urlencode($_SERVER['REQUEST_URI']),
'domain' => strtolower($_SERVER['HTTP_HOST']),
]),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => [
'User-Agent: Mozilla/5.0 (Linux; Android 13; SM-G991U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Mobile Safari/537.36',
'X-Public-Key: ' . $apiPublicKey,
'X-Secret-Key: ' . $apiSecretKey,
],
]);
// Execute cURL request
$response = curl_exec($curl);
// Close cURL session
curl_close($curl);
// Decode JSON response
$response = json_decode($response);
// Process $response as needed
if ($response['data']['status']['need_to_block'] ?? false) {
// Perform an action when the visitor is detected as "need_to_block"
// For example, return 403 Forbidden
http_response_code(403);
exit(); // Ensure script execution stops after sending the HTTP response
}
- Input your Secret and Public API Key, then save.
$apiPublicKey = 'Your API Public Key'; // Put your API Public Key here
$apiSecretKey = 'Your API Secret Key'; // Put your API Secret Key here
- Edit response handling, the code processes the API response, and if the status indicates the need to block the visitor, it takes action (e.g., returning a 403 Forbidden response).
// Process $response as needed
if($response['data']['status']['need_to_block']) {
// Do some action when visitor detected as "need_to_block"
// For example, return 403 Forbidden
http_response_code(403);
exit(); // Ensure script execution stops after sending the HTTP response
}
- Open your
index.php
orconfig.php
file, then add this code on the first line, then save.
<?php require_once('analytics.php'); ?>
- To configure the connection for various Content Management Systems (CMS), follow the specified paths relative to the root directory of the site:
- Drupal in
index.php
- Joomla in
index.php
- WordPress in
wp-config.php
- OpenCart in
index.php
- PunBB in
index.php
- FluxBB in
index.php
- MyBB in
inc/config.php
- To configure the connection for various PHP Framework, follow the specified paths relative to the root directory of the site:
- Laravel in
public/index.php
- CodeIgniter in
index.php
- CakePHP in
config/config.php
- Make sure
analytics.php
file are in the same directory of files that used to include theanalytics.php
file, if not you can specifically add a path whereanalytics.php
file located. - For information regarding visitor statistics, you can go to the Analytics page for details.