0

Working on a link shortening script and I'm stumped. I figured the following code would function as needed however, I get an execution time out related to $str .= $charset[mt_rand(0, $count-1)];. I have scoured over the code several times, I can't find what I am doing wrong.

function randString($length, $charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') {
    $str = '';
    $count = strlen($charset);
    while ($length--) {
        $str .= $charset[mt_rand(0, $count-1)];
    }
    return $str; 
}

function shrinkURL($url) {
    $is_unique = false;
    $num = 4;
    $random_string = randString($num);

    $count = 0;
    while (!$is_unique) {
        $q1 = "SELECT id FROM linkShortner WHERE short = '".$random_string."' LIMIT 1";
    $result = mysql_query($q1) or die(mysql_error());

        if ($result === false)   // if you don't get a result, then you're good
            $is_unique = true;
        else                     // if you DO get a result, keep trying
            $count++;

    if ($count >= 10) {
        $num = (strlen($random_string) + 1);
    $random_string = randString($num);
    $count = 0;
        }
    $random_string = randString($num);
    }

    $shortURL = "https://domain.com/l/".$random_string;

    $q2 = "INSERT INTO linkShortner (id, destination, short, shorURL, creationDate) VALUES (NULL, '".$url."', '".$random_string."', '".$shortURL."', '".$DateTime."')";
    $r2 = mysql_query($q2) or die(mysql_error());

    return $shortURL; 

}

$shortURL = shrinkURL('http://domain.com');
echo $shortURL;

Any help would be greatly appreciated, think maybe I am just burnt out.

1
  • I got the answer but I cant post it [the answer as answer] due to my reputation being under 10. So here I is: Changed if ($result === false) to if (!mysql_num_rows($result)) and all is well. if ($result === false) is for mysqli which is not being used here. Basically the shrinkURL function scours the DB for a matching string before trying to insert a unique string/row, if ($result === false) would never = false because the value of $result is MySQL_query($q1) therefore producing an endless loop. Commented Mar 26, 2014 at 16:35

2 Answers 2

1

My guess is that at some point your function randString() will be called with the $length argument being 0.

This would make:

while ($length--) {
    $str .= $charset[mt_rand(0, $count-1)];
}

get stuck, because the first iteration would be while (-1), which is true. And then while (-2), which is also true.. etc etc etc.

I would change your while ( $length-- ) to while ( $length-- >= 0 )

Sign up to request clarification or add additional context in comments.

4 Comments

If I add the following under the randString function It works.
$num = 4; $random_string = randString($num); echo $random_string; exit();
You are calling the randString() twice in your code. Since the exit makes it work, I'm guessing the second call (in the if ($count >=10)... bit) is the one ending up in an endless loop.
I did try your suggestions out of frustration, though I do appreciate the thought they were not helpful. Changing while ( $length-- ) to while ( $length-- >= 0 ) resulted in a string length of 5, I would prefer to start with 4 and only increase the length if there are no more unique strings available quickly. I probably should have been more informative with my question. You were right about the randString calls being the culprit due to improper use of $result (see comment under my question). Thanks for the help.
0

Changed if ($result === false) to if (!mysql_num_rows($result)) and all is well. if ($result === false) is for mysqli which is not being used here.

Basically the shrinkURL function scours the DB for a matching string before trying to insert a unique string/row, if ($result === false) would never = false because the value of $result is MySQL_query($q1) therefore producing an endless loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.