google_forms/application/controllers/Users.php

124 lines
4.2 KiB
PHP
Raw Permalink Normal View History

2024-07-11 12:05:44 +00:00
<?php
2024-08-09 12:04:48 +00:00
2024-07-11 12:05:44 +00:00
class Users extends CI_Controller
{
//signup user
public function register()
{
$data['title'] = 'Sign Up';
$this->form_validation->set_rules('username', 'Username', 'required|callback_check_username_exists');
$this->form_validation->set_rules('email', 'Email', 'required|callback_check_email_exists');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('password2', 'Confirm Passsword', 'matches[password]');
2024-08-09 12:04:48 +00:00
if ($this->form_validation->run() === false) {
2024-07-11 12:05:44 +00:00
$this->load->view('templates/header');
$this->load->view('users/register', $data);
$this->load->view('templates/footer');
} else {
$enc_password = md5($this->input->post('password'));
$this->load->model('user_model');
$this->user_model->register($enc_password);
// $this->user_model->register();
$this->session->set_flashdata('user_registered', 'You are now registered and can log in');
redirect('start');
}
}
2024-07-12 11:50:41 +00:00
2024-07-26 12:41:46 +00:00
2024-07-23 12:54:27 +00:00
public function login($form_id = null)
2024-07-11 12:05:44 +00:00
{
$data['title'] = 'Sign In';
2024-07-23 12:54:27 +00:00
$data['form_id'] = $form_id;
2024-08-09 12:04:48 +00:00
2024-07-11 12:05:44 +00:00
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
2024-08-09 12:04:48 +00:00
if ($this->form_validation->run() === false) {
2024-07-11 12:05:44 +00:00
$this->load->view('templates/header');
$this->load->view('users/login', $data);
$this->load->view('templates/footer');
} else {
// Get username
$username = $this->input->post('username');
// Get and encrypt the password
$password = md5($this->input->post('password'));
$this->load->model('user_model');
// Login user
2024-07-12 11:50:41 +00:00
$person_id = $this->user_model->login($username, $password);
2024-08-09 12:04:48 +00:00
2024-07-12 11:50:41 +00:00
if ($person_id) {
2024-07-11 12:05:44 +00:00
// Create session
$user_data = array(
2024-07-12 11:50:41 +00:00
'user_id' => $person_id,
2024-07-11 12:05:44 +00:00
'username' => $username,
'logged_in' => true
);
2024-08-09 12:04:48 +00:00
2024-07-11 12:05:44 +00:00
$this->session->set_userdata($user_data);
2024-08-09 12:04:48 +00:00
2024-07-11 12:05:44 +00:00
// Set message
$this->session->set_flashdata('user_loggedin', 'You are now logged in');
2024-08-09 12:04:48 +00:00
2024-07-23 12:54:27 +00:00
if ($form_id) {
2024-07-26 12:41:46 +00:00
redirect('forms/response_preview/' . $form_id);
2024-07-23 12:54:27 +00:00
} else {
2024-07-26 12:41:46 +00:00
redirect('home');
}
2024-07-11 12:05:44 +00:00
} else {
// Set message
$this->session->set_flashdata('login_failed', 'Login is invalid');
2024-08-09 12:04:48 +00:00
2024-07-26 12:41:46 +00:00
redirect('users/login/' . $form_id);
2024-07-11 12:05:44 +00:00
}
}
}
public function logout()
{
$this->session->unset_userdata('logged_in');
$this->session->unset_userdata('user_id');
$this->session->unset_userdata('username');
$this->session->set_flashdata('user_loggedout', 'You are now logged out');
redirect('users/login');
}
// check if username exists
public function check_username_exists($username)
{
$this->form_validation->set_message('check_username_exists', 'The username is taken.Please choose a different one');
$this->load->model('user_model');
if ($this->user_model->check_username_exists($username)) {
return true;
} else {
return false;
}
}
public function check_email_exists($email)
{
$this->form_validation->set_message('check_email_exists', 'The email is taken.Please choose a different one');
$this->load->model('user_model');
if ($this->user_model->check_email_exists($email)) {
return true;
} else {
return false;
}
}
// public function callback_user_id_exists($id)
// {
// $this->form_validation->set_message('user_id_exists', 'The ID is taken.Please choose a different one');
// $this->load->model('user_model');
// if ($this->user_model->user_id_exists($id)) {
// return true;
// } else {
// return false;
// }
// }
}