2007.09.08

This post is outdated and may contain bad-practice techniques.

The most useful and almost necessary feature of any interactive website is a registration form. A way to give users access to features while keeping out the riff-raff. While a registration script can be the perfect time for an amateur to grab the bull by the horns it can also leave lots of pitfalls for a more experienced programmer.

For this example I'll be using PHP 4.3x and MySQL 5.1.2x, as well as PHPMyAdmin 2.8. 

The first thing to do is to set up our database. For this example I'll try to keep the number of fields short.

PHPMyAdmin

Five fields should do it.

id - our unique key, should be sent to auto_increment and type int

username - field to store the username, limit to 20 characters

password - field to store the encrypted password, limit to 50 characters

email - field to store the user's email address

date - a field to store the unix timestamp of when the user registers 

Please see my article on Using and Understanding Improper MySQL Field Types for more information on the fieldtypes used here. 

Here is the SQL Query to create the table 

Now that the database is setup the next step is to create the form:

Very basic html form that will pass the form variables to register2.php. This second page will do some validation and insert the information into the table.

 The first line of register2.php sets up the MySQL connection. The next block of code validates the form to ensure that each form field is filled out and if not adds a string to the variables $errors. If $errors is empty and all is well then we can insert the information into the table. I used PHP's addslashes() function around each value that could be tampered with to prevent injection hacks. 

The md5() function produces a 1-way hash of the users password and stores that into the table. The reason for this is the obvious security risk of storing passwords as plain text in the table. Later on when a user attempts to log in we can compare the md5 of the password in the table with the md5 of the password provided by the user. This is a practice that every programmer should get used to using or any another secure method that does not store passwords in plain text.

Running through the form and submitting it your table should look something like this:

PHPMyAdmin

As you can see the password field is a 32 character hexadecimal jumble that looks useless to the naked eye but in fact is to help keep our users secure and less worried about someone stealing their information.

In a later tutorial I will go over how to set up a login script that works with this table schema.  

An interesting and underrate, or underused, function that php makes available is checkdnsrr() which can be used on an email domain to check to see if it is valid. This will involve parsing the email field to get everything after the @ symbol but can be very useful in checking to see if a domain is valid without going as far as sending a confirmation email.

If I get enough of a response from this article I will consider showing how to append this script to work with email confirmation and even lost/forgot passwords.

 

Extra Reading

Creating a PHP Login Script - Comprehensive and detailed tutorial on how to create a complete login script.

PHP Login Script Tutorial -  Another basic registration/login tutorial. Covers all the basics.

Get my RSS Feed!

Comments

dejan on (8.27.2008 8:44 pm) says

Keep on, dude, it's great to read the rest of this article

 

hannah on (10.7.2008 11:04 am) says

i just want to know where to put the form?.. when i click the register link what would be the link? the page containing the form or the register.php? see wee alredy have a page that has the dbconnection... pls help me because when i click register.. the link page is empty..

thanks in advance.. 

 

David on (2.10.2009 1:46 pm) says

Great Job ,
Keep it up. its very educative

 

Anoop K Krishna on (2.13.2009 7:02 am) says

Thanks i will try soon.....

 

Hasnain on (5.26.2009 11:50 am) says

How I Add This Code And How I Upload ?

 

ouakrim on (5.31.2009 6:09 pm) says

 good script 
thanks man

 

S on (6.12.2009 1:58 am) says

please show us how to do it with forgot password as well

 

Ranjith on (8.5.2009 12:36 am) says

Hai there... I`m designing a web site for my colleges inter college cultural`s event... so i want a registration page placed on that web site... fields i`m using are Name(text box),E-mail ID(text box),Phone no(text box),College Name(text box),accommodation(radio button) yes or no,Events(check boxes)choice1,choice2,...choice n,submit(button).... if user fills entire form and click submit all data`s should send to a email and user should receive an automatic email reply from us with a unique code... 

 

tshering tashi on (9.8.2009 9:10 am) says

i realy didnt understand ur registration code but it was some how helped me some field...

 

juice on (9.28.2009 9:30 am) says

this is very nice.

 

edmund on (11.14.2009 9:49 am) says

thanks for you education its very good  

 
* Name
* Email (Will not be displayed)
Website