Ecommerce Developer
 
 

Code

Tutorial: How to Create a Signup System with CodeIgniter

 

Most every ecommerce website has a user registration page. This basic form typically captures information about the user before processing orders from an online shop.

In this tutorial, I will step through the process of creating a PHP-based registration page using the CodeIgnitor PHP framework.

What You Should Learn

This tutorial article should teach you:

  • How to use the Model-View-Controller architecture;
  • Form validation techniques;
  • How to store data in sessions with CodeIgniter’s session class;
  • Database integration.

I will start the tutorial by creating a View page containing a form. The form will have the following fields:

  • Name – Text area, required
  • Email – Text area, required, check if it is a valid email
  • Password – Password type, required, minimum 5 characters
  • Confirm password – Password type, required, minimum 5 characters, needs to match Password.

In the Controller, I will create the form validation. If the data is not validated, the server will show the errors in the views, and if it is validated, I will demonstrate how to encrypt the password, save to database, set a loginid_ session for the user and redirect the user to "My Panel" page.

The View

Create a PHP file in the Views folder (/system/application/views) called signupform.php_ and write this content:

     <h1>Signup</h1>
     <form method="post" action="<?=site_url('signup');?>">
	<label for="name">Name</label>
	<input type="text" name="name" id="name" value="<?=$name;?>"/>
	<?php echo form_error('name'); ?>
	<br/>

	<label for="email">Email</label>
	<input type="text" name="email" id="email" value="<?=$email;?>"/>
	<?php echo form_error('email'); ?>
	<br/>

	<label for="password">Password</label>
	<input type="password" name="password" id="password"/>
	<?php echo form_error('password'); ?>
	<br/>

	<label for="confirm_password">Confirm Password</label>
	<input type="password" name="confirm_password" id="confirm_password"/>
	<?php echo form_error('confirm_password'); ?>
	<br/>

	<input type="submit" value="Send"/>
     </form>

We have created a simple form in the code above. However there are some explanations we shall specify:

  • action="<?=site_url('signup');?>" - The site_url() function returns your site URL as defined in system/application/config/config.php.
  • value="<?=$name;?>" – the values will be sent from the Controller. We will explain this later.
  • <?php echo form_error('name'); ?> - we will show the errors individually. The form_error() function belongs to the Form validation class.

The Controller

In the Controller, we need to validate the form. If the form is not valid—for example if the password is too short or the name field is left blank—we'll want to reloaded on the view, with an error message.

If the form content is valid, the control should encrypt the password, insert the form content into our database, generate an ID with the function $this->db->insert_id(), and a session should be set with the ID so that the user is logged in.

We'll use a controller called Signup and a function called index to achieve our goals in this section of the tutorial. The URL to the path will be http://yousite.tld/signup . In the index function, we will load the URL helper (we use the siteurl() function) and the formvalidation library.

Here is the code:

     <?php
     class Signup extends Controller {
          function index() {
               $this->load->helper('url');
               $this->load->library('form_validation');
                    if ($this->form_validation->run() == FALSE) {
                        $this->load->view('signup_form');
                     }
                     else {
                    }
               }
          }
     ?>

Now we can setup the validation rules.

$this->form_validation->set_rules(‘’, ‘’, ‘’) .

In case of validation failure, we need to show the data that has already been submitted. Here is the code that will do this for us.

     <?php
     class Signup extends Controller {
          function index() {
               $this->load->helper('url');
               $this->load->library('form_validation');
               $this->form_validation->set_rules('name',  'Name',  'required');
               $this->form_validation->set_rules('email',  'Email',  'required|valid_email');
               $this->form_validation->set_rules('password',  'Password',  'required|min_length[5]');
               $this->form_validation->set_rules('confirm_password',  'Confirm password',  'required|min_length[5]|matches[password]');
                    if ($this->form_validation->run() == FALSE) {
		          foreach ($_POST as $key => $val)
			  $data[$key] = $val;
                          $this->load->view('signup_form');
                         }
                         else {
                         }
               }
          }
     ?>

Last, but not least, we need to encrypt the password, insert the data into database if it is valid in order to store the ID in the session and redirect to the MyPanel controller.

We will use the database and the session class here as well as the URL helper that we have already loaded.

     <?php
     class Signup extends Controller {
          function index() {
               $this->load->helper('url');
               $this->load->library(array('form_validation', ‘database’, ‘session’));
               $this->form_validation->set_rules('name',  'Name',  'required');
               $this->form_validation->set_rules('email',  'Email',  'required|valid_email');
               $this->form_validation->set_rules('password',  'Password',  'required|min_length[5]');
               $this->form_validation->set_rules('confirm_password',  'Confirm password',  'required|min_length[5]|matches[password]');
                    if ($this->form_validation->run() == FALSE) {
		          foreach ($_POST as $key => $val)
			  $data[$key] = $val;
                    $this->load->view('signup_form');
                    }
                    else {
                       //encrypt the password
                      $_POST[‘password’] = sha1($_POST[‘password’]);
                       //insert the $_POST array into database.
		     $this->db->insert(‘users’, $_POST);
		     //get the unique ID of the insert.
		     $userid = $this->db->insert_id();
		     //login the  user to his account
		     $this->session->set_userdata(‘login_id’, $userid);
		     //redirect to mypanel controller
		     redirect(‘mypanel’);
                    }
               }
     }
?>

Conclusion

By implementing a signup system, we have learned how to use the MVC system, the form validation, the session class and the database insert. This system can be used as it is or can be improved to fit your website requirements.

Resources

2 Comments

Rss-sm