Need help with my redirecting rotator script

6 replies
I'm a newbie in PHP programming so I guess that's why my script isn't running the way I want it to.

Here's the code:
Code:
<?php
$bannerCounter= 1;
$bannerCode[$bannerCounter] = header("Location: xyz1.com" ) ;$bannerCounter++;
$bannerCode[$bannerCounter] = header("Location: xyz2.com" ) ;$bannerCounter++;
$bannerCode[$bannerCounter] = header("Location: http://xyz3.com" ) ;$bannerCounter++;
$bannerCode[$bannerCounter] = header("Location: http://xyz4.com" ) ;$bannerCounter++;
$bannerAdTotals = $bannerCounter - 1;
if ($bannerAdTotals > 1)
{
$bannerPicked = array_rand($bannercode, $bannercounter);
}
else
{
   $bannerPicked = 1;
}
$bannerAd = $bannerCode[$bannerPicked];
?>
The problem with the code is that the code always redirects to the last url in the list.
Would be great if some one helps me correct this.
#redirecting #rotator #script
  • Profile picture of the author Jahnold
    the second parameter in array_rand is the number of items you wish to randomly pick from the array...you always want 1. Also you needed to have a capitol 'C' in your first parameter

    <?php
    $bannerCounter= 1;
    $bannerCode[$bannerCounter] = "abc1" ;$bannerCounter++;
    $bannerCode[$bannerCounter] = "abc2" ;$bannerCounter++;
    $bannerCode[$bannerCounter] = "abc3" ;$bannerCounter++;
    $bannerCode[$bannerCounter] = "abc4" ;$bannerCounter++;
    $bannerAdTotals = $bannerCounter - 1;

    if ($bannerAdTotals > 1)
    {
    $bannerPicked = array_rand($bannerCode, 1);

    }
    else
    {
    $bannerPicked = 1;
    }
    $bannerAd = $bannerCode[$bannerPicked];

    ?>
    {{ DiscussionBoard.errors[3166690].message }}
  • Profile picture of the author SiddFisher
    I tried this. It still didn't work.
    Signature

    Computer Science student/coder who wants to learn more/out-of-the-box thinker/copywriter available for work during the summer. PM me for anything. :)

    {{ DiscussionBoard.errors[3167724].message }}
  • Profile picture of the author webpro4hire
    The code you've submitted has many errors in it, I'm surprised it even runs for you (gave me an error when I tried it). Also could be optimised a tad.

    I can help you understand it if you wish (pm me), or use the optimised code I've submitted below.

    Cheers!
    WP4H

    <?php
    // add as many locations as you want.
    $locations = array('xyz1.com','xyz2.com','xyz3.com','xyz4.com') ;

    // selects a 'random' number to serve a index for above locations
    $randomBannerIndex = rand(0, (count($locations) - 1));

    // assigns a random location
    $bannerAd = $locations[$randomBannerIndex];

    // send to selected random location.
    // NOTE add the h-t-t-p-:-/-/ after semi colon & space and before last quote (w/o dashes). I can't post links, post count is too low.
    header("Location: ".$bannerAd);
    ?>
    {{ DiscussionBoard.errors[3172279].message }}
  • Profile picture of the author SiddFisher
    Thanks man.....it worked.

    But I'm wondering why you didn't need to use an srand function to seed the rand function? When I do C programming, and rand function always generated the same random number if we didn't seed the rand with a system time function.
    Signature

    Computer Science student/coder who wants to learn more/out-of-the-box thinker/copywriter available for work during the summer. PM me for anything. :)

    {{ DiscussionBoard.errors[3172716].message }}
  • Profile picture of the author SteveJohnson
    From php.net:
    As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically.
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[3172846].message }}
  • Profile picture of the author webpro4hire
    Steve said it all!

    Thanks

    WP4H
    {{ DiscussionBoard.errors[3174241].message }}

Trending Topics