i am trying to learn codeigniter, and i start with basic login function. Everything works well, until i want to redirect member to their homepage.
i seems like codeigniter can not redirect to it. It just always stops at this url:
I want this redirect to the site/members_area like mycode. This is my controller code:
class login extends ci_controller{
function index(){
$data['main_content'] = 'login_form';
$this->load->view('includes/template.php', $data);
}
function validate_credentials(){
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if($query)//user's credentails validated
{
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true);
$this->session->set_userdata($data);
redirect('site/members_area');
}
else
{
$this->index();
}
}
}
And this is my model code:
class Membership_model extends ci_model{
function validate(){
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('membership');
if($query->num_rows == 1){
return true;
}
}
}
I already input helper url and form in my autoload, but it still does not work.
Can someone help?
num_rows is a function and should have parentheses like this: $query->num_rows()
You can eliminate the if and simply use
return $query->num_rows() > 0;
Related
I have created a login page using codeigniter framework
inside the models folder I created
login_model.php
class Login_model extends CI_Model
{ //CI version 3.0
function validate_login()
{
$this->db->where('username', $this->input->post('username'));//this is form username
$this->db->where('password', md5($this->input->post('password')));//this is form password
$query = $this->db->get('membership'); //database autoloaded
if($query->num_rows == 1)
{
return true;
}
}
}
I think here everything fine after this
inside the controllers folder I created
login_control.php
class Login_control extends CI_Controller
{
function index()
{
$data['main_content'] = 'login_view';
$this->load->view('includes/template', $data);
}
function validate_credential() {
$this->load->model('login_model');
$query = $this->login_model->validate_login();
//redirect('logged_site/members_area');
if($query)//if user credential validate!. run following events
{
$newdata = array(
'username' => $this->input->post('username'),
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
redirect('logged_site/members_area');
}
else
{
$this->index();
}
}
}
here If I give directly redirect to members_area page it is working as follows
redirect('logged_site/members_area');
after comment above line I used if/else condition to validate query but after this condition coding not redirecting to specified page and without any error just staying in $this->index(); page which is available in else part.
Please help me where I made mistake?
Error on your model validate_login()
Change:
if($query->num_rows == 1)
To:
if($query->num_rows() == 1)
do like this
In Controller
if($query == true) # check true
{
$newdata = array(
'username' => $this->input->post('username'),
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
redirect('logged_site/members_area');
}
else
{
$this->index();
}
In Model
class Login_model extends CI_Model
{
function validate_login()
{
$this->db->where('username', $this->input->post('username'));//this is form username
$this->db->where('password', md5($this->input->post('password')));//this is form password
$query = $this->db->get('membership'); //database autoloaded
$result = $query->result_array();
$count = count($result) # add
if($count == 1) #changed
{
return true;
}
else
{
return false;
}
}
}
How do I output the username and database value "access" in the view?
It should be on the main page display user name and id of the band, how to implement? The database has a table "Access" at the base of users, you need to get the value and just bring it. Help
Here is my controller and model:
Model:
Class User extends CI_Model
{
function login($username, $password)
{
$this->db->select('id', 'username', 'password', 'access');
$this->db->from('users');
$this->db->where('username', $username);
$this->db->where('password', MD5($password));
$this->db->limit(1);
$query = $this->db->get();
if($query->num_rows() == 1)
{
return $query->result();
}
else
{
return false;
}
}
}
Controller:
class Home extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('Layout');
}
function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->layout->content('home_view', $data);
}
else
{
redirect('login', 'refresh');
}
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('home', 'refresh');
}
}
The problem is that Users should be a Model and Home should be a Controller.
when you get the user name from the table, make a $this->session->set_userdata('username', $username) where $username is the query result.
There is another problem. CI does not use $_SESSION so session_destroy() will not work, use
$this->session->sess_destroy() instead.
i am new in CodeIgniter (v.2.1.4). I have a problem with getting post from model.
I have a contoller:
class Login extends CI_Controller{
public function index(){
$data['main_view'] = 'login_form';
$this->load->view('login/include/template', $data);
}
public function validate(){
$this->load->library('form_validation');
$this->form_validation->set_rules('user', 'Име', 'trim|required|min_length[3]');
$this->form_validation->set_rules('pass', 'Парола', 'trim|required|min_length[4]');
if($this->form_validation->run()){
$this->load->model('users_model');
if($this->users_model->validate_login($username, $password)){
//valid user
}
else{
//not vlaid user
}
}
else{
$this->index();
}
}
}
and a model:
class Users_model extends CI_Model{
function __construct() {
parent::__construct();
}
function validate_login(){
$username = $this->input->post('user');
$password = md5($this->input->post('pass'));
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get('users');
if($query->num_rows() == 1){
return TRUE;
}
else return FALSE;
}
}
When send via form (post) valid pair (username and password) nothing happens, but in apache2 log appears this:
PHP Fatal error: Call to a member function where() on a non-object in /var/www/ci/application/models/users_model.php
What is wrong?
Add a constructor to the Login class
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
}
}
Do you have database loaded? You need to load the database first.
You can either add it to /config/autoload.php to autoload database function:
$autoload['libraries'] = array('database');
Or call on demand like this:
$this->load->database();
More details here
Do Not Use post in model instead use this in controller like this
controller login.php
public function validate(){
$this->load->library('form_validation');
$this->form_validation->set_rules('user', 'Име', 'trim|required|min_length[3]');
$this->form_validation->set_rules('pass', 'Парола', 'trim|required|min_length[4]');
if($this->form_validation->run()){
$this->load->model('users_model');
$post = $this->input->post();
$data['username'] = $post['username'];
$data['password'] = md5($post['password']);
if($this->users_model->validate_login($data)){
//valid user
}else{
//not vlaid user
}
}
else{
$this->index();
}
}
Model
function validate_login($where){
$this->db->where($where);
$query = $this->db->get('users');
if($query->num_rows() == 1){
return TRUE;
}
return FALSE;
}
This should work. Remember calling post somehow doesn't work in model. I dont exactly know they reason.
new to stack overflow (and codeigniter!) and I'm running into this issue on my first project, naturally... I'm sure its a bonehead error on my part, but I've been researching and trying to figure it out for the last few hours and I cat seem to figure it out...
I have the database library autoloaded, but everytime I try to use the methods in my model I get this error:
"Fatal error: Call to a member function where() on a non-object in application\models\user_model.php on line 13"
this is my User controller method called by the form action from my login form view:
function login() {
$this->load->model('user_model');
$query = $this->user_model->validate();
if ($query)
{
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('members');
}
else
{
$this->load->view('user');
}
} // end login function
this is my model:
<?php
class User_model extends CI_Model {
function __construct() {
parent::__construct();
}
// -------------------------------------------------------------------------------
// validate(): query db for user/pass and return true or false
function validate() {
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', sha1($this->input->post('password')));
$query = $this->db->get('users');
if ($query->num_rows() > 0)
{
return true;
}
} // end validate function
// -------------------------------------------------------------------------------
function check()
{
$dbo = $this->load->database('default',TRUE);
$query = $this->$dbo->get('users');
echo $query->num_rows();
}
} // end user_model class
when I was attempting to debug the issue, I added the db methods to the controller and they worked fine... when using them in my model they dont want to work for whatever reason and throw the fatal error I mentioned. Appreciate any help you guys can throw my way.
Thanks in advance!
(using codeigniter 2.1)
EDIT:
Just to clear things up a little bit more, When I add the validate method (from my model) to my controller, I get no errors, and everything works fine... But when leaving it in the model and calling it from the controller I get the fatal error
You missed $query = part, Should be:
$query = $this->db->get('users');
if ($query->num_rows() > 0)
{
return true;
}
Edited:
I think you have not added all the parent constructor, try:
class Users extends CI_Model {
function __Users() {
parent::__CI_Model();
$this->load->database();
}
I think the function validate in your model should be like this
// validate(): query db for user/pass and return true or false
function validate($username,$password) {
$this->db->where('username', $username);
$this->db->where('password', sha1($password));
$query = $this->db->get('users');
if ($query->num_rows() > 0)
{
return true;
}
} // end validate function
In your controller you can call it like so
function login()
{
$query =$this->user_model->validate($this->input->post('username'),$this->input->post('password'));
// the rest
}
Here is my controller
public function login() {
$this->load->model('admin_model');
$access = $this->admin_model->check_login();
$data['content'] = 'content/home';
$data['title'] = 'Admin Panel Login';
if($access) {
$data= array(
'email' => $this->input->post('email'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('admin/index/dashboard');
} else {
$this->index($page = 'home', $msg = 'failure');
}
Here is my model
public function check_login() {
$this->db->where('email', $this->input->post('email'));
$this->db->where('password', $this->input->post('password'));
$query = $this->db->get('customers');
return (bool) $query->num_rows;
}
So what I have been working on is a function in my Admin constructor that will ensure that users cannot access any page until they are logged in. The problem is that when I go to admin/login, it is setting is_logged_in to true, even though the check_login function should be returning as false. It's saying that it is finding a row, even though there is only 1 row in my DB right now which it shouldn't be matching.
I'm new to CI, so please be gentle. :)
num_rows() is a method (not a property) and should be used more like this:
return $query->num_rows() > 0;
An expansion on Colin's answer:
public function check_login() {
$this->db->where('email', $this->input->post('email'));
$this->db->where('password', $this->input->post('password'));
$query = $this->db->get('customers');
if ($query->num_rows()>0) {
return TRUE;
} else {
return FALSE;
}
}
I didn't see the point in your model.
A model can't (or even if it can, shouldn't) get the variables from the view. It should come via controller.
Are you sure, the post variables are appearing in the model?
Better solution. Catch the post variables in controller.
In the model, create functions with parameters.
Then in the controller, retrieve it like this.
$data['bla'] = $this->admin_model->method_name($this->input->post('blabla'));
this will prevent all these confusions.