php script misfiring

Website

Supreme Member
Joined
Feb 8, 2008
Messages
1,260
Reaction score
287
I have a script that is supposed to display a dandom url and a random password

<?php



/**

* The letter l (lowercase L) and the number 1

* have been removed, as they can be mistaken

* for each other.

*/



function createRandomPassword() {



$chars = "abcdefghijkmnopqrstuvwxyz023456789";

srand((double)microtime()*1000000);

$i = 0;

$pass = '' ;



while ($i <= 7) {

$num = rand() % 33;

$tmp = substr($chars, $num, 1);

$pass = $pass . $tmp;

$i++;

}



return $pass;



}



// Usage

$password = createRandomPassword();




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



?>


<?php

echo "$password";

?>

<?php

echo "$url";

?>


but sometimes only the password will display and not the url
 
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:
it tired that script and it displays all the urls at once instead of just one
 
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:
thank you
 
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;
 
Back
Top