Anyone know how to program url rotator which picks urls one by one not random?
Temporaly i using starsol url rotator which is simple script with just few lines of codes and text database but with random pick.But how to do it sequential?
Here is the code:

$version = "v1.01";

$linksfile ="urlrotator.txt";

$urls = file("$linksfile");
$url = rand(0, sizeof($urls)-1);

$goto = "$urls[$url]";

header("Location: $goto");

?>


    Here's what I came up with. It assumes you have PHP5, if you have PHP4, replace the file_put_contents() function with a fopen(), fwrite(), and fclose() sequence.

    EDIT: a minor change to remove newlines '\n' from urls

    <?php
    $linksfile ="urlrotator.txt"; 
    
    $urls = file("$linksfile");
    $urls=str_replace('\n','',$urls); 
    $goto=array_shift($urls);
    array_push($urls,$goto);
    $strurls=implode('\n',$urls);
    
    file_put_contents($linksfile,$strurls);
    
    header("Location: $goto"); 
    
    ?>
    

      I have PHP 4.3.11.

      So code should look like this:

      <?php
      $linksfile ="urlrotator.txt"; 
      
      $urls = file("$linksfile");
      $urls=str_replace('\n','',$urls); 
      $goto=array_shift($urls);
      array_push($urls,$goto);
      $strurls=implode('\n',$urls);
      
      fopen()($linksfile,$strurls);
      
      fwrite()($linksfile,$strurls);
      
      fclose()($linksfile,$strurls);
      
      header("Location: $goto"); 
      
      ?>

        Not quite--your file functions aren't used correctly. You might want to check the manual to learn the proper syntax for these functions.

        $handle=fopen($linksfile,"w"); 
        
        fwrite($handle,$strurls); 
        
        fclose($handle); 
        
        

          So is this now 100% correct code?

          <?php
          $linksfile ="urlrotator.txt";
          
          $urls = file("$linksfile");
          $urls=str_replace('\n','',$urls);
          $goto=array_shift($urls);
          array_push($urls,$goto);
          $strurls=implode('\n',$urls);
          
          $handle=fopen($linksfile,"w"); 
          
          fwrite($handle,$strurls); 
          
          fclose($handle);
          
          header("Location: $goto");
          
          ?> 
          

            I believe so. I'm away from my testing server right now, so I can't test it directly. Try it and let me know how it works 🙂

              Belivet or not,its working.Last question:How its exactly function that all system?.How script know did i click rotator link before or not?Also on end is it he return to begin or it simply stop?

                How its exactly function that all system?

                Not exactly sure what you are asking here...

                The script is really simple. I'll walk you through it:
                1. Opens the list of links into an array, where each element is a single link.

                $linksfile ="urlrotator.txt";
                $urls = file("$linksfile"); 
                

                2. Removes the end line characters to prevent possible errors.

                $urls=str_replace('\n','',$urls); 
                

                3. Uses the array_shift function to remove the first link from the array. This is the one that you are redirected to in the last line.

                $goto=array_shift($urls);
                

                4. Puts this link onto the end of the array using array_push. Note that the first link is now at the bottom of the list, and the next sequential link is at the top of the list.

                array_push($urls,$goto);

                5. Makes the array into a string separated by new lines '\n'.

                $strurls=implode('\n',$urls);

                6. Replaces the old list of links with this modified one.

                $handle=fopen($linksfile,"w"); 
                fwrite($handle,$strurls); 
                fclose($handle); 

                7. Performs the redirection.

                header("Location: $goto"); 

                Since the link that was redirected to is moved to the bottom of the array, there is no "end"--the function continues to cycle through the list of links each time it is called.

                ?>

                  There's no good posts to reply to at the moment and I'm bored so I'm going to be pedantic, sorry. If your link file gets bigger you're going to have a lot of disk writes going on, this has the potential to get slow. Also, if your site starts to get a higher load you're going to have an increased chance of two people hitting the page at almost the same time and therefore you get the possibility that one page call will be reading from the links file as another is writing to it, or even two writing to it at the same time.

                  There are two issues here, a. performance hit of doing a large amount of disk writes and b. risk of getting your data messed up by two people writing to it at the same time. Two sollutions are needed. To avoid the excess disk writes you could have a seperate file with a number in, each time the page is hit this number is incremented untill it reaches the number of links and then goes back to 0. To avoid the possiblity of data cockup you can use php's file locking [man]flock[/man]. Here's the modded code.

                  <?php
                  $linksfile ="urlrotator.txt";
                  $posfile = "pos.txt";
                  
                  $links = file($linksfile);
                  $numlinks = count($linksfile);
                  
                  $fp = fopen($posfile, 'r+') or die("Failed to open posfile");
                  flock($fp, LOCK_EX);
                  $num = fread($fp, 1024);
                  if($num<$numlinks-1) {
                    fwrite($fp, $num+1);
                  } else {
                    fwrite($fp, 0);
                  }
                  flock($fp, LOCK_UN);
                  fclose($fp);
                  
                  header("Location: {$links[$num]}");
                  ?>
                  
                    Write a Reply...