Simple PHP Help

Status
Not open for further replies.

stussy5555

HNIC
Dec 12, 2007
821
10
0
I am currently using this code to rotate between 2 offers:

<?php
if(rand(0,1) == 0) {
header("Location: http://www.Offer1.html");
} else {
header("Location: http://www.Offer2.html");
}
?>

How do I rotate between say 5 offers or 10 offers? What would the code look like?

I know its easy, but I know jack shit about programming.

Thanks guys
 


PHP:
$rand = rand(0,9) ;

if ($rand  == 0) {
    header("Location: http://www.Offer1.html");
   } elseif ($rand == 1) {
    header("Location: http://www.Offer2.html");
   } elseif ($rand == 2) {
    header("Location: http://www.Offer3.html");
   } elseif ($rand == 3) {
    header("Location: http://www.Offer4.html");
   } elseif ($rand == 4) {
    header("Location: http://www.Offer5.html");
   } elseif ($rand == 5) {
    header("Location: http://www.Offer6.html");
   } elseif ($rand == 6) {
    header("Location: http://www.Offer7.html");
   } elseif ($rand == 7) {
    header("Location: http://www.Offer8.html");
   } elseif ($rand == 8) {
    header("Location: http://www.Offer9.html");
   } else {
    header("Location: http://www.Offer10.html");
 }
... but danbo's post below is probably better. I simply answered your question, his use of arrays will prove easier when you start changing the destination url
 
<?php

// Put a ' or a " around each url and a , after each one except
// the last one
$urls = array(

'http://www.yahoo.com',

'http://www.wickedfire.com',

'http://www.google.com/search?num=100&q=free%20porn'

);

$random = $urls[rand(0, count($urls)-1)];

header("Location: $random");

exit;

?>



------- to cloak, dont use 'header'
<SCRIPT LANGUAGE="JavaScript">
<!-- Script courtesy of Web Site Design and Development - Your Guide to Professional Web Site Design and Development
window.location="http://www.yourdomain.com/";
// -->
</script>
 
Could use rand_array() too instead of counting the elements.

PHP:
$random = $urls[rand_array($urls)];
 
Or just shuffle the array and grab the first element.

$links = array('link1', 'link1', 'link3');
shuffle($links);
redirect to $links[0]
 
why not put your offers in a text file, have php script open the file, count the lines, pull a random number and grab that line.
 
I try to stay away from IF statements in PHP and use the CASE SWITCH statement:

PHP:
$rand = rand(0,2);

switch($rand)
{
case 0: header("Location: http://www.Offer1.html"); break;
case 1: header("Location: http://www.Offer2.html"); break;
case 2: header("Location: http://www.Offer3.html"); break
}
Easier to add to later on down the line.

FB.
 
Status
Not open for further replies.