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

php script misfiring

Discussion in 'PHP & Perl' started by Website, Jun 5, 2010.

  1. Website

    Website Supreme Member

    Joined:
    Feb 8, 2008
    Messages:
    1,291
    Likes Received:
    286
    Location:
    whitehouse
    I have a script that is supposed to display a dandom url and a random password


    but sometimes only the password will display and not the url
     
  2. kaidoristm

    kaidoristm Power Member

    Joined:
    Feb 13, 2009
    Messages:
    561
    Likes Received:
    726
    Occupation:
    Freelancer
    Location:
    Estonia
    Home Page:
    Try this (hope your urls are one per line in file)

    PHP:
    // Lets create random password

    function rand_pass()
    {
        
    $length 7;
        
    $chars "abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ23456789";    
        
    $size strlen($chars);

        for(
    $i 0$i $length$i++)
        {
            
    $randpass .= $chars[rand(0$size 1)];
        }
        return 
    $randpass;
    }


    // Grab random url from file

    function get_url()
    {
        
    $links file_get_contents('urls.txt');
        
    $links explode("\r\n",$links);
        
    $link $links[rand(0,array_pop(array_keys($links)))];
        return 
    $link;
    }

        
    $password rand_pass();
        
    $url get_url();


     
    Last edited: Jun 5, 2010
  3. Website

    Website Supreme Member

    Joined:
    Feb 8, 2008
    Messages:
    1,291
    Likes Received:
    286
    Location:
    whitehouse
    it tired that script and it displays all the urls at once instead of just one
     
  4. kaidoristm

    kaidoristm Power Member

    Joined:
    Feb 13, 2009
    Messages:
    561
    Likes Received:
    726
    Occupation:
    Freelancer
    Location:
    Estonia
    Home Page:
    Displays one for me just tested. How are your urls in text file. Make it one per line and should work

    PHP:
    <?php

    // Lets create random password

    function rand_pass()
    {
        
    $length 7;
        
    $chars "abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ23456789";    
        
    $size strlen($chars);

        for(
    $i 0$i $length$i++)
        {
            
    $randpass .= $chars[rand(0$size 1)];
        }
        return 
    $randpass;
    }


    // Grab random url from file

    function get_url()
    {
        
    $links file_get_contents('websites.txt');
        
    $links explode("\r\n",$links);
        
    $link $links[rand(0,array_pop(array_keys($links)))];
        return 
    $link;
    }

        
    $password rand_pass();
        
    $url get_url();  
        
        echo 
    $password."<br />";
        echo 
    $url;
    ?>
     
    Last edited: Jun 6, 2010
  5. Website

    Website Supreme Member

    Joined:
    Feb 8, 2008
    Messages:
    1,291
    Likes Received:
    286
    Location:
    whitehouse
    thank you
     
  6. kaidoristm

    kaidoristm Power Member

    Joined:
    Feb 13, 2009
    Messages:
    561
    Likes Received:
    726
    Occupation:
    Freelancer
    Location:
    Estonia
    Home Page:
    Not problem at all :)
     
  7. Bots_Do_it_Faster

    Bots_Do_it_Faster Registered Member

    Joined:
    Jun 28, 2010
    Messages:
    64
    Likes Received:
    77
    Occupation:
    Web Developer php and c#
    Location:
    South Florida
    I know someone else posted new code, but I thought I'd point out the logic error in the original code. In the following section:

    $urls = @file('files.txt');
    $num = count($urls);
    $url = $urls[rand(0,$num)];


    $urls is set to an array
    $num is set to integer being the total number of items
    When you do $urls[rand(0,$num)] what you are doing is saying give me a random number between 0 and $num(The total number of items). Most of the time this will work, but the problem occurs when you randomly pick the last item.
    Lets say the array has 10 items and you randomly pick item 10. $urls[10] will be null because the array will start with $urls[0] and end with $urls[9]. That is why you were getting random blank urls. The easy fix could have been:

    $url = $urls[(rand(1,$num)-1)];

    This will take a random number between 1 and the total items then subtract 1 from that to compensate for the array pointer;