What are some best practices for implementing referral links in PHP applications to track user interactions externally?

When implementing referral links in PHP applications to track user interactions externally, it is important to securely pass and validate the referral information. One best practice is to use unique identifiers in the referral links that can be validated on the server side to track the referral source accurately.

// Example of generating a referral link with a unique identifier
$referralCode = uniqid();
$referralLink = "http://example.com/signup?ref=".$referralCode;

// Example of validating the referral code on the server side
if(isset($_GET['ref']) && !empty($_GET['ref'])){
    $referralCode = $_GET['ref'];
    // Validate the referral code against a database or other source
    // Track the referral source and user interaction accordingly
} else {
    // Handle cases where no referral code is present
}