/    Sign up×
Community /Pin to ProfileBookmark

I seem to be sending a do statement into an infinite loop, and I’m not quite sure why.

Here’s the loop…

do{//create a random number
$rnd = mt_rand();

//check it against existing numbers

$qrr = “SELECT PLrn FROM people WHERE PLrand = ‘$rnd'”;
$rsr = mysql_query($qrr, $link);
$nr = mysql_num_rows($rsr);
if($nr==0)
{
$rchk == “Y”;
}
}while($rchk != “Y”);

The idea is that we create a random number which is assigned to a user when he/she logs on. That random number rotates every 24 hours, but it must be unique.

The do statement is supposed to allocate a random number, and then check it against already stored random numbers to make sure it is unique. If it is, the loop changes the value of $rchk to “Y” and ends the loop. If not, it repeats the loop, generating and checking the random number until it finds one that is unique.

It doesn’t work, and times out after 30 seconds.

Any suggestions?

CTB

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYJun 21.2005 — where is $rchk defined?

why do you name your vars in such a cryptic way?
Copy linkTweet thisAlerts:
@chestertbauthorJun 21.2005 — At the beginning of the script, I set the starting state variables, so

$rchk = 'N';

And not cryptic at all...

$rnd ought to be self explanatory.

$qrr is 'qr' for query and 'r' for random, which means this is the query where I check the random number

$rsr is 'rs' for result and 'r' for random

$nr is 'n' for number of rows and 'r' for random

and $rchk is 'r' for random and 'chk' for check

logical really... well maybe logical for me anyway. I use this because it makes it easy to keep track of which query does what in the script.

For example, I have one script that runs three queries...

$qra queries the admin file

$qrm queries the menu file

and

$qri queries the item file.
Copy linkTweet thisAlerts:
@NogDogJun 21.2005 — I'm not crazy about having to query the database on every loop iteration - that'll definitely slow you down. I think you'd be better off querying once before the loop for a list of all PLrn and save them to an array, then just check to see if your random number is in that array.

If your only concern is to assign a unique number (i.e., you're not trying to set a random order of users), you might want to consider using uniqid() instead and save yourself a lot of trouble.
Copy linkTweet thisAlerts:
@chestertbauthorJun 21.2005 — Thanks NogDog.

What's the difference between mt_rand and uniqid()? I've looked the latter up on the php site and it seems that it just generates a number based on the time in microseconds on the server.

Is it remotely possible that two uniqid numbers could be the same?

In any event, as the random number is 16 digits long, the actual chance of it being used before is remote in the extreme. The chance of two consecutive random numbers being used before are a ten with 31 zeros on the end. That's a number so large I'm not even sure what it's called.

The idea is to build a level of security into a user's logon. The random number is not stored in a cookie, but stored on the server and then passed from page to page, a little like a session id.

Each time the user accesses a page, the random number is checked against the database. If it is found, the user gets access rights according to his/her access level. If it is not found, then the user either gets the lowest access rights or is asked to log on again (depending on the page their logging onto).

Each time the user logs on, a new number will be generated so that even if someone knew his/her user logon id, they'd take about a million years to guess the random number.

We do store the user's logon id in a cookie, so the system will 'remember' them, but the cookie has a last logon date recorded. If that's more than 24 horus old, a new random number is generated.

It would be easier to guess the password and just logon. However, if the user is already logged on and someone does that, then as the user moves from page to page, the random number will be rejected and he'll be asked to logon again, changing the random number and locking out the unauthorized user etc etc etc.

CTB
Copy linkTweet thisAlerts:
@NogDogJun 21.2005 — I believe what uniqid() does with no arguments is return what is essentially a hexidecimal number based on the current system time in microseconds, so it's going to be unique as long as you don't call it twice within the same millionth of a second. However, you can use the optional args to make it even more unique (and longer, of course), something like:
[code=php]
$id = uniqid(rand(100,999), TRUE);
[/code]
Copy linkTweet thisAlerts:
@chestertbauthorJun 21.2005 — thanks nogdog.

i'll give it a try. seems like it could be more efficient.


cheers

CTB
Copy linkTweet thisAlerts:
@chrysJun 21.2005 — I don't know if your question was answered, but from a quick look at your code, I found your problem:

[code=php]
if($nr==0)
{
$rchk == "Y";
}
[/code]
That needs to be $rchk = "Y";

Otherwise that statement just returns false, and no assignment is done.
Copy linkTweet thisAlerts:
@chestertbauthorJun 21.2005 — doh!

This is what happens when you try and write late at night.

Thanks Chris.

CTB
×

Success!

Help @chestertb spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...