FraanXT Posted May 25, 2014 Share Posted May 25, 2014 Hello guys, I have a php page that inserts some data in a table, example: Name, email, password. Every line have an identificator(ID) that have autoincrement, so when I insert some data it generates the following number(exemple if last line have id 13, id 14 will be the next(as always). I want to continue using this method but without using this id type, I want to use random ids like 1234567890 and the next line 4324234234(exemple), but always a new and unique id, pretty simple. Would have to make it creating a random number, checking in table if it exists and then insert it? or I can make it with mysql or any fastest method? Sorry for my english, I don't know how to explain that. If someone can help me please. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 25, 2014 Share Posted May 25, 2014 your database table(s) should continue to use the auto-increment id as a primary key. this id should be used for internal purposes, i.e. relating database tables to each other, identifying the user in a login session variable... if you need to have a random and unique id for external use, i.e. a remember-me cookie, a password reset token, a form/link consumable token,..., then generate a value using uniqid(), and store this for each visitor in addition to the auto-increment id. you can enforce unique values using the database table, by defining any database column storing the values as a unique key and checking if when storing the value if the query produced an error for a duplicate value. Quote Link to comment Share on other sites More sharing options...
FraanXT Posted May 25, 2014 Author Share Posted May 25, 2014 your database table(s) should continue to use the auto-increment id as a primary key. this id should be used for internal purposes, i.e. relating database tables to each other, identifying the user in a login session variable... if you need to have a random and unique id for external use, i.e. a remember-me cookie, a password reset token, a form/link consumable token,..., then generate a value using uniqid(), and store this for each visitor in addition to the auto-increment id. you can enforce unique values using the database table, by defining any database column storing the values as a unique key and checking if when storing the value if the query produced an error for a duplicate value. How uniqid() works? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 25, 2014 Share Posted May 25, 2014 lol, the php documentation for uniqid recommends using openssl_random_pseudo_bytes - http://us1.php.net/manual/en/function.openssl-random-pseudo-bytes.php Quote Link to comment Share on other sites More sharing options...
FraanXT Posted May 25, 2014 Author Share Posted May 25, 2014 lol, the php documentation for uniqid recommends using openssl_random_pseudo_bytes - http://us1.php.net/manual/en/function.openssl-random-pseudo-bytes.php I mean which differences have of rand()? I will need to check the database for coincidents anyways right? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 25, 2014 Share Posted May 25, 2014 (edited) rand(), mt_rand() and uniqid() produce very poor pseudo-random numbers derived from trivial factors like the server time and the process ID. With a bit of effort, it's in fact possible to predict the next value. If the IDs aren't secret, and if the traffic of your website is low, then those functions may still be good enough in your case. But if you want “real” random numbers, you must use the generator of your operating system. There are three ways to access it: through openssl_random_pseudo_bytes() as mentioned by mac_gyver through mcrypt_create_iv() by reading directly from the system device (e. g. /dev/urandom) If you read 16 bytes from any of those interfaces, you don't have to worry about collisions. There won't be any. Edited May 25, 2014 by Jacques1 Quote Link to comment Share on other sites More sharing options...
Solution FraanXT Posted May 25, 2014 Author Solution Share Posted May 25, 2014 rand(), mt_rand() and uniqid() produce very poor pseudo-random numbers derived from trivial factors like the server time and the process ID. With a bit of effort, it's in fact possible to predict the next value. If the IDs aren't secret, and if the traffic of your website is low, then those functions may still be good enough in your case. But if you want “real” random numbers, you must use the generator of your operating system. There are three ways to access it: through openssl_random_pseudo_bytes() as mentioned by mac_gyver through mcrypt_create_iv() by reading directly from the system device (e. g. /dev/urandom) If you read 16 bytes from any of those interfaces, you don't have to worry about collisions. There won't be any. Thank you bro, helped me a lot. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.