1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

how can you do this? php picks 1 link out of 1000 links

Discussion in 'PHP & Perl' started by rap8557, Mar 25, 2011.

  1. rap8557

    rap8557 Jr. VIP Jr. VIP Premium Member

    Joined:
    Sep 15, 2008
    Messages:
    1,081
    Likes Received:
    436
    How can i do this? with combination of DMR?

    i got this code but it will only picks a random of links in my link.txt
    i just need to pick 1 link from top to bottom and it will never picks the same links again.

    here the code

    Code:
    <?php
    // Open our links file
    $sites = array_map("trim", file("links.txt"));
    // Grab a random URL
    $redirect = $sites[array_rand($sites)];
    $referer = $_SERVER['HTTP_REFERER']; if($referer == "")
    // Redirect to our Random URL
    echo "<meta http-equiv=\"refresh\"content=\"0;url=http://".$redirect."\">"; 
    else
    {
    // Referrer is not blank send traffic to our homepage
    echo "<meta http-equiv=\"refresh\" content=\"0;url=http://www.google.com\">";  
    }
    ?>
    and the links for the links.txt
    Code:
    google.com
    yahoo.com
    aol.com
    msn.com
    
    thanks
     
  2. Autumn

    Autumn Elite Member

    Joined:
    Nov 18, 2010
    Messages:
    2,197
    Likes Received:
    3,041
    Occupation:
    I figure out ways to make money online and then au
    Location:
    Spamville
    Copy links.txt to links_og.txt before you start running the script. Pops a link from links.txt and uses it for the redirect; the remaining urls are saved back to links.txt. When you run out of links in links.txt, the full list of links is copied over from links_og.txt and you can begin again.

    PHP:
    <?php

    // Open our links file
    $lines file("links.txt");
    foreach(
    $lines as $line) {
        
    $line trim($line);
        if(!empty(
    $line)) {
            
    $sites[] = $line;
        }
    }

    // Load sites from fresh list if links.txt is empty
    if(empty($sites)) {
        
    $sites = array();
        
    copy("links_og.txt""links.txt");
        
    $sites array_map("trim"file("links.txt"));
    }


    // Grab a random redirect URL
    // shuffle($sites);
    $redirect array_shift($sites);

    // Save links.txt again
    if(!empty($sites)) {
        
    $save_this implode("\n"$sites);
        
    $fh fopen("links.txt"'w');
        
    fwrite($fh$save_this);
        
    fclose($fh);
    } else {
        
    copy("links_og.txt""links.txt");
    }


    // Redirect based on referrer
    $referer  $_SERVER['HTTP_REFERER']; 

    if(
    $referer == "") {
        
    // Redirect to our Random URL
        
    echo "<meta http-equiv=\"refresh\"content=\"0;url=http://".$redirect."\">";
    } else {
        
    // Referrer is not blank send traffic to our homepage
        
    echo "<meta http-equiv=\"refresh\" content=\"0;url=http://www.google.com\">";
    }


    ?>
     
    • Thanks Thanks x 2
    Last edited: Mar 25, 2011
  3. rap8557

    rap8557 Jr. VIP Jr. VIP Premium Member

    Joined:
    Sep 15, 2008
    Messages:
    1,081
    Likes Received:
    436
    the script is working fine but the code picks random links..
    it didn't pick the link from top to bottom

    your code is just the same as my code :)
     
    Last edited: Mar 25, 2011
  4. Autumn

    Autumn Elite Member

    Joined:
    Nov 18, 2010
    Messages:
    2,197
    Likes Received:
    3,041
    Occupation:
    I figure out ways to make money online and then au
    Location:
    Spamville
    No it's not. Did you even bother to read it and see what it's doing?

    Take out the shuffle(), I shouldn't have put it in there and have removed it from the code above. If you actually read the code you will see that even if you have the shuffle() in there, the order that the links are picked will be random but the frequency will not be - they will all be picked with exactly the same frequency because the links are extracted in a self refreshing cycle.

    Also look at array_pop() rather than array_shift() depending on which order you want the links to come from.
     
    Last edited: Mar 25, 2011
  5. JesusBack

    JesusBack Executive VIP Premium Member

    Joined:
    Sep 15, 2010
    Messages:
    1,159
    Likes Received:
    1,284
    Occupation:
    Almost done :D
    Location:
    {calm|cool|collected}
    you just need to array_pop() the the one picked
     
  6. Autumn

    Autumn Elite Member

    Joined:
    Nov 18, 2010
    Messages:
    2,197
    Likes Received:
    3,041
    Occupation:
    I figure out ways to make money online and then au
    Location:
    Spamville
    array_shift() takes the link from the start of the array, array_pop() will take it from the end of the array.
     
  7. rap8557

    rap8557 Jr. VIP Jr. VIP Premium Member

    Joined:
    Sep 15, 2008
    Messages:
    1,081
    Likes Received:
    436
    ohh i see the problem now :D i didn't blank the links.txt thats why its picking up random url instead of top to bottom.

    here the site
    heres the list of links in order
    thanks Autumn!
     
    Last edited: Mar 25, 2011