When I login success then redirect to the home page.
After I set session in login and view page, when I refresh that same page then it is redirect in to the login page, whats the reason for that and would you please provide solution for this problem?
Controller
public function login() {
$email = $this->input->post('email');
$password = md5($this->input->post('password'));
$result = $this->search_model->login($email, $password);
if($result !='') {
$this->session->set_userdata('user_id', $email);
$seid = $this->session->userdata('user_id');
}
if ($seid=='') {
//echo ($seid);
$this->index();
} else {
$this->view();
}
}
public function view() {
$seid = $this->session->userdata('user_id');
$data['result']=$this->search_model->getall($seid);
$this->load->view('pagination_view',$data);
}
Model
function login($email, $password) {
$this->db->where("email", $email);
$this->db->where("password", $password);
$query=$this->db->get("tbl_reg");
return $query->result_array();
}
View
<?php
if(!$this->session->userdata('user_id'))
{
redirect(base_url().'search/login');
}
else
{
}
?>
Two problems, the first architectural the second likely causing your problem. The first is you should not be redirecting in a view. This should be in the controller that loads your home page (or other pages). You shouldn't even get to the view if you don't belong there.
The second problem is if($result!=''). This won't work. $result is an array. Use if(!empty($result)) or if(count($result)!==0) or something similar.
On your pagination_view I would not use redirect instead place that on controller function
public function view() {
if ($this->session->userdata('user_id') == FALSE) {
redirect(base_url().'search/login');
} else {
$data['result'] = $this->search_model->getall($seid);
$this->load->view('pagination_view',$data);
}
}
Related
Here is the controller, when I click the login button, nothing happens. What I want is to load the success screen when user data is validated and show error messages when user data is not validated.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
function __construct(){
parent::__construct();
}
public function index() {
$this->load->view('login');
}
function getlogin() {
$user = $this->input->post('username');
$pass = $this->input->post('password');
$user = $this->auth->log_admin($user,$pass);
if($user==true){
$this->session->set_userdata($user);
$data['hasil']=1;
echo json_encode($data);
}else{
$data['hasil'] = 0;
echo json_encode($data);
}
}
function logout(){
//helper_log("logout", "Logout");
$this->session->sess_destroy();
redirect('Login','refresh');
}
}
Here the log_admin
function log_admin($user,$pass)
{
$cek=$this->CI->db->where('admin_user',$user)
->where('admin_password',md5($pass))
->get('admin')
->row();
$row=count($cek);
if ($row==1) {
return $cek;
} else {
return false;
}
}
you can redirect the page from the controller, something like this.
after validating the user, it will redirect to your desired page.
function getlogin() {
$user = $this->input->post('username');
$pass = $this->input->post('password');
$user = $this->auth->log_admin($user,$pass);
if($user==true){
$this->session->set_userdata($user);
redirect('go to the next page');
}else{
$data['hasil'] = 0;
echo json_encode($data);
}
}
I've created a PHP desktop application, but after login, I want to redirect to the controller, but I'm getting error:
redirect('dashboard');
Loading error (-102).
and when I like something this:
redirect('https://www.google.com/');
it works.
How can I redirect to a controller?
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('LoginModel','LoginModel');
}
public function index()
{
if(!empty($_POST))
{
$email = $this->input->post('email');
$password = $this->input->post('password');
$result = $this->LoginModel->login($email,$password);
if($result -> num_rows() > 0)
{
foreach ($result->result() as $row)
{
$this->session->userid = $row->id;
$this->session->email= $row->email;
$this->session->is_admin = $row->is_admin;
**redirect('dashboard');**
}
}
else
{
$data['email'] = $email;
$data['password'] = $password;
$this->session->set_flashdata('SUCCESSMSG','Email and Password is Wrong');
$this->load->view('login',$data);
}
}
else
{
$this->load->view('login');
}
}
public function logout()
{
$this->session->sess_destroy();
redirect('login');
}
}
I think you need to use redirect to relative path; try something like this:
redirect('/controller_name/Dashboard');
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 already registered session with my code and preventing the the profile direct access with following code.
public function profile() {
if ($this->session->userdata('logged_in')) {
//var_dump($this->session->userdata['logged_in']);
$session_data = $this->session->userdata('logged_in');
$data['nric_number'] = $session_data['nric_number'];
$this->load->view('templates/header');
$this->load->view('layouts/employee/profile', $data);
$this->load->view('templates/footer');
} else {
//If no session, redirect to login page
$base = base_url();
redirect($base . 'checkinout', 'refresh');
}
}
Suppose, I am going prevent another controller named allusers. So, Script will be something like below-
public function allusers() {
if ($this->session->userdata('logged_in')) {
$this->load->view('layouts/employee/allusers');
} else {
//If no session, redirect to login page
$base = base_url();
redirect($base . 'checkinout', 'refresh');
}
}
But, I would not like to use following condition for each method. Can I skip actually?
if($this->session->userdata('logged_in')) {
} else {
}
Just put the code in your constructor of class, in this way you dont need to check for the session in all methods!
if(!($this->session->userdata('logged_in'))) {
$allowed = array(
'method1',
'method2'
);
if ( ! in_array($this->router->fetch_method(), $allowed) {
redirect('login');
}
}
If a user does not exist its supposed to throw back an error on the login screen but the user is then brought to the home page as if they have previously registered in my sign controller. The login controller - validate credentials function - and my model - function can_log_in() - will illustrate the error its supposed to bounce back
model:
class Membership extends CI_Model
{
function Membership()
{
parent::__construct();
}
public function can_log_in()
{
$this->db->select('*')->from('membership');
$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;
}
else{
return false;
}
}
login controller:
class Login extends CI_Controller
{
function Login()
{
parent::__construct();
$this->load->model('membership');
}
function loguserin()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|trim');
$this->form_validation->set_rules('password', 'Password', 'required|md5|trim');
$username = $this->input->post('username');
$password = $this->input->post('password');
if ($this->form_validation->run()==TRUE)
{
$this->load->model('membership');
if($this->membership->can_log_in($this->input->post('username'),$this->input->post('password'))
{ // this is unepected despite a new if statement called
$this->session->set_userdata('status', 'OK');
$this->session->set_userdata('username', $username);
redirect('home');
} else //this else is unexpected
{
$this->session->set_userdata('status', 'NOT_OK');
$this->session->set_flashdata('Incorrect username/password');
$this->index();
}
} else {
$this->index;
}
}
function logout()
{
$this->session->sess_destroy();
redirect ('start');
}
function index()
{
$this->load->view('shared/header');
$this->load->view('account/logintitle');
$this->load->view('account/loginview');
$this->load->view('shared/footer');
}
}
Once again thanks :) its just that the user if not already in the table should not be able to sign into the home page
Your logic is all over the place and you're misunderstanding the concept of form validation. Form validation validates form input that's it, it does not validate credentials. The validate_credentials function you have is sort of pointless to be honest since you can't call the form validation error from outside the run function. So you may as well just make that go away entirely.
First, the initial call after form_validation should be like this:
if ($this->form_validation->run()==TRUE)
{
$this->load->model('membership');
if($this->membership->can_log_in($this->input->post('username'),$this->input->post('password')))
{
$this->session->set_userdata('status', 'OK');
$this->session->set_userdata('username', $username);
redirect('home');
} else {
$this->session->set_userdata('status', 'NOT_OK');// you'll need to do something
// here to tell them their credentials are wrong as I said this won't work through
// the form validation, perhaps look into using $this->session->set_flashdata()
$this->index();
}
} else {
$this->index; //reload the index to display the validation errors
Okay that takes care of sending the data to the login function, now the function actually needs to receive data, and since it is NOT the first page sent the data the post string will be empty, that is why I passed the data in the function above. The only part of the model that should need to change is the following:
public function can_log_in($username,$password)
{
$this->db->select('*')->from('membership');
$this->db->where('username', $username);
$this->db->where('password', md5($password));
try replacing
if ($this->form_validation->run())
with
if ($this->form_validation->run() && validate_credentials())