How to track short URLs generated from your site

To track short URLs generated from your site, you'll need to implement a tracking mechanism. Here's a basic example of PHP code to create and track short URLs, along with a simple database structure for storing tracking data. This example uses MySQL as the database.

  1. Set up your MySQL database:
CREATE TABLE short_urls (
    id INT AUTO_INCREMENT PRIMARY KEY,
    short_id VARCHAR(20) NOT NULL,
    long_url VARCHAR(255) NOT NULL,
    created_at TIMESTAMP NOT NULL,
    clicks INT DEFAULT 0
);

Create a PHP script for generating and tracking short URLs, let's call it generate_short_url.php:

<?php
// Database connection (Replace with your own database connection details)
$servername = "your_server";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";

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

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

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the long URL from the form submission
    $longURL = $_POST["long_url"];

    // Generate a unique identifier (you can use a library for this)
    $shortIdentifier = substr(md5(uniqid(rand(), true)), 0, 6);

    // Insert the mapping into the database
    $sql = "INSERT INTO short_urls (short_id, long_url, created_at) VALUES ('$shortIdentifier', '$longURL', NOW())";

    if ($conn->query($sql) === TRUE) {
        $shortURL = "https://www.yourdomain.com/$shortIdentifier";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}

// After you generate the short URL, set it as a link
$shortURL = "https://www.yourdomain.com/redirect.php?short_id=$shortIdentifier";

// Display the short URL as a link
echo "Short URL: <a href='$shortURL'>$shortURL</a>";

$conn->close();
?>

Create a PHP script for tracking clicks on short URLs, let's call it redirect.php:

<?php
// Database connection (Replace with your own database connection details)
$servername = "your_server";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";

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

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

// Get the short identifier from the URL
$shortIdentifier = $_GET["short_id"];

// Look up the corresponding long URL in the database
$sql = "SELECT long_url FROM short_urls WHERE short_id = '$shortIdentifier'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $longURL = $row["long_url"];
    
    // Increment the click count for this short URL
    $sql = "UPDATE short_urls SET clicks = clicks + 1 WHERE short_id = '$shortIdentifier'";
    $conn->query($sql);
    
    // Redirect to the long URL
    header("Location: $longURL");
    exit;
} else {
    echo "Short URL not found.";
}

$conn->close();
?>

This code will create short URLs and track the number of clicks for each short URL in the database. Replace the database connection details and URLs with your own. Additionally, you may want to add more features and improve error handling as needed for your specific use case.


To read the number of clicks associated with a specific short URL, you can create a PHP script that queries your database and retrieves the click count for the given short URL. Here's an example of how to do this:

  1. Create a PHP script, let's call it click_count.php:
<?php
// Database connection (Replace with your own database connection details)
$servername = "your_server";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";

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

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

if (isset($_GET["short_id"])) {
    $shortIdentifier = $_GET["short_id"];
    
    // Query the database to get the click count for the short URL
    $sql = "SELECT clicks FROM short_urls WHERE short_id = '$shortIdentifier'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $clickCount = $row["clicks"];
        echo "Click count for short URL '$shortIdentifier': $clickCount";
    } else {
        echo "Short URL not found.";
    }
} else {
    echo "Short URL identifier not provided.";
}

$conn->close();
?>

This script accepts a short_id parameter in the URL and queries the database to retrieve the click count for the corresponding short URL.

  1. To retrieve the click count for a specific short URL, you can call this script by providing the short_id as a parameter in the URL. For example:
https://www.yourdomain.com/click_count.php?short_id=abc123

Replace "your_server", "your_username", "your_password", and "your_database" with your actual database connection details.

Make sure the short_urls table in your database contains the click count data as shown in the previous examples, and that the short_id parameter is properly passed to the click_count.php script.

This script will return the click count for the specified short URL or indicate if the short URL is not found.

Related Articles

- All From ChatGPT
PLG_GSPEECH_SPEECH_BLOCK_TITLE