Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class User extends CI_Controller {
- public function __construct() {
- parent::__construct();
- $this->load->library('session');
- $this->load->model('users_model', '', TRUE);
- }
- public function index() {
- // Authenticate
- echo 'Logged in as ' . $this->session->userdata('screen_name');
- }
- public function authenticate() {
- if( ! $this->session->userdata('id')) {
- // Load libraries
- $this->load->library('form_validation');
- // Header
- $data['page_title'] = 'Authentication Page';
- $this->load->view('header', $data);
- // Set validation rules
- $this->form_validation->set_rules('username', 'Username', 'required|min_length[3]|max_length[15]');
- $this->form_validation->set_rules('password', 'Password', 'required|min_length[5]');
- // Validate
- if( ! $this->form_validation->run()) {
- // Form
- $this->load->view('login');
- } else {
- // Get data
- $username = $this->input->post('username');
- $password = sha1($this->input->post('password'));
- // Check the credentials
- $user_info = $this->users_model->check_user($username, $password);
- if($user_info) {
- // Login successful
- // Create user info array to be passed to set_userdata()
- $session_info['id'] = $user_info->id;
- $session_info['username'] = $user_info->username;
- $session_info['screen_name'] = $user_info->screen_name;
- // Set the data
- $this->session->set_userdata($session_info);
- $this->username = $this->session->userdata('username');
- $this->screen_name = $this->session->userdata('screen_name');
- // Redirect to the main page
- redirect('/');
- } else {
- // Invalid data; Reload the form
- $data['invalid'] = 'Sorry, but the username/password combination is invalid,
- or you do not have enough privileges yet. Please try again!';
- $this->load->view('login', $data);
- }
- }
- // Footer
- $this->load->view('footer');
- } else {
- redirect('/');
- }
- }
- public function logout() {
- if($this->session->userdata('id')) {
- // Clear user data
- $this->session->sess_destroy();
- redirect('/user/authenticate');
- } else {
- // Already logged out
- }
- }
- public function register() {
- if( ! $this->session->userdata('id')) {
- // Form validation
- $this->load->library('form_validation');
- // Header
- $data['page_title'] = 'Registration Page';
- $this->load->view('header', $data);
- // Form validation rules
- $this->form_validation->set_rules('username', 'Username', 'required|min_length[4]|max_length[15]');
- $this->form_validation->set_rules('screen_name', 'Screen Name', 'min_length[4]|max_length[25]');
- $this->form_validation->set_rules('password', 'Password', 'required|min_length[5]|matches[pass_conf]');
- $this->form_validation->set_rules('pass_conf', 'Password Confirmation', 'required|min_length[5]|');
- $this->form_validation->set_rules('email', 'Email', 'required|valid_email|min_length[5]');
- // Run the validation
- if( ! $this->form_validation->run()) {
- // Form
- $this->load->view('register');
- } else {
- // Get the data
- $username = $this->input->post('username');
- $password = sha1($this->input->post('password'));
- $email = $this->input->post('email');
- $screen_name = $this->input->post('screen_name');
- // If the Username, the Screen Name or the Email Address are already in use, display an error message
- if( ! ($this->users_model->check_field('username', $username)
- && $this->users_model->check_field('screen_name', $screen_name)
- && $this->users_model->check_field('email', $email))) {
- // Rebuild the form
- $data['invalid'] = 'The Username, Screen Name or Email Address are already in use';
- $this->load->view('register', $data);
- } else {
- // Add the user
- $user_id = $this->users_model->add_user($username, $password, $email, $screen_name);
- // Display registration link
- $registration_url = base_url() . 'user/activate/' . sha1($user_id);
- $data['success'] = 'Congratulations! You are now registered in our database,
- but in order to start using your account,
- you will need to activate it by pressing the link below:
- ' . "\n\n" .
- '<a href="' . $registration_url . '">' . $registration_url . '</a>';
- $this->load->view('message', $data);
- }
- }
- $this->load->view('footer');
- } else {
- redirect('/');
- }
- }
- public function activate($user_id) {
- $result = $this->users_model->activate_account($user_id);
- if($result == 1) {
- $data['error'] = 'This account is already activated!';
- $this->load->view('message', $data);
- } else if($result == 2) {
- $data['error'] = 'The requested account could not be found!';
- $this->load->view('message', $data);
- } else {
- $data['success'] = 'The account has been successfully activated! <a href="' . site_url('user/authenticate') . '">Click here to authenticate</a>!';
- $this->load->view('message', $data);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement