Results 1 to 5 of 5

Thread: code needed to auto forward randomly to 1 of 2 sites!

  1. #1
    Join Date
    Apr 2016
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question code needed to auto forward randomly to 1 of 2 sites!

    This is my current code:

    <?php

    { header("Location: /ts"); exit(); }

    ?>


    I'd like to change it, or make some new code, so it will randomly forward either to the above site, or another on the same domain (50% chance of either site). I have very little PHP experience. Thanks!!

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Look into array_rand() http://php.net/manual/en/function.array-rand.php

    There are code examples in the link above and here http://stackoverflow.com/a/8742221

    You will also find many other code examples via Google if you search something like "php random select from 2 strings"
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. #3
    Join Date
    Apr 2016
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Okay great! Here's what I have now:

    Code:
    <?php
    $a=array("/ts","/ts2");
    $random_keys=array_rand($a,2);
    
    if ($a='/ts') {
      header("Location: /ts"); exit();
    } elseif ($a='/ts2') {
      header("Location: /ts2"); exit();
    }
    ?>
    I ran it about 10 times and it always goes to "/ts". Can you offer some suggestions? Thanks!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Though you're dancing all around it, there's a lot wrong there. $a isn't changed by array_rand, so it's still an array, but then if ($a='/ts') sets it (to test it you need two equal signs == just one = sets it) to /ts in a "truthy" fashion (all setting of non false values resolves as true to the boolean parser), so header("Location: /ts"); exit(); will always happen.

    You can actually work this out with array_rand, but as I was looking at the man page, it was suggested there was a better method, mt_rand (said to be faster and closer to truly random). It works a little differently. In any case, this does the trick:

    PHP Code:
    <?php
    $a 
    = array("/ts""/ts2");
    header("Location: " $a[mt_rand(0count($a) - 1)]);
    die();
    ?>
    Now random is random though, and you only have two choices, so sometimes you will get the same result a few times or more in a row. Ten is unlikely, but could happen. Even 100, though that's very unlikely. In fact, after just 20 or so tries, you should be seeing it go to one about half the time, and the other the rest of the time. I tried both methods (array_rand and mt_rand), and the mt_rand method seemed more evenly distributed.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Apr 2016
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    Though you're dancing all around it, there's a lot wrong there. $a isn't changed by array_rand, so it's still an array, but then if ($a='/ts') sets it (to test it you need two equal signs == just one = sets it) to /ts in a "truthy" fashion (all setting of non false values resolves as true to the boolean parser), so header("Location: /ts"); exit(); will always happen.

    You can actually work this out with array_rand, but as I was looking at the man page, it was suggested there was a better method, mt_rand (said to be faster and closer to truly random). It works a little differently. In any case, this does the trick:

    PHP Code:
    <?php
    $a 
    = array("/ts""/ts2");
    header("Location: " $a[mt_rand(0count($a) - 1)]);
    die();
    ?>
    Now random is random though, and you only have two choices, so sometimes you will get the same result a few times or more in a row. Ten is unlikely, but could happen. Even 100, though that's very unlikely. In fact, after just 20 or so tries, you should be seeing it go to one about half the time, and the other the rest of the time. I tried both methods (array_rand and mt_rand), and the mt_rand method seemed more evenly distributed.
    I really need to learn more about PHP. I used to code in C++ back in the early to mid 90's when I ran a BBS and it seems similar.

    Anyway, thank you so much, it works great! I saw it switching between sub-folders/sites perfectly!

Similar Threads

  1. Code to auto refresh page every day?
    By mlegg in forum HTML
    Replies: 4
    Last Post: 11-29-2011, 03:43 AM
  2. Auto-deleting code
    By adfy in forum Looking for such a script or service
    Replies: 4
    Last Post: 04-02-2010, 08:57 PM
  3. Auto Code or Software?
    By vroni357 in forum Looking for such a script or service
    Replies: 3
    Last Post: 05-16-2009, 10:38 PM
  4. Ultimate Fade-In + Auto forward
    By BU2007 in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 04-11-2007, 04:10 PM
  5. [CODE/OTHER] Auto-Windows Code
    By IceMetalPunk in forum Submit a DHTML or CSS code
    Replies: 0
    Last Post: 03-13-2007, 01:08 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •