pgsjoe Posted March 26, 2006 Share Posted March 26, 2006 So I decided I want the ID #'s assigned to each row in my database to be off the wall confusing just to make sure nobody would be able to guess other ones. Here's why...a person enters an event into the database, they get an e-mail back with a link to edit the event that is reference by the ID. So obviously using IDs as simple as 32, 33, 34 some jerk could easily mess with all listings.So I found this code....[code]settype($template, "string");// you could repeat the alphabet to get more randomness$template = "1234567890abcdefghijklmnopqrstuvwxyz";function GetRandomString($length) { global $template; settype($length, "integer"); settype($rndstring, "string"); settype($a, "integer"); settype($b, "integer"); for ($a = 0; $a <= $length; $a++) { $b = rand(0, strlen($template) - 1); $rndstring .= $template[$b]; } return $rndstring; } $id = GetRandomString(30);[/code]And it works beautifully! Get the id as: vzda0brfh10by64qoby25m440n99jhjNow, it feeds it into the database, even displays it back on the page. But, for some reason, it won't allow me to edit it. The "edit" process I'm running through worked for, and still works for any IDs set as a couple digits. I changed my primary key from INT to VARCHAR since it wouldn't accept TEXT as a primary.Just seems weird that it will read and register in the database, but not allow me to edit it. So, wonder if anyone has any ideas why this would happen or if there's a better way.Oh, one more question. I was contemplating using the rand() function, because it says it goes up to 32,000 or something, and I know I won't need more than that. But, if using it as a primary, do I have to worry about it duplicating numbers and then not allowing my customers to post?Thanks in advance. Quote Link to comment Share on other sites More sharing options...
Barand Posted March 26, 2006 Share Posted March 26, 2006 [!--quoteo(post=358460:date=Mar 26 2006, 05:36 AM:name=PGS JoE)--][div class=\'quotetop\']QUOTE(PGS JoE @ Mar 26 2006, 05:36 AM) [snapback]358460[/snapback][/div][div class=\'quotemain\'][!--quotec--]But, if using it as a primary, do I have to worry about it duplicating numbers and then not allowing my customers to post?Thanks in advance.[/quote]Yes. PK must be unique.Having allocated and emailed ID to user, why would you want to edit it? Quote Link to comment Share on other sites More sharing options...
jellystones Posted August 9, 2006 Share Posted August 9, 2006 [quote author=pgsjoe link=topic=89344.msg358460#msg358460 date=1143347760]Oh, one more question. I was contemplating using the rand() function, because it says it goes up to 32,000 or something, and I know I won't need more than that. But, if using it as a primary, do I have to worry about it duplicating numbers and then not allowing my customers to post?Thanks in advance.[/quote]You have to worry about it duplicating numbers in both cases. However with the first case the chance of a duplicate will be much more rare. 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.