Web Analytics Made Easy -
StatCounter Looking for a spinner that can spin between 10 different links - CodingForum

Announcement

Collapse
No announcement yet.

Looking for a spinner that can spin between 10 different links

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Looking for a spinner that can spin between 10 different links

    I'm looking for a spinner that...

    When visitors come to my website, I want them to be randomly redirected from a choice of 10 different links.

    For example.

    Visitor #1 goes to http://mysite.com/page.php and lands on http://google.com
    Visitor #2 goes to http://mysite.com/page.php and lands on http://twitter.com
    etc.... etc...

    Thanks.

  • #2
    create an array in php of all of the links (you said 10, so thats what we will use). Lets call the array links, so links[0] = http://www.google.com, links [1] = http://www.facebook.com, etc...

    Then we randomly select a number between 0 and 9 so use
    PHP Code:
    $randomNumber rand(0,9); 
    then we print out this line:

    PHP Code:
    print '<script type="text/javascript">
    <!--
    window.location = "'
    .links[$randomNumber].'"
    //-->
    </script>'

    I believe that should work...

    Comment


    • #3
      Here's another way using only php:

      PHP Code:
      <?php
      $urls 
      = array(
      "http://url_1.html",
      "http://url_2.html",
      "http://url_3.html",
      "http://url_4.html",
      "http://url_5.html",
      "http://url_6.html",
      "http://url_7.html",
      "http://url_8.html",
      "http://url_9.html",
      "http://url_10.html"
      );

      shuffle($urls);
      $url array_shift($urls);
      header("location:$url");
      exit;
      ?>

      Comment


      • #4
        Originally posted by Jinxy View Post
        Here's another way using only php:

        PHP Code:
        <?php
        $urls 
        = array(
        "http://url_1.html",
        "http://url_2.html",
        "http://url_3.html",
        "http://url_4.html",
        "http://url_5.html",
        "http://url_6.html",
        "http://url_7.html",
        "http://url_8.html",
        "http://url_9.html",
        "http://url_10.html"
        );

        shuffle($urls);
        $url array_shift($urls);
        header("location:$url");
        exit;
        ?>
        If you don't mind me saying, that's a bit of a roundabout way of doing it . You could just use an random index like so:
        PHP Code:
        $urls = array(
        "http://url_1.html",
        "http://url_2.html",
        "http://url_3.html",
        "http://url_4.html",
        "http://url_5.html",
        "http://url_6.html",
        "http://url_7.html",
        "http://url_8.html",
        "http://url_9.html",
        "http://url_10.html"
        );
        $url $urls[rand(0,(count($urls)-1))];
        header("location: $url"); 
        Avoids the stack function, and the shuffle function and just returns a result based on a random number.
        Useful function to retrieve difference in times
        The best PHP resource
        A good PHP FAQ
        PLEASE remember to wrap your code in [PHP] tags.
        PHP Code:
        // Replace this
        if(isset($_POST['submitButton']))
        // With this
        if(!empty($_POST))
        // Then check for values/forms. Some IE versions don't send the submit button 
        Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

        Comment


        • #5
          Originally posted by BluePanther View Post
          If you don't mind me saying, that's a bit of a roundabout way of doing it .
          I don't mind. Especially if it's a better way!

          Comment

          Working...
          X