Create link that automatically alternates between two destination URLs

The title describes a question I got lately. The idea is this: You want to run a campaign on social media and you have several partners on your project that sell the same product. You want to create just one link that evenly alternates between two (or even more) destination links. It has to do that evenly so that everyone gets the same chances.

My first idea was to set this up using link shortening tools like bit.ly, short.io or my favourite tools from Luxembourgish company Neon Internet called neontools. However I did not find such a feature. You can just define one destination link or create a neon page for example as landing page which then displays all the different links.

So the next idea was to do this with a script, JavaScript for example. I searched the internet if I was able to find some code or ideas on how to do this. Then I had the idea, simultaneously with Misch Strotz from Neon Internet, to use ChatGPT for this and let it write that script for me.

ChatGPT to help

I launched ChatGPT and started to write my prompt. The AI immediately understood what I wanted but gave me first a code with some errors and then as it explained it used a function called „localstorage“ to achieve what I asked. However „localStorage“ is only stored locally, in the user’s browser. That does not help, so I asked ChatGPT to rewrite the code using a different function so that it can store the information on the webserver itself. Otherwise it can not decide when do redirect to the first link or when to the second one.

Using PHP and its file_get_contents() and file_put_contents() function

The next code ChatGPT was generating for me used the file_get_contents() and file_put_contents() functions to read and write the count data to a file named "count.txt". If the file doesn’t exist, it sets the count to 0. It then increments the count and saves it back to the file.

The code then checks if the count is even or odd, and uses the header() function to redirect to the corresponding URL. The count data will be stored on the server, so it will be shared among all users who visit the page. (make sure the server has the appropriate permissions to read and write the count file).

The code it gave me however did not work and ChatGPT gave me some ideas how I could investigate further. Finally I asked it to consolidate all the ideas into one new code. And yes, that one then worked perfectly. For those who are looking for the solution, here is the whole code:

<?php
// Set error reporting level to display all errors and warnings
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Start output buffering to prevent any output before redirect
ob_start();

// Open count.txt file or create it if it doesn't exist
$count_file = 'count.txt';
if (!file_exists($count_file)) {
    $fp = fopen($count_file, 'w');
    fwrite($fp, '0');
    fclose($fp);
}
$count = (int) file_get_contents($count_file);

// Define URLs to redirect to
$url1 = 'https://www.link1-replace-with-your-link';
$url2 = 'https://www.link2-replace-with-your-link';

// Determine which URL to redirect to based on count value
if ($count % 2 == 0) {
    $redirect_url = $url1;
} else {
    $redirect_url = $url2;
}

// Increment count and write back to file
$count++;
file_put_contents($count_file, $count);

// Redirect to the appropriate URL
header("Location: $redirect_url");

// End output buffering and send output to browser
ob_end_flush();
exit();
?>

You have to replace the two destination URLs by your own, save as PHP-file and then create a textfile with nothing in it and just name it count.txt (no rich text format). Then copy these two files to your webserver, check out the link and use it everywhere you want to share or you can use a link shortener tool like Neontools to even track the linkclicks.

1 Kommentar

  1. Veröffentlich von Eric J. FRANCOIS am 22. März 2023 um 17:10

    Chatty Pete is indeed a great tool to create small scripts like that one. But it does sometimes do odd stuff. Like in this case it first enables the displaying of all errors and then starts a buffer because the redirect would fail if any error were to be output before the header is set. This is completely silly, as you will never be able to see any error message either way, because the redirect will happen before you can read the message.



Hinterlassen Sie einen Kommentar