What is the best practice for tracking user clicks on text links in PHP?

Tracking user clicks on text links in PHP can be achieved by creating a script that records each click in a database. This involves updating a click counter in the database every time a user clicks on a link. By using PHP to handle this process, you can easily keep track of link clicks and analyze user behavior.

<?php

// Connect to your database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "your_database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Get the URL parameter from the clicked link
$url = $_GET['url'];

// Update the click counter in the database
$sql = "UPDATE links SET clicks = clicks + 1 WHERE url = '$url'";
$conn->query($sql);

// Redirect the user to the original link
header("Location: $url");

$conn->close();

?>