Generate Unique URL

talkinggoat

Member
I am trying to make a field that will use php to generate a unique URL... but I have to search the list/table it's stored in, to make sure the URL does not exist, before the data is generated. I'd rather do it before, than use validation and tell the person that something went wrong. To accomplish this, I believe I'll need to use a mysql query to run through all the data, using a while loop. If the data exists, it simply repeats the unique URL generation script, below.

PHP:
function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}
return 'http://mywebsite.org/'.generateRandomString();

I'm not sure if a raw php query of the table is the correct way to do this or if Fabrik has a better way.
 
I can't help but feel you may be taking a wrong approach on something here, at least from the look of your example URL

Code:
return 'http://mywebsite.org/'.generateRandomString();

Can you explain what the random URL is for?

Usually that kind of "token" approach to URL's, which would typically be to ensure that someone has a "valid" / "authenticated" (whatever you want to call it) URL would use the random token as a part of a query string ... index.php?option=whatever&whatever=something&token=xxxxxxx ... and you would store just the token in your table, not a URL.

-- hugh
 
I just remembered our conversation a while ago about random ticket numbers, so I'm guessing this is something to do with that?

And just FYI, here's a thread where I gave someone code for doing pretty much exactly that, with the table lookup to make sure it hasn't been used.

http://fabrikar.com/forums/index.php?threads/how-to-create-a-random-number.48678/#post-254807

While not truly cryptographically random, the entropy in the method I gave there for generating the randomness is high enough for your purposes. But still worth the table lookup to make sure, especially in your situation where lives can be on the line.

-- hugh
 
Good afternoon, Hugh. I am using this to create a link, that will be attached to each user's profile. They can then share the link. We'll use this so that when they share their link, anyone they recruit will be accrued to their account. For example, if we're having a giveaway and the user has to get 3 other people to signup, to be entered for the drawing, I want to generate a link that would look like this:

http://mysite.com/index.php/form/33?email_list___referring_user=fk56i8u4

When the next person shows up, because they followed the link, this information is inserted into a hidden field and recorded, when they click save.
I haven't worked out the remainder of the details, but, somehow, we'll need to have a way of recording how many times the "referring_user" key shows up.

I tried using the sequence element, but it wasn't working quite like I wanted. If the link were used twice, the sequence element would not work and would throw PRIMARY key exists.

This is the php I have, so far. It is placed into a field element and generated at account creation. It seems to generate the random string and uses Joomla to check to make sure the string doesn't exist. I am not sure if I am doing it right, as this is the most complex php I've written. Please let me know if I've created a black hole or something.

PHP:
function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
        return $randomString;
}

Function checkRandomString() {
// Get a db connection.
    $randomString = generateRandomString();
    $db = JFactory::getDbo();

    // Create a new query object.
    $query = $db->getQuery(true);

    // Select all records from the user profile table where key begins with "custom.".
    // Order it by the ordering field.
    $query->select($db->quoteName('random_url_gen'));
    $query->from($db->quoteName('email_signup'));
    $query->where($db->quoteName('random_url_gen') . ' LIKE '. $db->quote($randomString));
    //$query->order('ordering ASC');

    // Reset the query using our newly populated query object.
    $db->setQuery($query);

    // Load the results as a list of stdClass objects (see later for more options on retrieving data).
    $results = $db->loadResult();

    $testPhrase = "matches";
    //return $results;
    if ($results == $randomString) {
      while ($results == $randomString) {
        generateRandomString();
        //return $testPhrase;
      }
    } else {
    return generateRandomString();
    }}
   
return checkRandomString();


@cheesegrits
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top