Hi,

I have a sign up form that generates a activation key and emails it to user and they cannot login until account has been activated etc.

I know how to do the MySQL query to check if activation key already exists and have a function that generates a random key but not sure how to basically tell PHP that if the activation key already exists in DB then generate a new key until one is found that does not already exist.

I gather i may need a loop but not sure. Could someone please help ?

Like i say i know how to do the MySQL query and have created a function that generates a random key just need some sample code to know how to do what i asked above.

Thanks for any help; Much Appreciated

Recommended Answers

All 7 Replies

function getKey()
{
  $key=generateKey();
  if(!chkKeyExists($key))
  {
    return $key;
  }
  else
  {
    getKey();
  }
}

Hi,

Thanks for replying :)

Where you have chkKeyExists am i rite in thinking that would be another function that would check query db and see if the key exists in db? just want to check on that. If so i will get onto it now.

Thanks
PHPLOVER

You can alleviate this problem by using a UUID or hashing the UUID for your activation key.
http://en.wikipedia.org/wiki/Universally_unique_identifier

There are several php modules that do this, but I've been using a pure PHP implementation with a lot of success
Class: http://www.shapeshifter.se/wp-content/uploads/2008/09/classuuid.phps
Usage: http://www.shapeshifter.se/2008/09/29/uuid-generator-for-php/

Check out the probability getting a duplicate key on the Wikipedia article. You basically have the same chance of getting hit by a meteorite as you do of creating a duplicate uuid for every 70 trillion generated.

Thanks for replying but this is far beyond what i want. Also i have not learnt classes. I will await a reply from my second post but thanks for posting anyway :)

Hi,

Thanks for replying :)

Where you have chkKeyExists am i rite in thinking that would be another function that would check query db and see if the key exists in db? just want to check on that. If so i will get onto it now.

Thanks
PHPLOVER

Bump.

Where you have chkKeyExists am i rite in thinking that would be another function that would check query db and see if the key exists in db? just want to check on that. If so i will get onto it now.

Yes, chkKeyExists is function where we check that key already exists in data base or not.

Thanks Chanti :)

Much appreciated your time so thank you.

SOLVED

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.