1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Script that automatically redirects to a choice of links?

Discussion in 'General Scripting Chat' started by 420bud, Nov 11, 2011.

  1. 420bud

    420bud Newbie

    Joined:
    Jul 25, 2011
    Messages:
    1
    Likes Received:
    0
    Hi fellas, I just wanted to know if anyone knew how to make something that I could put my links in and upload it to my site, then it would automatically redirect them to a random link of my choice?

    Basically the visitor wouldn't even see the page, just the link they type hat just redirects to one of the random pages straight away.

    I'm dreadful at this type of thing so any help would be appreciated. :ranger:
     
  2. artizhay

    artizhay BANNED BANNED

    Joined:
    Nov 21, 2010
    Messages:
    1,867
    Likes Received:
    1,335
    Can set it up, for a price.
     
  3. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,155
    Save this as a php file.

    PHP:
    <?php

    $links
    [] = 'http://www.mysite1.com';
    $links[] = 'http://www.mysite2.com'
    $links[] = 'http://www.mysite3.com';

    $choice rand(0count($links) -1);
    header('Location: '.$links[$choice]);
    ?>

    I just typed it - didn't check it. If there 's a problem, let me know.
     
    • Thanks Thanks x 1
  4. gimme4free

    gimme4free Executive VIP Jr. VIP Premium Member

    Joined:
    Oct 22, 2008
    Messages:
    1,884
    Likes Received:
    1,932
    PHP:
    <?php
    $links 
    = array("http://link1.com""http://link2.com""http://link3.com");
    header("location:".$links[array_rand($links)]);
    ?>
     
  5. gimme4free

    gimme4free Executive VIP Jr. VIP Premium Member

    Joined:
    Oct 22, 2008
    Messages:
    1,884
    Likes Received:
    1,932
    Beat me to it :p

    I used to have issues using $links[] = ""; on some servers, it is 'best practice' I have been told to declare the array before using [] E.g. $links = array(); and then use $links[] = "";

    array_rand also shortens the code a lot more ;)
     
    • Thanks Thanks x 1
  6. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,155
    True! That will happen if the vhost is configured to display php notices. PHP will issue a notice if the array is not initialized before adding a member. And the notice will break the header redirect, as there must be absolutely no textual output before the header command.

    Thanks for pointing it out! :)

    ;)