session_start(); if(!isset($_SESSION['user_count'])){ $_SESSION['user_count'] = 0; } $_SESSION['user_count']++; echo "Total users: ".$_SESSION['user_count'];
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password"); $stmt = $pdo->prepare("SELECT COUNT(*) FROM user_visits"); $stmt->execute(); $user_count = $stmt->fetchColumn(); echo "Total users: ".$user_count;
require_once 'vendor/autoload.php'; $client = new Google_Client(); $client->setApplicationName("My App"); $client->setAuthConfig("client_secrets.json"); $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']); $service = new Google_Service_AnalyticsReporting($client); $request = new Google_Service_AnalyticsReporting_ReportRequest(); $request->setViewId('123456789'); // set other query parameters $response = $service->reports->batchGet($request); $user_count = $response->getReports()[0]->getData()->getTotals()[0]->getValues()[0]; echo "Total users: ".$user_count;In this example, we use the Google Analytics API to count the number of users who have visited the website. We need to install the Google API client library and configure it with the Google Analytics credentials to use this approach. This approach is the most advanced as it can provide detailed information about users such as demographics and behavior.