Exemple #1
0
function random_str($length, $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#%$')
{
    $str = '';
    while (!good_pass($str)) {
        $str = gen_str($length, $keyspace);
    }
    return $str;
}
Exemple #2
0
    if (is_url($_POST['url'])) {
        // Encode the URL (For safe MySQL input)
        $url = urlencode($_POST['url']);
        $result = mysql_query("SELECT * FROM rshort WHERE url = '" . $url . "'", $mcon);
        // Check if there's a result. Again, this should be only one (if any).
        if (mysql_num_rows($result) == 1) {
            $data = mysql_fetch_assoc($result);
            mysql_close($mcon);
            echo $sitename . $data['hash'];
            exit;
        }
        // No result, generate the hash
        // Check if the hash already exist in the database. If it does, generate another one.
        $existing_hash = true;
        while ($existing_hash) {
            $hash = gen_str(5);
            $result = mysql_query("SELECT * FROM rshort WHERE hash = '" . $hash . "'", $mcon);
            if (mysql_num_rows($result) == 0) {
                $existing_hash = false;
            }
        }
        // All is fine, let's insert the necessary values
        mysql_query("INSERT INTO rshort (url, hash) VALUES('" . $url . "', '" . $hash . "') ", $mcon) or die(mysql_error());
        // Then echo the hash.
        echo $sitename . $hash;
    } else {
        echo "Invalid URL";
    }
}
mysql_close($mcon);
// Function to validate URL (I know abouth the filter_var but it works in PHP 5 >= 5.2 only.)