cult3

How to create an invite system using PHP

Oct 26, 2011

Table of contents:

  1. What do we need
  2. The database
  3. The Form
  4. Processing the form
  5. Using the invite code
  6. Conclusion

When launching a new product or website online, it has become popular to only allow the first set of users in with an invite. Although there are many different ways to accomplish this, here I’m going to show you how to make a system yourself using PHP. This is intended to only show you the bare bones of a system, but if you have any questions, feel free to leave a comment.

What do we need

Here is exactly what we are going to need to make our invite system

  1. Mysql database
  2. A php page to accept the email address

And that is really it!

The database

I’m not going to go into depth about creating the database as there are already thousands of tutorials out there that will be able to explain it far better than I can. Below is a screenshot of my simple table that I’ll be using for this tutorial.

The Form

Next we are going to create the form that will accept the user’s email address and a button to submit the form to be processed by the PHP. Again, I’m only giving you the bare bones of what you need as there are already thousands of tutorials out there if you are looking for more advanced functionality.

<form action="" method="POST">
  <input id="email" name="email" type="text" value="Email">
  </input>
<input name="submit" type="submit" value="Submit">
</input>
</form>

Processing the form

As you can see from the code above, we are going to be processing the form on the same page. To do this we must determine if the submit button has been pressed. This will make a decision to process the form or not. To do this, we will be using the following code.

if (isset($_POST["submit"])) {
}

Next we must get the email address and save it as a variable, like this.

$email = $_POST["email"];

Now at this stage you should do some checks on the email address that was submitted. For Helplist I check to ensure it is a proper email address and check to see if that email address has already been submitted. There are many ways of doing this, and, once again, I’ll leave it up to you to find the right validation methods you want from the numerous tutorials out there on the Internet.

Next we will create the invitation code that will be a unique 10 character string that will be used later to send the invite to the user.

$length = 10;
$inviteCode = "";
$characters = "0123456789abcdefghijklmnopqrstuvwxyz";

mysql_query(
    'INSERT INTO invitation (email, inviteCode) Values ($email, $inviteCode)'
);

Using the invite code

Once people start signing up to your invite system you will get a database of email address and unique invite codes. Once you are ready to start inviting people to your website simply write a script that will send them an email with a link to a file such as register.php?invite=invitecode. The user will then be able to register using their email address that will verify against the invite code in the URL. If they try and share their invite code, the other users won’t be able to get access as that invite code won’t be registered with their email address.

Conclusion

I hope that was clear and straight forward. With these type of tutorials I always find that they’re are too bloated for what I need. I hope by keeping it simple I’ve allowed you to get what you need to complete your project.

If you have any questions, feel free to leave a comment!

Philip Brown

@philipbrown

© Yellow Flag Ltd 2024.