Quantcast
Channel: CodeIgniter Forums - All Forums
Viewing all articles
Browse latest Browse all 14343

Login System Not Working

$
0
0
Hello so I've been trying to create a login and register system on my CodeIgniter Application. So far it works great, I can create and fetch them(if not logged in) wherever I want them to be displayed, the problem that now comes to me is the restricting part.

For example I have an admin_controller to which I need to restrict the access. Rather than adding a code to any controller that needs the same configuration, I created a "MY_Controller" in the core folder, here is my code:

PHP Code:
<?php
class MY_Controller extends CI_Controller{
    function 
__construct(){
        
parent::__construct();
    }
}

class 
Admin_Controller extends MY_Controller{
    function 
__construct(){
        
parent::__construct();

        
// Check Login
        
if(!$this->session->userdata('logged_in')){
            
redirect('admin/login');
        }
    }
}

class 
Public_Controller extends MY_Controller{
    public function 
__construct(){
        
parent::__construct();
    
        
$this->load->library('menu');

        
$pages_public /*$this->pages*/ $this->menu->get_pages();

        
// Brand/Logo
        
$this->brand 'My Website';

        
// Banner
        
$this->banner_heading 'Welcome To Our Website';
        
$this->banner_text 'This example is a quick exercise to illustrate how the default, static navbar and fixed to top navbar work. It includes the responsive CSS and HTML, so it also adapts to your viewport and device.';
        
$this->banner_link 'pages/show/our-team';
    }


and this is what I have on my admin_controller, so far nothing wrong:

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Pages extends Admin_Controller {
    
    public function 
index(){
        
$data['pages'] = $this->Page_model->get_list();

        
// Load template
        
$this->template->load('admin''default''pages/index'$data);
    }

    public function 
add(){
        
// Field Rules
        
$this->form_validation->set_rules('title''Title''trim|required|min_length[3]');
        
$this->form_validation->set_rules('subject_id''Subject''trim|required');
        
$this->form_validation->set_rules('body''Body''trim|required');
        
$this->form_validation->set_rules('is_published''Publish''required');
        
$this->form_validation->set_rules('is_featured''Feature''required');
        
$this->form_validation->set_rules('order''Order''integer');

        if(
$this->form_validation->run() == FALSE){
            
$subject_options = array();
            
$subject_options[0] = 'Select Page Category';

            
$subject_list $this->Pages_categories_model->get_list();

            foreach(
$subject_list as $subject){
                
$subject_options[$subject->id] = $subject->name;
            }

            
$data['subject_options'] = $subject_options;

            
// Load template
            
$this->template->load('admin''default''pages/add'$data);
        } else {
            
$slug str_replace(' ''-'$this->input->post('title'));
            
$slug strtolower($slug);

            
// Page Data
            
$data = array(
 
               'title'         => $this->input->post('title'),
 
               'slug'          => $slug,
 
               'subject_id'    => $this->input->post('subject_id'),
 
               'body'          => $this->input->post('body'),
 
               'is_published'  => $this->input->post('is_published'),
 
               'is_featured'   => $this->input->post('is_featured'),
 
               'in_menu'       => $this->input->post('in_menu'),
 
               'user_id'       => $this->session->userdata('user_id'),
 
               'order'       => $this->input->post('order')
 
           ); 

 
           // Insert Page
 
           $this->Page_model->add($data);

 
           // Activity Array
            
$data = array(
                
'resource_id'   => $this->db->insert_id(),
                
'type'            => 'page',
                
'action'        => 'added',
                
'user_id'        => $this->session->userdata('user_id'),
                
'message'        => 'A new page was added ('.$data["title"].')'
            
);

            
// Insert Activity
            
$this->Activity_model->add($data);

            
// Set Message
            
$this->session->set_flashdata('success''Page has been added');

            
// Redirect
            
redirect('admin/pages');
        }

    }

    public function 
edit($id){
        
// Field Rules
        
$this->form_validation->set_rules('title''Title''trim|required|min_length[3]');
        
$this->form_validation->set_rules('subject_id''Subject''trim|required');
        
$this->form_validation->set_rules('body''Body''trim|required');
        
$this->form_validation->set_rules('is_published''Publish''required');
        
$this->form_validation->set_rules('is_featured''Feature''required');
        
$this->form_validation->set_rules('order''Order''integer');

        if(
$this->form_validation->run() == FALSE){
            
$data['item'] = $this->Page_model->get($id);

            
$subject_options = array();
            
$subject_options[0] = 'Select Page Category';

            
$subject_list $this->Pages_categories_model->get_list();

            foreach(
$this->Pages_categories_model->get_list() as $subject){
                
$subject_options[$subject->id] = $subject->name;
            }

            
$data['subject_options'] = $subject_options;

            
// Load template
            
$this->template->load('admin''default''pages/edit'$data);
        } else {
            
$slug str_replace(' ''-'$this->input->post('title'));
            
$slug strtolower($slug);

            
// Page Data
            
$data = array(
 
               'title'         => $this->input->post('title'),
 
               'slug'          => $slug,
 
               'subject_id'    => $this->input->post('subject_id'),
 
               'body'          => $this->input->post('body'),
 
               'is_published'  => $this->input->post('is_published'),
 
               'is_featured'   => $this->input->post('is_featured'),
 
               'in_menu'       => $this->input->post('in_menu'),
 
               'user_id'       => $this->session->userdata('user_id'),
 
               'order'       => $this->input->post('order')
 
           ); 

 
           // Update Page
 
           $this->Page_model->update($id$data);

 
           // Activity Array
            
$data = array(
                
'resource_id'   => $this->db->insert_id(),
                
'type'            => 'page',
                
'action'        => 'updated',
                
'user_id'        => $this->session->userdata('user_id'),
                
'message'        => 'A page was updated ('.$data["title"].')'
            
);

            
// Insert Activity
            
$this->Activity_model->add($data);

            
// Set Message
            
$this->session->set_flashdata('success''Page has been updated');

            
// Redirect
            
redirect('admin/pages');

        }
    }

    public function 
delete($id){
        
$title $this->Page_model->get($id)->title;

        
// Delete Page
        
$this->Page_model->delete($id);

        
// Activity Array
            
$data = array(
                
'resource_id'   => $this->db->insert_id(),
                
'type'            => 'page',
                
'action'        => 'deleted',
                
'user_id'        => $this->session->userdata('user_id'),
                
'message'        => 'A page was deleted'
            
);

            
// Insert Activity
            
$this->Activity_model->add($data);

            
// Set Message
            
$this->session->set_flashdata('success''Page has been deleted');

            
// Redirect
            
redirect('admin/pages');
    }


the problem comes from the controller users_controller. I already created an account with some data and that data should at least allow me to have access to the admin_controller which it does not, instead it redirects me to the admin/login form.

I would like to say that for some reason when I tried to add a page, I get an error message saying that user_id cannot be null, but as I'm "supposed" to be logged in that error should not appear. Any knows how to fix it?

erro message:
Quote:Error Number: 1048
Column 'user_id' cannot be null
INSERT INTO `pages` (`title`, `slug`, `subject_id`, `body`, `is_published`, `is_featured`, `in_menu`, `user_id`, `order`) VALUES ('Page One', 'page-one', '1', '
thrhjtyjrjrj
', '1', '0', '1', NULL, '1')

Filename: C:/xampp/htdocs/codeigniter/application/models/page_model.php
Line Number: 20

Users_controller:
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Users extends CI_Controller {
    function 
__construct(){
        
parent::__construct();
    }

    public function 
index(){
 
       // Check Login
 
       if(!$this->session->userdata('logged_in')){
 
           redirect('admin/login');
 
       }

        
$data['users'] = $this->User_model->get_list();
        
// Load template
        
$this->template->load('admin''default''users/index'$data);
    }

    public function 
add(){
 
       // Check Login
 
       if(!$this->session->userdata('logged_in')){
 
           redirect('admin/login');
 
       }

        
$this->form_validation->set_rules('first_name','First Name','trim|required|min_length[2]');
 
       $this->form_validation->set_rules('last_name','Last Name','trim|required|min_length[2]');
 
       $this->form_validation->set_rules('username','Username','trim|required|min_length[4]');
 
       $this->form_validation->set_rules('email','Email','trim|required|min_length[7]|valid_email');
 
       $this->form_validation->set_rules('password','Password','trim|required|min_length[4]|matches[password2]');
 
       $this->form_validation->set_rules('password2','Confirm Password','trim|required|min_length[6]|matches[password2]');

 
       if ($this->form_validation->run() == FALSE){
            
// Load View Into Template
 
           $this->template->load('admin','default','users/add');
        } else {
            
// Create Page Data Array
 
           $data = array(
 
               'first_name'    => $this->input->post('first_name'),
 
               'last_name'     => $this->input->post('last_name'),
 
               'email'         => $this->input->post('email'),
 
               'username'      => $this->input->post('username'),
 
               'password'      => md5($this->input->post('password'))
 
           );   

            
// Add User
 
           $this->User_model->add($data);

 
           //Activity Array
 
           $data = array(
 
               'resource_id'   =>  $this->db->insert_id(),
 
               'type'          => 'user',
 
               'action'        => 'added',
 
               'user_id'       => $this->session->userdata('user_id'),
 
               'message'       => 'A new user was added ('.$data["username"].')'
 
           ); 

 
           // Add Activity  
 
           $this->Activity_model->add($data);

 
           // Create Message
 
           $this->session->set_flashdata('success''User has been added');
 
           
            
// Redirect to pages
 
           redirect('admin/users');
        }
    }

    public function 
edit($id){
 
       // Check Login
 
       if(!$this->session->userdata('logged_in')){
 
           redirect('admin/login');
 
       }

        
$this->form_validation->set_rules('first_name','First Name','trim|required|min_length[2]');
 
       $this->form_validation->set_rules('last_name','Last Name','trim|required|min_length[2]');
 
       $this->form_validation->set_rules('username','Username','trim|required|min_length[4]');
 
       $this->form_validation->set_rules('email','Email','trim|required|min_length[7]|valid_email');

 
       if ($this->form_validation->run() == FALSE){
 
           // Get Current Subject
 
           $data['item'] = $this->User_model->get($id);
 
           //Load View Into Template
 
           $this->template->load('admin','default','users/edit'$data);
 
       } else {
 
           // Create User Data Array
 
           $data = array(
 
               'first_name'    => $this->input->post('first_name'),
 
               'last_name'     => $this->input->post('last_name'),
 
               'email'         => $this->input->post('email'),
 
               'username'      => $this->input->post('username')
 
           );   

            
// Update User
 
           $this->User_model->update($id$data);

 
           // Activity Array
 
           $data = array(
 
               'resource_id'   =>  $this->db->insert_id(),
 
               'type'          => 'user',
 
               'action'        => 'updated',
 
               'user_id'       => $this->session->userdata('user_id'),
 
               'message'       => 'A user was updated ('.$data["username"].')'
 
           ); 

 
           // Add Activity  
 
           $this->Activity_model->add($data);

 
           //Create Message
 
           $this->session->set_flashdata('success''User has been updated');
 
           
            
//Redirect to Users
 
           redirect('admin/users');
 
       }
    }

    public function 
delete($id){
 
       // Check Login
 
       if(!$this->session->userdata('logged_in')){
 
           redirect('admin/login');
 
       }
 
       
        
// Get Username
 
       $username $this->User_model->get($id)->username;

 
       // Delete User
 
       $this->User_model->delete($id);

 
       // Activity Array
 
       $data = array(
 
           'resource_id'   =>  $this->db->insert_id(),
 
           'type'          => 'user',
 
           'action'        => 'deleted',
 
           'user_id'       => $this->session->userdata('user_id'),
 
           'message'       => 'A user was deleted'
 
       ); 

 
       // Add Activity  
 
       $this->Activity_model->add($data);
 
    
        
// Create Message
 
       $this->session->set_flashdata('success''User has been deleted');
 
       
        
// Redirect to Subjects
 
       redirect('admin/users');
    }

    public function 
login(){
 
       $this->form_validation->set_rules('username','Username','trim|required|min_length[4]');
 
       $this->form_validation->set_rules('password','Password','trim|required|min_length[4]');


 
       if ($this->form_validation->run() == FALSE){
 
           //Load View Into Template
 
           $this->template->load('admin','login','users/login');
 
       } else {
 
          // Get Post Data
 
           $username $this->input->post('username');
 
           $password $this->input->post('password');
 
           $enc_password md5($password);

 
           $user_id $this->User_model->login($username$enc_password);

 
           if($user_id){
 
               $user_data = array(
 
                   'user_id' => $user_id,
 
                   'username'  => $username,
 
                   'logged_in' => true
                
);

 
               // Set Session Data
 
               $this->session->set_userdata($user_data);

 
               // Create Message
 
               $this->session->set_flashdata('success''You are logged in');
 
           
                
// Redirect to pages
 
               redirect('admin');
 
           } else {
 
               // Create Error
 
               $this->session->set_flashdata('error''Invalid Login');
 
               
                
// Redirect to pages
 
               redirect('admin/users/login');
 
           }
 
       }
    }
    
        public function 
register(){

        
$this->form_validation->set_rules('first_name','First Name','trim|required|min_length[2]');
 
       $this->form_validation->set_rules('last_name','Last Name','trim|required|min_length[2]');
 
       $this->form_validation->set_rules('username','Username','trim|required|min_length[4]');
 
       $this->form_validation->set_rules('email','Email','trim|required|min_length[7]|valid_email');
 
       $this->form_validation->set_rules('password','Password','trim|required|min_length[4]|matches[password2]');
 
       $this->form_validation->set_rules('password2','Confirm Password','trim|required|min_length[6]|matches[password2]');

 
       if ($this->form_validation->run() == FALSE){
            
// Load View Into Template
 
           $this->template->load('admin','login','users/register');
        } else {
            
// Create Page Data Array
 
           $data = array(
 
               'first_name'    => $this->input->post('first_name'),
 
               'last_name'     => $this->input->post('last_name'),
 
               'email'         => $this->input->post('email'),
 
               'username'      => $this->input->post('username'),
 
               'password'      => md5($this->input->post('password'))
 
           );   

            
// Add User
 
           $this->User_model->add($data);

 
           //Activity Array
 
           $data = array(
 
               'resource_id'   =>  $this->db->insert_id(),
 
               'type'          => 'user',
 
               'action'        => 'registered',
 
               'user_id'       => $this->session->userdata('username'),
 
               'message'       => 'A new user was registered ('.$data["username"].')'
 
           ); 

 
           // Add Activity  
 
           $this->Activity_model->add($data);

 
           // Create Message
 
           $this->session->set_flashdata('success''User has been registered');
 
           
            
// Redirect to pages
 
           redirect('admin/users/login');
        }
    }

    public function 
logout(){
        
$this->session->unset_userdata('logged_in');
 
       $this->session->unset_userdata('user_id');
 
       $this->session->unset_userdata('username');
 
       $this->session->sess_destroy();

 
       // Message
 
       $this->session->set_flashdata('success''You are logged out');
 
       redirect(base_url());
    }


Viewing all articles
Browse latest Browse all 14343

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>