hostsum Posted July 19, 2013 Share Posted July 19, 2013 I have this PHP link rotating script, it works pretty fine...when I run the script it redirect to:link1.comlink2.comlink3.com.... and so onbut when I change the web browser or someone else runs it, it restart from the begging.. I mean that it redirect back to link1.com the script use a link.txt file where the link list is storedwhat I want is that when I run the script it redirect to link1.comwhen I change the web browser or someone else runs it continue redirecting to next link and so on...(to link2.com) Here's the PHP code: <?php function redirect_to($link) { header("Location: {$link}"); exit; } $links = file('links.txt'); while (!$links) { $links = file('links.txt'); } $links_count = count($links); $one_month = 60 * 60 * 24 * 30; $index = $_COOKIE['link_index'] + 1; setcookie('link_index', $index, time() + $one_month); if ($index == $links_count) { setcookie('link_index', 0, time() + $one_month); $index = 0; } redirect_to($links[$index]); ?> Please help me solve this issue Thanks Quote Link to comment https://forums.phpfreaks.com/topic/280319-php-link-rotating-script/ Share on other sites More sharing options...
Muddy_Funster Posted July 19, 2013 Share Posted July 19, 2013 I don't understand what the problem is - mainly because you havn't explained what the script should be doing. Quote Link to comment https://forums.phpfreaks.com/topic/280319-php-link-rotating-script/#findComment-1441394 Share on other sites More sharing options...
hostsum Posted July 19, 2013 Author Share Posted July 19, 2013 Hey, Thanks for your response. The script should rotate links.. Quote Link to comment https://forums.phpfreaks.com/topic/280319-php-link-rotating-script/#findComment-1441397 Share on other sites More sharing options...
Muddy_Funster Posted July 19, 2013 Share Posted July 19, 2013 yeah I got that bit, but we kinda need to know what the desired condition of the rotation should be Vs what it actualy is? Quote Link to comment https://forums.phpfreaks.com/topic/280319-php-link-rotating-script/#findComment-1441398 Share on other sites More sharing options...
AbraCadaver Posted July 19, 2013 Share Posted July 19, 2013 Just for fun. links.txt needs to be writable by the webserver: $file = 'links.txt'; $links = file($file); if(filemtime($file) <= strtotime('-1 month')) { array_push($links, array_shift($links)); file_put_contents($file, $links, LOCK_EX); } header('Location: ' . trim($links[0])); exit; Quote Link to comment https://forums.phpfreaks.com/topic/280319-php-link-rotating-script/#findComment-1441401 Share on other sites More sharing options...
AbraCadaver Posted July 19, 2013 Share Posted July 19, 2013 My bad. Should be: file_put_contents($file, implode('', $links), LOCK_EX) Quote Link to comment https://forums.phpfreaks.com/topic/280319-php-link-rotating-script/#findComment-1441419 Share on other sites More sharing options...
rythemton Posted July 19, 2013 Share Posted July 19, 2013 The problem is that you are using a COOKIE. Cookies are unique per computer and per browser, so when someone new gets on, or a new browser is used, their system has no 'link_index' cookie, and the count starts over. You need to save the 'link_index' on the server in some way that is not computer or browser specific. A Session won't work either, because it is also unique per computer and per browser, just stored on the server instead of the client machine. Quote Link to comment https://forums.phpfreaks.com/topic/280319-php-link-rotating-script/#findComment-1441423 Share on other sites More sharing options...
rythemton Posted July 19, 2013 Share Posted July 19, 2013 Just for fun. links.txt needs to be writable by the webserver: $file = 'links.txt'; $links = file($file); if(filemtime($file) <= strtotime('-1 month')) { array_push($links, array_shift($links)); file_put_contents($file, implode('', $links), LOCK_EX); } header('Location: ' . trim($links[0])); exit; This solution should work for what you want, because it rotates the links in the file itself, rather than keeping a counter. Quote Link to comment https://forums.phpfreaks.com/topic/280319-php-link-rotating-script/#findComment-1441424 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.