Welcome Guest, Not a member yet? Register   Sign In
Login is not working
#1

Hello,
I have a login installed.
The bulk works.
But when I go to account.php, nothing is displayed there.
I hope you can continue helping me there.

Controller:
PHP Code:
<?php
    
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class 
Users extends CI_Controller {
        function 
__construct() {
            
parent::__construct();
        }
        public function 
account(){
            
$data = array();
            if(
$this->session->userdata('isUserLoggedIn')){
                
$data['user'] = $this->User_model->getRows(array('id'=>$this->session->userdata('userId')));
                
//load the view
                
$this->load->view('templates/header');
                
$this->load->view('users/account'$data);
                
$this->load->view('templates/footer');
            }else{
                
redirect('users/login');
            }
        }

        public function 
login(){
            
$data = array();
            if(
$this->session->userdata('success_msg')){
                
$data['success_msg'] = $this->session->userdata('success_msg');
                
$this->session->unset_userdata('success_msg');
            }
            if(
$this->session->userdata('error_msg')){
                
$data['error_msg'] = $this->session->userdata('error_msg');
                
$this->session->unset_userdata('error_msg');
            }
            if(
$this->input->post('loginSubmit')){
                
$this->form_validation->set_rules('email''Email''required|valid_email');
                
$this->form_validation->set_rules('password''password''required');
                if (
$this->form_validation->run() == true) {
                    
$con['returnType'] = 'single';
                    
$con['conditions'] = array(
                        
'user_email'=>$this->input->post('email'),
                        
'user_password' => md5($this->input->post('password')),
                        
// 'status' => '1'
                    
);
                    
$checkLogin $this->User_model->getRows($con);
                    if(
$checkLogin){
                        
$this->session->set_userdata('isUserLoggedIn',TRUE);
                        
$this->session->set_userdata('userId',$checkLogin['id']);
                        
redirect('users/account/');
                    }else{
                        
$data['error_msg'] = 'Wrong email or password, please try again.';
                    }
                }
            }
            
$this->load->view('templates/header');
            
$this->load->view('users/login'$data);
            
$this->load->view('templates/footer');
        }
        public function 
registration(){
            
$data = array();
            
$userData = array();
            if(
$this->input->post('regisSubmit')){
                
$this->form_validation->set_rules('name''Name''required');
                
$this->form_validation->set_rules('email''Email''required|valid_email|callback_email_check');
                
$this->form_validation->set_rules('password''password''required');
                
$this->form_validation->set_rules('conf_password''confirm password''required|matches[password]');
                
$userData = array(
                    
'user_name' => strip_tags($this->input->post('name')),
                    
'user_email' => strip_tags($this->input->post('email')),
                    
'user_password' => md5($this->input->post('password')),
                );
                if(
$this->form_validation->run() == true){
                    
$insert $this->User_model->insert($userData);
                    if(
$insert){
                        
$this->session->set_userdata('success_msg''Your registration was successfully. Please login to your account.');
                        
redirect('users/registration');
                    }else{
                        
$data['error_msg'] = 'Some problems occured, please try again.';
                    }
                }
            }
            
$data['user'] = $userData;
            
$this->load->view('templates/header');
            
$this->load->view('users/registration'$data);
            
$this->load->view('templates/footer');
        }
        public function 
email_check($str){
            
$con['returnType'] = 'count';
            
$con['conditions'] = array('user_email'=>$str);
            
$checkEmail $this->User_model->getRows($con);
            if(
$checkEmail 0){
                
$this->form_validation->set_message('email_check''The given email already exists.');
                return 
FALSE;
            } else {
                return 
TRUE;
            }
        }
        public function 
logout(){
            
$this->session->unset_userdata('isUserLoggedIn');
            
$this->session->unset_userdata('userId');
            
$this->session->sess_destroy();
            
redirect('users/login/');
        }
    } 


Model:
PHP Code:
<?php
    
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class 
User_model extends CI_Model {
        function 
__construct() {
            
$this->userTbl 'user';
        }
        function 
getRows($params = array()) {
            
$this->db->select('*');
            
$this->db->from($this->userTbl);
            
            
//fetch data by conditions
            
if(array_key_exists("conditions",$params)){
                foreach (
$params['conditions'] as $key => $value) {
                    
$this->db->where($key,$value);
                }
            }
            
            if(
array_key_exists("id",$params)){
                
$this->db->where('user_id',$params['id']);
                
$query $this->db->get();
                
$result $query->row_array();
            }else{
                
//set start and limit
                
if(array_key_exists("start",$params) && array_key_exists("limit",$params)){
                    
$this->db->limit($params['limit'],$params['start']);
                }elseif(!
array_key_exists("start",$params) && array_key_exists("limit",$params)){
                    
$this->db->limit($params['limit']);
                }
                
$query $this->db->get();
                if(
array_key_exists("returnType",$params) && $params['returnType'] == 'count'){
                    
$result $query->num_rows();
                }elseif(
array_key_exists("returnType",$params) && $params['returnType'] == 'single'){
                    
$result = ($query->num_rows() > 0)?$query->row_array():FALSE;
                }else{
                    
$result = ($query->num_rows() > 0)?$query->result_array():FALSE;
                }
            }
            return 
$result;
        }
        
        
/*
         * Insert user information
         */
        
public function insert($data = array()) {
            
//add created and modified data if not included
            
if(!array_key_exists("created"$data)){
                
$data['user_datetime'] = date("Y-m-d H:i:s");
            }
            
// if(!array_key_exists("modified", $data)){
                // $data['modified'] = date("Y-m-d H:i:s");
            // }
            
            //insert user data to users table
            
$insert $this->db->insert($this->userTbl$data);
            
            
//return the status
            
if($insert){
                return 
$this->db->insert_id();;
            }else{
                return 
false;
            }
        }

    } 


View
Code:
<!DOCTYPE html>
<html lang="en">  
<head>
<link href="<?php echo base_url(); ?>assets/css/style.css" rel='stylesheet' type='text/css' />
</head>
<body>
<div class="container">
   <h2>User Account</h2>
   <h3>Welcome <?php echo $user['user_name']; ?>!</h3>
   <div class="account-info">
       <p><b>Name: </b><?php echo $user['user_name']; ?></p>
       <p><b>Email: </b><?php echo $user['user_email']; ?></p>
   </div>
</div>
</body>
</html>


Datenbank:
Quote:user:
user_id
user_name
user_password
user_email
user_datetime
Reply
#2

Check your Session data first.

PHP Code:
$data['user'] = $this->User_model->getRows(array('id'=>$this->session->userdata('userId')));

var_dump($data);
exit();

// Also try this
var_dump($this->session->userdata());
exit(); 

Do that before you load you view.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 05-01-2018, 10:53 AM by Germanikus.)

Ok habe ich und bekomme das raus.

array(1) { ["user"]=> array(1) { [0]=> array(5) { ["user_id"]=> string(1) "3" ["user_name"]=> string(7) "Lubomir" ["user_password"]=> string(32) "1adbb3178591fd5bb0c248518f39bf6d" ["user_email"]=> string(13) "[email protected]" ["user_datetime"]=> string(19) "2018-05-01 18:37:57" } } }
Reply




Theme © iAndrew 2016 - Forum software by © MyBB