Welcome Guest, Not a member yet? Register   Sign In
Not Connecting to Database
#1

Hello!

Im getting invalid username/password when login and I believe the reason is because login/register not connecting to database. Here is code within controllers:

1.
<?php

class Form extends CI_Controller {

        public function index()
        {
               $this->load->helper(array('form', 'url'));

                $this->load->library('form_validation');

                $this->load->database('citable');
                
               $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[50]');
                $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[2]');
                $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required|matches[password]');
                $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');

                if ($this->form_validation->run() == FALSE)
                {
                        $this->load->view('register_view');
                }
                else
                {
                        $this->load->view('login_view');
                }
        }
}





2.
<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller {

    public function __construct() {
        parent::__construct();
        
        //load the required libraries and helpers for login
        $this->load->helper('url');
        $this->load->library(['form_validation','session']);
        $this->load->database(citable);
        
        //load the Login Model
        $this->load->model('LoginModel', 'login');
    }

    public function index() {
        //check if the user is already logged in
        $logged_in = $this->session->userdata('logged_in');
        if($logged_in){
            //if yes redirect to welcome page
            redirect(base_url().'welcome');
        }
        //if not load the login page
        $this->load->view('header');
        $this->load->view('login_page');
        $this->load->view('footer');
    }

  public function loginUser() {

        $this->form_validation->set_rules('username','Username','required');
        $this->form_validation->set_rules('password','Password','required');

        if ($this->form_validation->run() == FALSE) {

            $this->load->view('login_view');

        } else {

            $this->load->model('Model_user');
            $reslt = $this->Model_user->checkLogin();

            if ($reslt != false) {

                //set session
                $username = $_POST['username'];
                $password = sha1($_POST['password']);

                //fetch from databse
                $this->db->select('citable');
                $this->db->from('username');
                $this->db->where(array('username' => $username , 'password' => $password));
                $query = $this->db->get('citable')->result_array();

                $user = $query->row();

                //if use exists
                if ($user->username) {

                    //login message
                    $this->session->set_flashdata("success","You are logged in");

                    //set session variables
                    $_SESSION['user_logged'] = TRUE;
                    $_SESSION['username'] = $user->username;

                    //redirect
                    redirect('user/profile','refresh');

                }


            } else {

                //wrong credentials
                $this->session->set_flashdata('error','Username or Password invalid!');
                redirect('Home/Login');

            }
        }

    }
    //logging out of a user
    public function logoutUser() {
        unset($_SESSION);
        redirect('Home/Login');
    }
}

Im slowly getting to goal but keeping getting minor snags. Can anyone help?

Heart Heart ,
Mekaboo
Reply
#2

@Mekaboo,

Is your database configurations correct in the database config file?
Reply
#3

(05-14-2019, 06:34 PM)php_rocs Wrote: @Mekaboo,

Is your database configurations correct in the database config file?

Yep its correct, I contacted webhost to make sure. I chose test some different code for login and now I get this error. Ive gotten this before:

A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /home4/cultured/public_html/application/controllers/Login.php:25)
Filename: core/Common.php
Line Number: 564
Backtrace:
A PHP Error was encountered
Severity: Error
Message: Call to a member function row() on a non-object
Filename: controllers/Login.php
Line Number: 25
Backtrace:

However I got it for this line of code:
$user = $this->db->get_where('user',['email' => $email])->row();

My rows within my DB are:username, email, password.

Trying to understand what I need to change in this line to make it work properly. Thank you for the help Blush
Reply
#4

The error like I mentioned before is because your trying to output data to the
view but it has already been outputted to the view.

Look at your code and see where your trying to output your data to the view.
What did you Try? What did you Get? What did you Expect?

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

@Mekaboo,

What have you done in your trouble shooting to help you find the issue? Do you use Xdebug? Have you tried commenting out certain statements?
Reply
#6

Hey folks!

Looking at an old post from here I figured the issue within this code

$user = $this->db->get_where('user',['email' => $email])->row();

I didnt change the word 'user' to my table name in my DB. Now its working decent. I need to now connect my register page to to my DB. I think Ill just copy code from one controller to another.

Thanks for the help, it means alot!!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB