Forum Moderators: coopster

Message Too Old, No Replies

Generating random text strings

         

lindajames

10:58 pm on Jun 23, 2003 (gmt 0)

10+ Year Member



Can anyone tell me how i can use PHP to generate random text strings consisting of numbers, uppercase letters and lowercase letters, a mixture of all. (the string must be 16 characters long) for example:

1cFPx17XsqWX2g4b6

Any suggestions would be appreciated.

Cheers
Linda

jeremy goodrich

11:01 pm on Jun 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Create an array of the strings you want to generate, and then randomly select one element from the array in a loop that runs the number that you want the string to be.

lindajames

11:02 pm on Jun 23, 2003 (gmt 0)

10+ Year Member



Well, like that i have create a list of many strings and then randomize them. isnt there anyway just to create a random string from scratch?

jeremy goodrich

11:03 pm on Jun 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, you can fill the array with a random order of the symbols you need as a 'seed' data set, and then randomly pull from it. There should be a php function for 'all letters & numbers' and you can use that in a loop to fill the array.

jatar_k

11:10 pm on Jun 23, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could also use an array of the letters and numbers you want then use something like shuffle [ca.php.net] to mix them up and then pull the first 16 elements.

oilman

11:12 pm on Jun 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jeez jatar - always making people do it themselves ;) Being the non-programmer that I am I ususally do one of 2 things when I need a bit of code:
1) I head to HotScripts.com (for random text: [hotscripts.com...]

2) Buy jatar a beer and get him to do it for me - hehehe

[edited by: jatar_k at 11:15 pm (utc) on June 23, 2003]
[edit reason] oilman has trouble with working links [/edit]

drbrain

11:20 pm on Jun 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



read from /dev/random, and map the data into characters.

How random does the data need to be? Per session? Per hit? Why not use an MD5 of client IP concatenated with time since the epoch?

lindajames

2:17 am on Jun 24, 2003 (gmt 0)

10+ Year Member



i took oilman's suggestion and got hold of a decent script that does the job. However, im passing the 16 digit string as a parameter called "TXT" to another script, and in the other script I have the following code:

<?php
$TXT = $_GET[TXT];
echo "$TXT";
?>

However, if the value of TXT is nothing i want the above code to print a message saying "error", can anyone tell me how that can be done?

cheers
Linda

jatar_k

3:03 am on Jun 24, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



<?php
if ($_GET[TXT] == "") {
echo "oops, the variable was empty";
} else {
echo $_GET[TXT];
}
?>

grahamstewart

4:07 am on Jun 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




if (!isset($_GET['TXT']) or $_GET['TXT'] == '' )
print 'Error';
else
print $_GET['TXT'];

<edit reason: snapping my braces>

[edited by: grahamstewart at 4:39 am (utc) on June 24, 2003]

jatar_k

4:08 am on Jun 24, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that brace after the if might throw it off ;)

grahamstewart

4:39 am on Jun 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Irk.. right you are... *edited*

grahamstewart

4:44 am on Jun 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I wasn't trying to be contrary by the way. But your version causes..


PHP Notice: Use of undefined constant TXT - assumed 'TXT' in test.php on line 2
PHP Notice: Undefined index: TXT in test.php on line 2

..if you have error reporting turned on.

jatar_k

4:53 am on Jun 24, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hehe, I did that way first and then switched it based on "if the value of TXT is nothing" I thought about that and wondered if it would still be passed empty.

So I went with the trimmed down version that might make me repost but also might teach more. ;)

tricky business and yes you are 100% right

vincevincevince

11:40 pm on Jun 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



echo substr(md5(time()),0,16);