CodeIgniter Registration System With Session

CodeIgniter Registration System With Session

Today we are going to show you how to create a CodeIgniter Registration Form and save data into database in simple way. Here we are explaining step by step how can we create simple effective CodeIgniter registration system.

This CodeIgniter Registration System is tested and working fine in case if you getting any kind of errors and not understanding how to implement feel free and comment will back to you.

You can also read CodeIgniter Login System.

Step 1. Create view name of “registration.php” inside (application/views) folder

<?php
/*
	Author: Mukesh Jakhar
	Description: User registration view
*/
defined('BASEPATH') OR exit('No direct script access allowed');
$this->load->helper('url');
?>
<!DOCTYPE HTML>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>User registration form</title>
	<meta name="viewport" content="width=device-width, initial-scale=1" />
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
</head>
<body>
<div class="container">
	<div class="col-md-3"></div>
		<div class="col-md-6">
			<h2>User registration form</h2>
			<?php if(isset($msg)){echo $msg;}?>
			<form method="post" enctype="multipart/form-data">
				<div class="form-group">
					<label>Name</label>
					<input class="form-control" type="text" name="name" />
				</div>
				<div class="form-group">
					<label>Email</label>
					<input class="form-control" type="text" name="email" />
				</div>
				<div class="form-group">
					<label>Phone Number</label>
					<input class="form-control" type="text" name="phone" />
				</div>
				<div class="form-group">
					<label>Password</label>
					<input class="form-control" type="text" name="password" />
				</div>
				<div class="form-group">
					<input class="btn btn-primary" type="submit" value="Publish" />
				</div>
			</form>
		</div>
	</div>
</body>
</html>

Step 2. Create Controller name of “User_registration.php” inside (application/controllers) folder

<?php
/*
	Author: Mukesh Jakhar
	Description: User registration controller class
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class User_registration extends CI_Controller {
	function __construct() {
		parent::__construct();
		$this->load->library('form_validation');
		$this->load->model('Registartion_model');
	}
	public function index()
	{
		if($this->input->post('name'))
		{
			$formData = array(
				'name' => $this->input->post('name'),
				'email' => $this->input->post('email'),
				'phone' => $this->input->post('phone'),
				'password' => $this->input->post('password')
			);
			$this->Registartion_model->add_new_user($formData);
			$res['msg']="New user has been added!";
			$this->load->view('registration', $res);
		}
		else
		{
			$this->load->view('registration');
		}
	}
}

Step 3. Create model name of “Registartion_model.php” inside (application/models) folder

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
	Author: Mukesh Jakhar
	Description: User registration model class
*/
class Registartion_model extends CI_Model{
	function __construct(){
		$this->load->database();
		parent::__construct();
	}

	function add_new_user($formData){
		$this->db->insert('users', $formData);
	}
}
?>
About Author

My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.