{"id":2408,"date":"2023-03-14T15:01:05","date_gmt":"2023-03-14T14:01:05","guid":{"rendered":"https:\/\/www.sachaheck.net\/blog\/?p=2408"},"modified":"2023-03-14T15:03:39","modified_gmt":"2023-03-14T14:03:39","slug":"create-link-that-automatically-alternates-between-two-destination-urls","status":"publish","type":"post","link":"https:\/\/www.sachaheck.net\/blog\/skripte\/create-link-that-automatically-alternates-between-two-destination-urls","title":{"rendered":"Create link that automatically alternates between two destination URLs"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<p>My first idea was to set this up using link shortening tools like bit.ly, short.io or my favourite tools from  Luxembourgish company <a href=\"https:\/\/neontools.io\/\" target=\"_blank\" rel=\"noreferrer noopener\">Neon Internet called neontools<\/a>. 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.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<h2>ChatGPT to help<\/h2>\n\n\n\n<p>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 &#8222;localstorage&#8220; to achieve what I asked. However &#8222;localStorage&#8220; is only stored locally, in the user&#8217;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.<\/p>\n\n\n\n<h2>Using PHP and its file_get_contents() and file_put_contents() function<\/h2>\n\n\n\n<p>The next code ChatGPT was generating for me used the <code>file_get_contents()<\/code> and <code>file_put_contents()<\/code> functions to read and write the count data to a file named <code>\"count.txt\"<\/code>. If the file doesn&#8217;t exist, it sets the count to <code>0<\/code>. It then increments the count and saves it back to the file.<\/p>\n\n\n\n<p>The code then checks if the count is even or odd, and uses the <code>header()<\/code> 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).<\/p>\n\n\n\n<p>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:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ Set error reporting level to display all errors and warnings\nerror_reporting(E_ALL);\nini_set('display_errors', 1);\n\n\/\/ Start output buffering to prevent any output before redirect\nob_start();\n\n\/\/ Open count.txt file or create it if it doesn't exist\n$count_file = 'count.txt';\nif (!file_exists($count_file)) {\n    $fp = fopen($count_file, 'w');\n    fwrite($fp, '0');\n    fclose($fp);\n}\n$count = (int) file_get_contents($count_file);\n\n\/\/ Define URLs to redirect to\n$url1 = 'https:\/\/www.link1-replace-with-your-link';\n$url2 = 'https:\/\/www.link2-replace-with-your-link';\n\n\/\/ Determine which URL to redirect to based on count value\nif ($count % 2 == 0) {\n    $redirect_url = $url1;\n} else {\n    $redirect_url = $url2;\n}\n\n\/\/ Increment count and write back to file\n$count++;\nfile_put_contents($count_file, $count);\n\n\/\/ Redirect to the appropriate URL\nheader(\"Location: $redirect_url\");\n\n\/\/ End output buffering and send output to browser\nob_end_flush();\nexit();\n?><\/code><\/pre>\n\n\n\n<p>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.<\/p>\n<div class=\"shariff\"><ul class=\"shariff-buttons theme-default orientation-horizontal buttonsize-medium\"><li class=\"shariff-button twitter shariff-nocustomcolor\" style=\"background-color:#1e3050\"><a href=\"https:\/\/twitter.com\/share?url=https%3A%2F%2Fwww.sachaheck.net%2Fblog%2Fskripte%2Fcreate-link-that-automatically-alternates-between-two-destination-urls&text=Create%20link%20that%20automatically%20alternates%20between%20two%20destination%20URLs\" title=\"Bei X (Twitter) teilen\" aria-label=\"Bei X (Twitter) teilen\" role=\"button\" rel=\"noreferrernoopener nofollow\" class=\"shariff-link\" style=\"; background-color:#000000; color:#fff\" target=\"_blank\"><span class=\"shariff-icon\" style=\"\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" height=\"1em\" viewBox=\"0 0 512 512\"><!--! Font Awesome Free 6.4.2 by @fontawesome - https:\/\/fontawesome.com License - https:\/\/fontawesome.com\/license (Commercial License) Copyright 2023 Fonticons, Inc. --><style>svg{fill:#ffffff}<\/style><path d=\"M389.2 48h70.6L305.6 224.2 487 464H345L233.7 318.6 106.5 464H35.8L200.7 275.5 26.8 48H172.4L272.9 180.9 389.2 48zM364.4 421.8h39.1L151.1 88h-42L364.4 421.8z\"\/><\/svg><\/span><span class=\"shariff-text\">twittern<\/span>&nbsp;<\/a><\/li><li class=\"shariff-button facebook shariff-nocustomcolor\" style=\"background-color:#4273c8\"><a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwww.sachaheck.net%2Fblog%2Fskripte%2Fcreate-link-that-automatically-alternates-between-two-destination-urls\" title=\"Bei Facebook teilen\" aria-label=\"Bei Facebook teilen\" role=\"button\" rel=\"noreferrernoopener nofollow\" class=\"shariff-link\" style=\"; background-color:#3b5998; color:#fff\" target=\"_blank\"><span class=\"shariff-icon\" style=\"\"><svg width=\"32px\" height=\"20px\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 18 32\"><path fill=\"#3b5998\" d=\"M17.1 0.2v4.7h-2.8q-1.5 0-2.1 0.6t-0.5 1.9v3.4h5.2l-0.7 5.3h-4.5v13.6h-5.5v-13.6h-4.5v-5.3h4.5v-3.9q0-3.3 1.9-5.2t5-1.8q2.6 0 4.1 0.2z\"\/><\/svg><\/span><span class=\"shariff-text\">teilen<\/span>&nbsp;<\/a><\/li><li class=\"shariff-button linkedin shariff-nocustomcolor\" style=\"background-color:#1488bf\"><a href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=https%3A%2F%2Fwww.sachaheck.net%2Fblog%2Fskripte%2Fcreate-link-that-automatically-alternates-between-two-destination-urls\" title=\"Bei LinkedIn teilen\" aria-label=\"Bei LinkedIn teilen\" role=\"button\" rel=\"noreferrernoopener nofollow\" class=\"shariff-link\" style=\"; background-color:#0077b5; color:#fff\" target=\"_blank\"><span class=\"shariff-icon\" style=\"\"><svg width=\"32px\" height=\"20px\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 27 32\"><path fill=\"#0077b5\" d=\"M6.2 11.2v17.7h-5.9v-17.7h5.9zM6.6 5.7q0 1.3-0.9 2.2t-2.4 0.9h0q-1.5 0-2.4-0.9t-0.9-2.2 0.9-2.2 2.4-0.9 2.4 0.9 0.9 2.2zM27.4 18.7v10.1h-5.9v-9.5q0-1.9-0.7-2.9t-2.3-1.1q-1.1 0-1.9 0.6t-1.2 1.5q-0.2 0.5-0.2 1.4v9.9h-5.9q0-7.1 0-11.6t0-5.3l0-0.9h5.9v2.6h0q0.4-0.6 0.7-1t1-0.9 1.6-0.8 2-0.3q3 0 4.9 2t1.9 6z\"\/><\/svg><\/span><span class=\"shariff-text\">mitteilen<\/span>&nbsp;<\/a><\/li><\/ul><\/div>","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>\n","protected":false},"author":1,"featured_media":2409,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[583,11],"tags":[],"_links":{"self":[{"href":"https:\/\/www.sachaheck.net\/blog\/wp-json\/wp\/v2\/posts\/2408"}],"collection":[{"href":"https:\/\/www.sachaheck.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sachaheck.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sachaheck.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sachaheck.net\/blog\/wp-json\/wp\/v2\/comments?post=2408"}],"version-history":[{"count":1,"href":"https:\/\/www.sachaheck.net\/blog\/wp-json\/wp\/v2\/posts\/2408\/revisions"}],"predecessor-version":[{"id":2410,"href":"https:\/\/www.sachaheck.net\/blog\/wp-json\/wp\/v2\/posts\/2408\/revisions\/2410"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sachaheck.net\/blog\/wp-json\/wp\/v2\/media\/2409"}],"wp:attachment":[{"href":"https:\/\/www.sachaheck.net\/blog\/wp-json\/wp\/v2\/media?parent=2408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sachaheck.net\/blog\/wp-json\/wp\/v2\/categories?post=2408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sachaheck.net\/blog\/wp-json\/wp\/v2\/tags?post=2408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}