redirect script - why doesn't it work?

Discussion in 'PHP' started by mmelen, Aug 9, 2008.

  1. #1
    I found this on a forum and tested it out:

    <?
    $urls[]="http://www.url1.com/";
    $urls[]="http://www.url2.com/";
    //Add as many urls as you want this way
    $rnd=rand(0,count($urls)-1);
    $redirect=$url[$rnd];
    echo "<html><head><meta http-equiv='refresh' content='0;url=".$redirect."'></head><body></body></html>";
    ?>
    
    Code (markup):
    However, I get this: http://www.melen.net/randomtest.php
    The page keeps refreshing itself and goes nowhere.

    I just copied that same exact code.
    anyone have an idea to what I did wrong?
     
    mmelen, Aug 9, 2008 IP
  2. ryandanielt

    ryandanielt Well-Known Member

    Messages:
    1,797
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    185
    #2
    Here Try This. Much better written!

    Edit

    Sorry for the Double Post, New code posted below!
     
    ryandanielt, Aug 9, 2008 IP
  3. mmelen

    mmelen Well-Known Member

    Messages:
    1,526
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    105
    #3
    I don't want to use Location: though, I want to use meta-refresh because it hides referrals while Location: doesn't.
     
    mmelen, Aug 9, 2008 IP
  4. ryandanielt

    ryandanielt Well-Known Member

    Messages:
    1,797
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    185
    #4
    ok give me 1 minute and I will change that for you!

    Edit

    
    <?
    $url[0] = "http://test1.com/";
    $url[1] = "http://test2.com/";
    $url[2] = "http://test3.com/";
    $url[3] = "http://test4.com/";
    $url[4] = "http://test5.com/";
    
    srand ((double)microtime()*1000000);
    $randomnum = rand(0, count($url)-1);
    ?>
    <meta http-equiv="refresh" content="0;url=<?=$url[$randomnum];?>">
    
    PHP:
     
    ryandanielt, Aug 9, 2008 IP
  5. gda1984

    gda1984 Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    this might be a dumb comment, but when I do redirects I usually just do it in dreamweaver.....
     
    gda1984, Aug 9, 2008 IP
  6. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #6
    <?
    $urls[]="http://www.url1.com/";
    $urls[]="http://www.url2.com/";
    //Add as many urls as you want this way
    $rnd=rand(0,count($urls)-1);
    $redirect=$url[$rnd];
    echo "<html><head><meta http-equiv='refresh' content='0;url=".$redirect."'></head><body></body></html>";
    ?>
    PHP:


    $redirect=$url[$rnd]; //<--- error here

    it should be

    $redirect=$urls[$rnd];
     
    php-lover, Aug 10, 2008 IP
  7. mmelen

    mmelen Well-Known Member

    Messages:
    1,526
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    105
    #7
    wow, how stupid of me!

    thanks a lot for all your help guys!
     
    mmelen, Aug 10, 2008 IP
  8. mmelen

    mmelen Well-Known Member

    Messages:
    1,526
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    105
    #8
    one more quick question maybe someone can help me with - should the array count start with 0 or 1?
     
    mmelen, Aug 10, 2008 IP
  9. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #9
    example
    
    $a = array('r','s','t','u');
    
    $i = count($a);
    echo $i; //<--- output 4
    PHP:
    From that result, we know that count() is start from 1
     
    php-lover, Aug 10, 2008 IP