2

in my table, i insert new records of user(new users). I have a int set as field primary key. What i want is to make this id secure. Because this id public to everyone. Any user or people can see this id.

If id i generate is like this:

0000000001
...
0000000023
...
0000000157
...

it is easy for another person to guess it because ids are in a order which is incremented by 1. Instead, i want to store a "public" id in a separate field which is a aplhanumeric representation of this auto_increment primary id.

So in database, table will be like this:

id------------------public_id
---------------------------------
0000000001 -------- W3UB3VNAU3222
0000000002 -------- 7BNXYO28CN2KK

I thought about using hash. But hash is a fixed length of 40 or above. But i want the public id to be fixed 10 characters in length. Should be unique and if possible, I prefer generating it from id

plz tell how to create this ? any builtin functions that helps me to do this?


I read about hash() function with crc32:http://www.php.net/manual/en/function.hash.php#104987 It will give 8 char length hash. Will it conflict in future ?

Test:

echo hash('crc32', '0000000001'); // gives 6c13f76e
4

5 回答 5

0

The following code example generates an 10 characters long random string that contains 1 digit:

<?php
  function random_string( )
  {
    $character_set_array = array( );
    $character_set_array[ ] = array( 'count' => 10, 'characters' => 'abcdefghijklmnopqrstuvwxyz' );
    $character_set_array[ ] = array( 'count' => 1, 'characters' => '0123456789' ); //you can change count
    $temp_array = array( );
    foreach ( $character_set_array as $character_set )
    {
      for ( $i = 0; $i < $character_set[ 'count' ]; $i++ )
      {
        $temp_array[ ] = $character_set[ 'characters' ][ rand( 0, strlen( $character_set[ 'characters' ] ) - 1 ) ];
      }
    }
    shuffle( $temp_array );
    return implode( '', $temp_array );
  }
?>
于 2012-08-02T12:46:30.680 回答
0

try this

MySql auto-incrementing Alpha-numeric primary key?

CREATE TABLE myItems (
    id INT NOT NULL AUTO_INCREMENT,
    prefix CHAR(30) NOT NULL,
    PRIMARY KEY (id, prefix),

Or

composite (alphanumeric) primary key and auto increment

public function random_id_gen($length)
    {
        //the characters you want in your id
        $characters = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
        $max = strlen($characters) - 1;
        $string = '';

        for ($i = 0; $i < $length; $i++) {
            $string .= $characters[mt_rand(0, $max)];
        }

        return $string;
    }
于 2012-08-02T12:47:42.203 回答
0

You can try to shuffle characters and XOR from original id.

于 2012-08-02T12:49:59.917 回答
0

Generate a hash and take the first 10 letters.

Check if such an ID is already in your database. If it is not you can take it as your ID, otherwise generate and new hash and try again.

If you want to represent your true ID you could chose a seperator letter and map your ID to letters.

Example:

ID:          ...0000021
PUBLIC_ID :  ...Hd3dXBA

X is the separator to separate your mapped ID from the randomly generated part.

2 is mapped to B

1 is mapped to A

于 2012-08-02T12:52:59.477 回答
0

There is no built-in functions that would do that for you, so I'm afraid you'll have to build one yourself. If you're not restricted by the 10 character requirement, have a look at UUID. This post: PHP function to generate v4 UUID will explain how to create a PHP function that will generate a UUID in PHP.

Otherwise try a function along those lines, although you will have to check if the id is unique every time you generate it.

function generateID()
{
    $capital_letters = range("A", "Z");
    $lowcase_letters = range("a", "z");
    $numbers         = range(0, 9);

    $all = array_merge($capital_letters, $lowcase_letters, $numbers);
    $count = count($all);    
    $id    = "";

    for($i = 0; $i < 10; $i++)
    {
        $key = rand(0, $count);
        $id .= $all[$key];
    }

    if(!uniqueId($id))
    {
        return generateID();
    }
    return $id;
}
于 2012-08-02T12:56:17.197 回答