Overview
In this example, we will discuss the user signup in CodeIgniter.
We will use three files for the user Signup.
- Signup.php (application\controllers\Signup.php)
- sign_up.php (application\views\sign_up.php)
- Signup_model.php (application\models\Signup_model.php)
applications/controllers/Signup.php
class Signup extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('signup_model');
}
public function sign_up() {
$this->load->view(‘sign_up’);
}
public function register() {
$name = $this->input->post('name');
$email = $this->input->post('email');
$password = $this->input->post('pass');
$phone = $this->input->post('phone');
$data = [
‘name’ => $name,
‘email’ => $email,
‘password’ => $password,
‘phone’ => $phone,
];
$result = $this->signup_model->is_user_exists($email);
if(!$result) {
$user = $this->signup_model->add($data);
if($user) {
echo “Your account created successfully”;
} else {
echo “Something went wrong.”;
}
} else {
echo “User is already exists.”;
}
}
}
applications/models/Signup_model.php
class Signup_model extends CI_Model {
public function add($data) {
return $this->db->insert(‘user_info’, $data);
}
public function is_user_exists($email) {
$this->db->where([‘email’ => $email]);
$query = $this->db->get(‘user_info’);
return $query->num_rows();
}
}
applications/views/sign_up.php
<form method="post" action="<?php echo base_url('user/register') ?>">
<table width="600" align="center" border="1" cellspacing="5" cellpadding="5">
<tr>
<td colspan="2">
<?php echo $error; ?>
</td>
</tr>
<tr>
<td width="230">Enter Your Name</td>
<td width="329">
<input type="text" name="name" />
</td>
</tr>
<tr>
<td>Enter Your Email</td>
<td>
<input type="text" name="email" />
</td>
</tr>
<tr>
<td>Enter Your Mobile</td>
<td>
<input type="text" name="phone" />
</td>
</tr>
<tr>
<td>Enter Your Password</td>
<td>
<input type="password" name="pass" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="save" value="Register Me" />
</td>
</tr>
</table>
</form>
Now, run following URL in the browser: