Alley Hector
Alley Hector

Reputation: 41

Can I use the Codeigniter redirect with probability?

We use a CMS, Fuel, built on Codeigniter, and use the redirect function often. I was wondering if there is a way to use probability with this function so that half (or whatever amount we set) the time it redirects to one page and half the time to another.

In general we will create a page with a redirect that looks like this:

<?=redirect('https://subdomian.onanotherserver.com/asdf')?> 

We would love to make it looks similar:

<?=probabilityRedirect(0.5, 'https://subdomian.onanotherserver.com/asdf', 'https://subdomian.onanotherserver.com/jkl')?> 

What would be the best way to go about this? Can we build off the redirect or just write a new function?

Upvotes: 3

Views: 109

Answers (2)

DFriend
DFriend

Reputation: 8964

I would make a new function. Probably adding this to url_helper.php makes it the easiest to implement. Could be a stand-alone helper too but then you have to be certain url_helper is loaded.

if(!function_exists('randomRedirect'))
{
  /**
   * Randomized Header Redirect
   *
   * @param int $seed value optional
   * @param array $list an indexed array of URL strings
   * @return void
   */
  function randomRedirect($seed = NULL, $list)
  {
    //list needs to be an array
    if(!is_array($list OR ! isset($list)))
    {
      throw new Exception('randomRedirect() requires Array in second parameter');
    }
    if(!empty($seed))
    {
      mt_srand($seed);
    }
    $choice_count = count($list);
    redirect($list[mt_rand(0, $choice_count)]);
  }
}

Note: I did not test this! Results not guaranteed. :)

Revised code below. Had some time to experiment with the above which eventually resulted in this.

randomURL_helper.php

<?php

if(!function_exists('p_redirect'))
{
  /**
   * Randomized Header Redirect
   *
   * @param array $list an indexed array of URL strings
   * @param bool $seed optional when true, the random number generator will be seeded
   * 
   * @return void
   * 
   * Takes a list of URL strings in an array and randomly selects one as the
   * input to the CodeIgniter function redirect. 

   * If you specify the full site URL that link will be built, 
   * but for local links simply providing the URI segments to the 
   * controller you want to direct to will create the link. 
   * 
   * The function will build the URL based on your config file values.
   * 
   * This function requires the CodeIgniter helper "URL Helper" 
   * to be loaded using the following code: $this->load->helper('url');
   * 
   * Use this line of code $this->load->helper('randomURL'); 
   * to make p_redirect available to your CodeIgniter application.
   * 
   */
  function p_redirect($list, $seed = FALSE)
  {
    if(!is_array($list))
    {
      // $list must be an array
      throw new Exception('randomRedirect() requires Array as first parameter');
    }
    if($seed)
    {
      list($usec, $sec) = explode(' ', microtime());
      $seed_val = (float) $sec + ((float) $usec * 100000);
      mt_srand($seed_val);
    }

    redirect($list[mt_rand(0, count($list) - 1)]);
  }

}

Tested it enough to see that it's not completely off-base. Seems to get the job done. Enjoy!

Upvotes: 2

Won Jun Bae
Won Jun Bae

Reputation: 5389

You can just include it inside a rand 50/50 condition,

 <?php
 if (mt_rand(0,1)) {
    redirect('https://subdomian.onanotherserver.com/asdf')
 }
 ?>

Upvotes: 1

Related Questions