Trevor
Trevor

Reputation: 2457

Sending Random numbers to a database as it's ID

I'm trying to send a random number to the database for a user/article ID. It is currently using auto increment as a counting system. However, I'd like for the number to be random and unpredictable.

The mt_rand() function in PHP does exactly what I need. Although, my question is what happens when the function returns a number already in use. Of course I can just use a is_null() to check. But if it keeps on picking a number in use I could imagine that that'd slow the operation down.

Any thoughts on what I might be able to do to get around this? Perhaps I'm going at this all wrong.

Also if there's a function that gives letters and numbers that would also help greatly (like Youtube's).

Thanks for reading!

Upvotes: 1

Views: 93

Answers (5)

Ronald Swets
Ronald Swets

Reputation: 1667

What you actually want is some "random" key to use as an identifier for the article. I would keep the auto_increment and eigther:

  1. add an column with a "hashkey" or "random key" to identify the article. This poses the "i already have this key" issue (which should not be that large unless you have billions of articles). See some code examples already posted.
  2. create an extra table with pregenerated keys (i.e. 10000 id -> key values) where you can lookup the id by key. If the table runs out you can easily generate new values. This way you don't have to worry about getting "slow" generation speed.

Upvotes: 0

The Humble Rat
The Humble Rat

Reputation: 4696

Here is a simple function to create a 10 character long string. The string is built using upper/lowercase text and numbers. Auto increment is definitely the way to go, however, if you are dead set, the function below should help.

<?php

function randomID()
{
    $ID = substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',5)),0,10);
    echo $ID;
}

randomID();

?>

To make the string longer, change 10 to whatever you like. In terms of ensuring it does not already exist. I would suggest you generate the new ID and then do a search in the database to ensure it does not exist before inserting. Granted this is an extra step in the chain, but unfortunately this is what needs to be done.

Hope this helps

Upvotes: 1

CS GO
CS GO

Reputation: 912

Always random (just encrypting ms) :

<?php

$value = time();
$key = "543yretghf436436";
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $value, MCRYPT_MODE_ECB);
      //if you want even long string change 128 to 256
$encrypted = base64_encode($encrypted);
$encrypted = rtrim($encrypted, '=');

echo $encrypted;

?>

e.g.

Egttu2XhRGdAiXVfszscWg

XlttfR3XaL6pym1uSNY7Kg

YvoKCweUnN8gZyodRYysLA

Upvotes: 0

Geert Wille
Geert Wille

Reputation: 1654

You should always (in normal use cases) use an auto incremented ID for performance reasons. If you're purpose is to be able to somewhat hide the next post because someone could be guessing for it then you better add some kind of hashed unique field to your database.

Upvotes: 1

jdp
jdp

Reputation: 3516

You should always use an auto_increment field as the primary key of your database. Not doing that costs you a great deal in performance. You can certainly create a secondary ID field with your random ID. I'd probably use a hashing function to get the best chance of a random string:

<?php $key = md5(rand(0,999).time().$myItemTitle); // ex. ce4075a3d3f6fd757eb6dd44810cbe14

Upvotes: 1

Related Questions