Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2012-01-09 23:43:02

lithium002
Member
Registered: 2011-04-29
Posts: 25

Random String Plugin. Any help?

Hi everyone,

I’m trying to develop a simple plugin to do a mundane task and do it well, but being a novice developer, I was hoping some of the geniuses here could try and analyze my code to see if it does it well, or if there is something much simpler, or safer to employ instead.

I’m trying to generate a random unique string that will track down order information as submitted through an HTML form on the textpattern site. This is my code:

/** * Generates a random, unique string */

function kam_random_string($atts, $thing=’‘) {

$kam_random_string = lAtts(array( ‘length’ => ‘’ ), $atts);

extract($kam_random_string);

do { $string = uniqid(); $unique = safe_field(“txp_custom_table”, “internal_tracking_id = ‘$string’”); } while ($unique != false);

safe_insert(“txp_custom_table”, “internal_tracking_id = ‘$string’”); return $string; }

I previously had a function that generated the string, however, I kept running into a problem. The while loop would terminate and no string was being passed back on certain occasions when I used a length of 1. I used digits from 1 to 9 and set the length to 1 in order to verify that the script would not generate the same digit twice and terminate after having all nine digits in the table, however, it didn’t function like I had planned.

I read around that it is almost impossible to generate the same string twice with uniqid(), and so I decided to use it instead of creating my own function and debugging it to no end.

Is this a safe choice in your opinion? Any advice would be extremely appreciated!

Thank you.

Last edited by lithium002 (2012-01-09 23:47:31)

Offline

Board footer

Powered by FluxBB