You are on page 1of 11

8/2/2014

Creating a registration form using PHP

HTML Form Guide All about web forms! Home All Posts About You are here: Home All Posts PHP Form Creating a registration form using PHP

Creating a registration form using PHP


in PHP Form Creating a membership based site seems like a daunting task at first. If you ever wanted to do this by yourself, then just gave up when you started to think how you are going to put it together using your PHP skills, then this article is for you. We are going to walk you through every aspect of creating a membership based site, with a secure members area protected by password. The whole process consists of two big parts: user registration and user authentication. In the first part, we are going to cover creation of the registration form and storing the data in a MySQL database. In the second part, we will create the login form and use it to allow users access in the secure area.

Download the code


You can download the whole source code for the registration/login system from the link below: RegistrationForm.zip Configuration & Upload The ReadMe file contains detailed instructions. Open the source\include\membersite_config.php file in a text editor and update the configuration. (Database login, your websites name, your email address etc). Upload the whole directory contents. Test the register.php by submitting the form.

The registration form

http://www.html-form-guide.com/php-form/php-registration-form.html

1/11

8/2/2014

Creating a registration form using PHP

In order to create a user account, we need to gather a minimal amount of information from the user. We need his name, his email address and his desired username and password. Of course, we can ask for more information at this point, but a long form is always a turn-off. So lets limit ourselves to just those fields. Here is the registration form:
< f o r mi d = ' r e g i s t e r 'a c t i o n = ' r e g i s t e r . p h p 'm e t h o d = ' p o s t ' a c c e p t c h a r s e t = ' U T F 8 ' > < f i e l d s e t> < l e g e n d > R e g i s t e r < / l e g e n d > < i n p u tt y p e = ' h i d d e n 'n a m e = ' s u b m i t t e d 'i d = ' s u b m i t t e d 'v a l u e = ' 1 ' / > < l a b e lf o r = ' n a m e '> Y o u rF u l lN a m e * :< / l a b e l > < i n p u tt y p e = ' t e x t 'n a m e = ' n a m e 'i d = ' n a m e 'm a x l e n g t h = " 5 0 "/ > < l a b e lf o r = ' e m a i l '> E m a i lA d d r e s s * : < / l a b e l > < i n p u tt y p e = ' t e x t 'n a m e = ' e m a i l 'i d = ' e m a i l 'm a x l e n g t h = " 5 0 "/ > < l a b e lf o r = ' u s e r n a m e '> U s e r N a m e * : < / l a b e l > < i n p u tt y p e = ' t e x t 'n a m e = ' u s e r n a m e 'i d = ' u s e r n a m e 'm a x l e n g t h = " 5 0 "/ > < l a b e lf o r = ' p a s s w o r d '> P a s s w o r d * : < / l a b e l > < i n p u tt y p e = ' p a s s w o r d 'n a m e = ' p a s s w o r d 'i d = ' p a s s w o r d 'm a x l e n g t h = " 5 0 "/ > < i n p u tt y p e = ' s u b m i t 'n a m e = ' S u b m i t 'v a l u e = ' S u b m i t '/ > < / f i e l d s e t > < / f o r m >

So, we have text fields for name, email and the password. Note that we are using the password widget for better usability.

Form validation
At this point it is a good idea to put some form validation code in place, so we make sure that we have all the data required to create the user account. We need to check if name and email, and password are filled in and that the email is in the proper format. We can use the free JavaScript form validation script to add form validations quickly and easily, with lesser code. Here is a sample JavaScript validation code to be used for the sample form we created earlier:
v a rf r m v a l i d a t o r =n e wV a l i d a t o r ( " r e g i s t e r " ) ; f r m v a l i d a t o r . E n a b l e O n P a g e E r r o r D i s p l a y ( ) ; f r m v a l i d a t o r . E n a b l e M s g s T o g e t h e r ( ) ;
http://www.html-form-guide.com/php-form/php-registration-form.html 2/11

8/2/2014

Creating a registration form using PHP

f r m v a l i d a t o r . a d d V a l i d a t i o n ( " n a m e " , " r e q " , " P l e a s ep r o v i d ey o u rn a m e " ) ; f r m v a l i d a t o r . a d d V a l i d a t i o n ( " e m a i l " , " r e q " , " P l e a s ep r o v i d ey o u re m a i la d d r e s s " ) ; f r m v a l i d a t o r . a d d V a l i d a t i o n ( " e m a i l " , " e m a i l " , " P l e a s ep r o v i d eav a l i de m a i la d d r e s s " ) ; f r m v a l i d a t o r . a d d V a l i d a t i o n ( " u s e r n a m e " , " r e q " , " P l e a s ep r o v i d eau s e r n a m e " ) ; f r m v a l i d a t o r . a d d V a l i d a t i o n ( " p a s s w o r d " , " r e q " , " P l e a s ep r o v i d eap a s s w o r d " ) ;

To be on the safe side, we will also have the same validations on the server side too. For server side validations, we will use the PHP form validation script

Handling the form submission


Now we have to handle the form data that is submitted. Here is the sequence (see the file fg_membersite.php in the downloaded source):
f u n c t i o nR e g i s t e r U s e r ( ) { i f ( ! i s s e t ( $ _ P O S T [ ' s u b m i t t e d ' ] ) ) { r e t u r nf a l s e ; } $ f o r m v a r s=a r r a y ( ) ; i f ( ! $ t h i s > V a l i d a t e R e g i s t r a t i o n S u b m i s s i o n ( ) ) { r e t u r nf a l s e ; } $ t h i s > C o l l e c t R e g i s t r a t i o n S u b m i s s i o n ( $ f o r m v a r s ) ; i f ( ! $ t h i s > S a v e T o D a t a b a s e ( $ f o r m v a r s ) ) { r e t u r nf a l s e ; } i f ( ! $ t h i s > S e n d U s e r C o n f i r m a t i o n E m a i l ( $ f o r m v a r s ) ) { r e t u r nf a l s e ; } $ t h i s > S e n d A d m i n I n t i m a t i o n E m a i l ( $ f o r m v a r s ) ; r e t u r nt r u e ; }

First, we validate the form submission. Then we collect and sanitize the form submission data (always do this before sending email, saving to database etc). The form submission is then saved to the database table. We send an email to the user requesting confirmation. Then we intimate the admin that a user has registered.

Saving the data in the database


Now that we gathered all the data, we need to store it into the database.
http://www.html-form-guide.com/php-form/php-registration-form.html 3/11

8/2/2014

Creating a registration form using PHP

Here is how we save the form submission to the database.


f u n c t i o nS a v e T o D a t a b a s e ( & $ f o r m v a r s ) { i f ( ! $ t h i s > D B L o g i n ( ) ) { $ t h i s > H a n d l e E r r o r ( " D a t a b a s el o g i nf a i l e d ! " ) ; r e t u r nf a l s e ; } i f ( ! $ t h i s > E n s u r e t a b l e ( ) ) { r e t u r nf a l s e ; } i f ( ! $ t h i s > I s F i e l d U n i q u e ( $ f o r m v a r s , ' e m a i l ' ) ) { $ t h i s > H a n d l e E r r o r ( " T h i se m a i li sa l r e a d yr e g i s t e r e d " ) ; r e t u r nf a l s e ; } i f ( ! $ t h i s > I s F i e l d U n i q u e ( $ f o r m v a r s , ' u s e r n a m e ' ) ) { $ t h i s > H a n d l e E r r o r ( " T h i sU s e r N a m ei sa l r e a d yu s e d .P l e a s et r ya n o t h e ru s e r n a m e " ) ; r e t u r nf a l s e ; } i f ( ! $ t h i s > I n s e r t I n t o D B ( $ f o r m v a r s ) ) { $ t h i s > H a n d l e E r r o r ( " I n s e r t i n gt oD a t a b a s ef a i l e d ! " ) ; r e t u r nf a l s e ; } r e t u r nt r u e ; }

Note that you have configured the Database login details in the membersite_config.php file. Most of the cases, you can use localhost for database host. After logging in, we make sure that the table is existing.(If not, the script will create the required table). Then we make sure that the username and email are unique. If it is not unique, we return error back to the user.

The database table structure


This is the table structure. The CreateTable() function in the fg_membersite.php file creates the table. Here is the code:
f u n c t i o nC r e a t e T a b l e ( ) { $ q r y=" C r e a t eT a b l e$ t h i s > t a b l e n a m e( " . " i d _ u s e rI N TN O TN U L LA U T O _ I N C R E M E N T, " . " n a m eV A R C H A R (1 2 8)N O TN U L L, " . " e m a i lV A R C H A R (6 4)N O TN U L L, " . " p h o n e _ n u m b e rV A R C H A R (1 6)N O TN U L L, " . " u s e r n a m eV A R C H A R (1 6)N O TN U L L, " . " p a s s w o r dV A R C H A R (3 2)N O TN U L L, " . " c o n f i r m c o d eV A R C H A R ( 3 2 ), " . " P R I M A R YK E Y(i d _ u s e r) " . " ) " ; i f ( ! m y s q l _ q u e r y ( $ q r y , $ t h i s > c o n n e c t i o n ) ) { $ t h i s > H a n d l e D B E r r o r ( " E r r o rc r e a t i n gt h et a b l e\ n q u e r yw a s \ n$ q r y " ) ; r e t u r nf a l s e ; }
http://www.html-form-guide.com/php-form/php-registration-form.html 4/11

8/2/2014

Creating a registration form using PHP

r e t u r nt r u e ; }

The id_user field will contain the unique id of the user, and is also the primary key of the table. Notice that we allow 32 characters for the password field. We do this because, as an added security measure, we will store the password in the database encrypted using MD5. Please note that because MD5 is an one-way encryption method, we wont be able to recover the password in case the user forgets it.

Inserting the registration to the table


Here is the code that we use to insert data into the database. We will have all our data available in the $ f o r m v a r sarray.
f u n c t i o nI n s e r t I n t o D B ( & $ f o r m v a r s ) { $ c o n f i r m c o d e=$ t h i s > M a k e C o n f i r m a t i o n M d 5 ( $ f o r m v a r s [ ' e m a i l ' ] ) ; $ i n s e r t _ q u e r y=' i n s e r ti n t o' . $ t h i s > t a b l e n a m e . ' ( n a m e , e m a i l , u s e r n a m e , p a s s w o r d , c o n f i r m c o d e ) v a l u e s ( " '.$ t h i s > S a n i t i z e F o r S Q L ( $ f o r m v a r s [ ' n a m e ' ] ).' " , " '.$ t h i s > S a n i t i z e F o r S Q L ( $ f o r m v a r s [ ' e m a i l ' ] ).' " , " '.$ t h i s > S a n i t i z e F o r S Q L ( $ f o r m v a r s [ ' u s e r n a m e ' ] ).' " , " '.m d 5 ( $ f o r m v a r s [ ' p a s s w o r d ' ] ).' " , " '.$ c o n f i r m c o d e.' " ) ' ; i f ( ! m y s q l _ q u e r y ($ i n s e r t _ q u e r y, $ t h i s > c o n n e c t i o n ) ) { $ t h i s > H a n d l e D B E r r o r ( " E r r o ri n s e r t i n gd a t at ot h et a b l e \ n q u e r y : $ i n s e r t _ q u e r y " ) ; r e t u r nf a l s e ; } r e t u r nt r u e ; }

Notice that we use PHP function md5() to encrypt the password before inserting it into the database. Also, we make the unique confirmation code from the users email address.

Sending emails
Now that we have the registration in our database, we will send a confirmation email to the user. The user has to click a link in the confirmation email to complete the registration process.
f u n c t i o nS e n d U s e r C o n f i r m a t i o n E m a i l ( & $ f o r m v a r s ) { $ m a i l e r=n e wP H P M a i l e r ( ) ; $ m a i l e r > C h a r S e t=' u t f 8 ' ; $ m a i l e r > A d d A d d r e s s ( $ f o r m v a r s [ ' e m a i l ' ] , $ f o r m v a r s [ ' n a m e ' ] ) ; $ m a i l e r > S u b j e c t=" Y o u rr e g i s t r a t i o nw i t h" . $ t h i s > s i t e n a m e ;

http://www.html-form-guide.com/php-form/php-registration-form.html

5/11

8/2/2014

Creating a registration form using PHP

$ m a i l e r > F r o m=$ t h i s > G e t F r o m A d d r e s s ( ) ; $ c o n f i r m c o d e=u r l e n c o d e ( $ t h i s > M a k e C o n f i r m a t i o n M d 5 ( $ f o r m v a r s [ ' e m a i l ' ] ) ) ; $ c o n f i r m _ u r l=$ t h i s > G e t A b s o l u t e U R L F o l d e r ( ) . ' / c o n f i r m r e g . p h p ? c o d e = ' . $ c o n f i r m c o d e ; $ m a i l e r > B o d y= " H e l l o" . $ f o r m v a r s [ ' n a m e ' ] . " \ r \ n \ r \ n " . " T h a n k sf o ry o u rr e g i s t r a t i o nw i t h" . $ t h i s > s i t e n a m e . " \ r \ n " . " P l e a s ec l i c kt h el i n kb e l o wt oc o n f i r my o u rr e g i s t r a t i o n . \ r \ n " . " $ c o n f i r m _ u r l \ r \ n " . " \ r \ n " . " R e g a r d s , \ r \ n " . " W e b m a s t e r \ r \ n " . $ t h i s > s i t e n a m e ; i f ( ! $ m a i l e r > S e n d ( ) ) { $ t h i s > H a n d l e E r r o r ( " F a i l e ds e n d i n gr e g i s t r a t i o nc o n f i r m a t i o ne m a i l . " ) ; r e t u r nf a l s e ; } r e t u r nt r u e ; }

We use the free PHPMailer script to send the email. Note that we make the confirmation URL point to confirmreg.php?code=XXXX (where XXXX is the confirmation code). In the confirmreg.php script, we search for this confirmation code and update the confirmed field in the table. After completing all these operations successfully, we send an email to the admin (configured in the membersite_config.php file) See also:Making a login form using PHP

Updates
9th Jan 2012 Reset Password/Change Password features are added The code is now shared at GitHub. 25th May 2011 Now you can display the logged-in users name with this code:
W e l c o m eb a c k< ? =$ f g m e m b e r s i t e > U s e r F u l l N a m e ( ) ;? > !

License

The code is shared under LGPL license. You can freely use it on commercial or non-commercial websites. Be Sociable, Share!

Tw eet

26
6/11

http://www.html-form-guide.com/php-form/php-registration-form.html

8/2/2014

Creating a registration form using PHP

12

Share

StumbleUpon

No related posts. Tagged as: download, PHP, php registration form Previous Comments

Jenelyn March 12, 2012 at 6:31 am How about a sample css cose for good background??

nima March 12, 2012 at 1:28 pm hi dear thx for this code but i have problem i get Failed sending registration confirmation email. plz help!!!!! what is the problems?

anec March 13, 2012 at 6:27 pm I receive the following message: Can you help me out? Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in //// /include/membersite_config.php on line 19 I am worried about what went wrong while I assume every thing has gone as per suggestions. Thanks Mate

Freddy M March 14, 2012 at 2:55 am I have a GODADDY hosted web site. At the start, the email confirmation was NOT sent out to a FEW people who signed up but now, NO emails are sent out and it seems there is a delay. what is the problem?

justberry March 15, 2012 at 1:50 am Hi.. To those people who have having trouble with registration and got an error Failed sending registration confirmation email. You must not have configured your e-mail correct. GO to Includes> membersite_config.php (open with notepad) and find //Provide the email address where you want to get notifications
http://www.html-form-guide.com/php-form/php-registration-form.html 7/11

8/2/2014

Creating a registration form using PHP

$fgmembersite->SetAdminEmail(provide YOUR EMAIL HERE where you will get notification when a new member register); IF you get another error after this with NOTICE: Unindentified index. All you have to do is upload it to your server. This registration will not fully work if your using XAMPP or WAMP. Thats all! Hope Ive help some people here ^^

Marks March 16, 2012 at 1:15 pm someone who had found the solution to this error Failed sending registration confirmation email pls help

John March 16, 2012 at 6:36 pm hi there, im a little confused :(, where do i put what, in what order?

BowlerBitesLane March 17, 2012 at 12:52 am I was hoping someone could give me some insight on how to correct the Failed sending registration confirmation email situation as well. I am using wamp, and the database seems to be updating the username and password, just for some reason wont send out emails. Do I need to download an SMTP server, or would this have more to do with the fact that the sites server is being hosted locally?

BowlerBitesLane March 17, 2012 at 12:55 am Ill be back to let you guys know if I find a solution.

Som March 18, 2012 at 7:50 pm Hi, Thank you for this code. I am completely new to php. I am trying to this code for my site. I have only changed the database related information all rest is kept as it is. The user registration data is correctly saved in the database and all the other functions are working perfectly however the login is not working after registering when I try to login it stays on the login screen does not go to the home page. Please help!! Thanks in advance.

Ivan March 19, 2012 at 10:17 pm Hello guys, whole registration form is great and works like a charm. Thank you very much. However, I have one question. Is it possible to pick up Session variable (Userfullname) and write it in another mysql table? Basicly what my problem is, i made form for ordering, i want beside the order to write in mysql table name from session so i can see who sent the order. If someone could help me it would be very nice. Thanks in advance.
http://www.html-form-guide.com/php-form/php-registration-form.html 8/11

8/2/2014

Creating a registration form using PHP

Gabby March 21, 2012 at 2:30 pm work perfect for me

andrew March 21, 2012 at 7:41 pm so if it wont fully work my xammp? how do i test it works properly. what do i use?

Dana March 21, 2012 at 8:09 pm I am not getting any emails to Admin or to the Users. But I dont get any errors as well. I have contact us form in the same site which uses the class.phpmailer.php in a different folder and it is sending emails. Please help me!

Dana March 22, 2012 at 8:55 pm It actually worked, initially emails were not showing up but now they are, I think it just took some extra time. I am very new to php and programming in general, I was wondering if you could help me in getting id_user from session or add and get it if its not already there. Thanks a lot, this site is very helpful!

Dana March 22, 2012 at 9:01 pm oops sorry, actually I figured how to work around session. thanks a lot..

Kajubi Benon March 23, 2012 at 2:43 pm Thanks for all the advice youre givings us. I want to restrict access to some website pages (PHP & Mysql) to only administrators without registering all of them. Is there any way to do it? Thanks for your help.

princeoo7 March 24, 2012 at 8:32 pm Sir I am Interested to add Admin features to his logging System where when an admin logs in addition features our a special admin panel/ center is opened up. And when a user log in he can edit his account information. so how can I add a admin feature to this code ???? I am A student in F.Y.B.Sc.I.t Please help me out
http://www.html-form-guide.com/php-form/php-registration-form.html 9/11

8/2/2014

Creating a registration form using PHP

Previous Comments Comments on this entry are closed. Previous post: Passing PHP form variables from one page to other Next post: Making a login form using PHP

Categories
http://www.html-form-guide.com/php-form/php-registration-form.html 10/11

8/2/2014

Creating a registration form using PHP

Contact Forms Email Forms Form mail scripts Form Widgets HTML Forms jQuery PHP Form Web forms

Recent Posts
Dreamweaver Email Form Tutorial How to Add an Email Form in Your WordPress Website Fixing the auto-focus Loading a Drop Down List Using jQuery Simfatic Forms: HTML form generator tool

Related Links
PHP Form Making a login form using PHP Passing PHP form variables from one page to other Creating a multi-page order form using PHP How to submit a form using PHP

Subscribe

Copyright 2013 HTML Form Guide. All rights reserved.

http://www.html-form-guide.com/php-form/php-registration-form.html

11/11

You might also like