-
Notifications
You must be signed in to change notification settings - Fork 0
/
Register.php
89 lines (77 loc) · 3.02 KB
/
Register.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Class Register.
*
* This controller serves the registration process.
*/
class Register extends CI_Controller
{
/**
* Register constructor.
*
* Initialize the database, loads the myredis library, user model and audit model.
*/
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library('myredis');
$this->load->model('user_model');
$this->load->model('audit_model');
}
/**
* Default function.
*
* Gets the user information in the registration form, validates it and create an account for the user in the database.
* Records the registered user's details in the Redis storage.
*/
function index()
{
$this->form_validation->set_rules(
'email',
'Email',
'trim|required|valid_email|is_unique[users.email]',
array(
'is_unique' => 'Email address already exists.'
)
);
$this->form_validation->set_rules('password', 'Password', 'trim|required|md5');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required|md5|matches[password]');
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('contact_no', 'Contact No', 'trim|required');
if ($this->form_validation->run()) {
$name = $this->input->post('name');
$email = $this->input->post('email');
$password = $this->input->post('password');
$contactNo = $this->input->post('contact_no');
$data = [
'name' => $name,
'email' => $email,
'password' => $password,
'contact_no' => $contactNo
];
$userId = $this->user_model->save_user($data);
if ($userId) {
$this->session->set_flashdata('msg', '<div class="alert alert-success text-center">Successfully Registered!</div>');
$auditData = [
'type' => AUDIT_REGISTER,
'id_user' => $userId,
'ip_address' => $this->input->ip_address()
];
$this->audit_model->save_audit($auditData);
/*
* Record all registered user's name, emails in Redis
*/
$redis = $this->myredis->get_instance();
$redis->hset('USER:' . $userId, 'name', $name);
$redis->hset('USER:' . $userId, 'email', $email);
$redis->rpush('REG_USERS', $email);
redirect('/');
} else {
$this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Failed to register. Please try later!</div>');
}
}
$this->load->view('register');
}
}