Error after redirection using CakePHP - php

I have created some code called LoginController. Whenever Admin gets successfully logged in I redirect the page to index.
However, I got an error like "problem on loading page".
This is my code:
<?php
class LoginController extends AdminAppController {
var $name = 'Login';
var $uses = array('Admin.Login');
var $sessionkey= '';
/*function beforeFilter()
{
if($this->Session->read('user')=='Admin' || $this->params['action']=='login')
{
echo "in"; exit;
}
else
{
echo "else"; exit;
$this->Session->setFlash('Login first','flash_failure');
$this->redirect(array('action'=>'login'));
}
}*/
function index() {
}
function login()
{
//pr($this->data); exit;
if(!empty($this->data))
{
$results = $this->Login->findByEmail($this->data['Login']['email']);
if(!empty($results) && $results['Login']['password']== md5($this->data['Login']['password']))
{
$this->Session->write('user', 'Admin');
$results['Login']['last_login']=date("Y-m-d H:i:s");
$this->Login->save($results);
$this->Session->setFlash('Login successfully.', 'flash_success');
$this->redirect(array('controller'=>'login','action' => 'index'));
}
}
}
}
?>
Can anyone help me? Thanks.

I got your problem.
IT goes into the infinite loop.ANd that is why you got in the Page loading error.
As you did not distinguish between admin login and client login you are facing this issue.
So my suggestion is that create new same function with diff name called as admin login
And set the router for admin login.
regards,
ARCHIT.

Related

Unable to load the requested file: validation.php

I am new to developing, and I want to do a simple validation in Codeigniter. I don't know where I am going wrong.
This is my form to be validated
And my controller is this
public function __construct()
{
parent::__construct();
$this->load->model('M_menu');
$this->load->model('master/M_user_type');
$this->load->helper(array('form'));
$this->load->library('form_validation');
}
public function index()
{
$data['menus']=$this->M_menu->getSideBarMenu_m();$data['error_message'] = '';
$this->load->view('master/V_user_type',$data);
}
function saveUserType_c()
{
$data['error_message'] = '';
$this->form_validation->set_rules('userType', 'UserTypeName', 'required');
echo var_dump($this->form_validation->run());
if (!$this->form_validation->run() )
{$data['menus']=$this->M_menu->getSideBarMenu_m();
$data['error_message'] .= validation_errors();
$this->load->view('master/V_user_type',$data);
}
else
{
$insert=$this->M_user_type->saveUserType_m();
if($insert){
$response=array("insert"=>true);
}else{
$response=array("insert"=>false);
}
echo json_encode($response);
}
}
With this I get the network error, and data is being saved to db. But no action in form(form not loading). Please guide me, where I go wrong. Also, if I need to give any more details
Please check your usertype view page, you can also load usertype view page before validation, so that you will be confirmed that usertype view page exist, I hope you understand my point.
You have to load a model before you call its methods. Use this:
$this->load->model('M_user_type');
$insert=$this->M_user_type->saveUserType_m();

Redirect to Login View from Controller In Code-igniter Error occured

Hi I'm new to php and code igniter. what I tried is to get login info from view and validated user and need to send a message to user whether login details are incorrect.
Controller Code :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('login_model');
$this->load->helper('form');
$this->load->helper('url');
}
public function index()
{
//$this->load->helper('url');
$this->load->view('login/login');
}
public function login_check()
{
//$this->load->view('hello');
//echo "directed";
$user_id = $this->input->post('usernm');
$userPassword = $this->input->post('passwordd');
//echo $user_id.' and'.$userPassword;
$var = $this->login_model->check_login($user_id);
$status = 0;
if(empty($var))
{
echo "Invalid user";
$status = 0;
}
else
{
//echo var_dump($var);
$username = $var->username;
//echo $username;
$status = 1;
}
$this->load->helper('url');
redirect('login/login');
//$this->load_>view('login\login');// at this point it does not redirect to login page and instead of that displaying error 404.page not found.
//echo $status;
}
}
Same url ,earlier loaded when it called from from index function but fi it is calling from login check()function does not directed to the view and displaying error 404 page not found.
Any assistance regarding this world be a great help.
Thanks a Lot!
You need to write redirect('welcome'); instead of redirect('login/login');. Because login/login is the view page and you are trying to redirect direct on view without using controller. So you have two option which I had written bellow.
redirect('welcome');
$this->load->view('login/login');
My suggestion is please choose 1st option because 2nd option already you have implemented in first one.
I hope this one will work on you.
public function login_check()
{
$user_id = $this->input->post('usernm');
$userPassword = $this->input->post('passwordd');
$var = $this->login_model->check_login($user_id, $userPassword); //pass username and password to your model to authenticate if input exists.
$data['error'] = '';
if(!empty($var)) //check if var is not empty
{
$this->session->set_userdata($var); //set user_data to var
redirect('Account/page'); //redirect it to your account or success page
}else{
$data['error'] = 'Invalid Username or Password.';
}
$this->load->view('login/login',$data); //pass the error notification to your page.
}

how can we restrict after login go back to login page in codeigniter

I have two controllers
1.Login
2.Dashboard
In Login controller I have two methods
1.logged_in()
2.logged_out()
This is my Login Controller
public function logged_in()
{
$user_email =$this->input->post('user_email');
$user_password =$this->input->post('user_password');
$result=$this->Login_model->login_data($user_email,$user_password);
if(!$result)
{
$this->session->set_flashdata('failure', 'Login failed');
redirect(BASE_URL.'admin/Login');
}
else
{
$data=array(
'user_email'=>$result[0]['user_email'],
'user_password'=>$result[0]['user_password'],
);
$this->session->set_userdata('session_data',$data);
$this->session->set_flashdata('success', 'Login sucessfully');
redirect(BASE_URL.'admin/Dashboard');
}
}
public function logged_out()
{
$this->session->unset_userdata('session_data');
$this->session->sess_destroy();
$this->session->set_flashdata('success', 'Logout sucessfully');
redirect(BASE_URL.'admin/Login');
}
And this is Dashboard controller :
class Dashboard extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
if($this->session->userdata('session_data')!='')
{
$this->load->view('admin/dashboard');
}
else
{
$this->session->set_flashdata('admin_flash', 'Try again');
redirect(BASE_URL."admin/Login");
}
}
}
I have a problem while after login it enters in my view which I have to load but the problem is that if I go back it goes to login page which i don't want.
so suggest me solution?
My idea is to make something like a boolean called logged by default false and set it true when the user log into the page and false if you log out. Then check if the user is login in the login page and if he's login redirect the user to the homepage
simply check whether user logged in or not in your login page. If user is already logged in, redirect him/her to dashboard.
In your code:
public function logged_in()
{
// check whether user is logged in or not,
// if yes redirect them to dashboard
if($this->session->userdata('session_data')!='')
{
redirect(BASE_URL."admin/dashboard");
}
$user_email =$this->input->post('user_email');
$user_password =$this->input->post('user_password');
$result=$this->Login_model->login_data($user_email,$user_password);
if(!$result)
{
$this->session->set_flashdata('failure', 'Login failed');
redirect(BASE_URL.'admin/Login');
}
else
{
$data=array(
'user_email'=>$result[0]['user_email'],
'user_password'=>$result[0]['user_password'],
);
$this->session->set_userdata('session_data',$data);
$this->session->set_flashdata('success', 'Login sucessfully');
redirect(BASE_URL.'admin/Dashboard');
}
}

Confirm form submission error in codeigniter grocery crud

When i click on back button, i got the confirm form submission error .How to prevent this issue?Please provide solution for this.
I am trying to login to the admi panel and edit in admin in this following code
Admin controller:
public function index()
{
$data = $this->data;
$this->load->view('admin/login.php',$data);
}
public function login()
{
$data = $this->data;
$username=$this->input->post("username");
$password=$this->input->post("password");
if($username=='admin'&&$password=='admin123')
{
$this->load->view('admin/home.php',$data);
}
else
{
$data['error']="Invalid Username or Password";
$this->load->view('admin/login.php',$data);
}
}
/* Function For Displaying the home page*/
public function home()
{
$data = $this->data;
$this->load->view('admin/index.php',$data);
}
/* Common Function for calling the View*/
public function _admin_output($output = null)
{
$output->base=$this->config->item('base_url');
$output->site=$this->config->item('site_url');
$output->css=$this->config->item('css');
$output->js=$this->config->item('js');
$this->load->view('admin/home_page.php',$output,'refresh');
}
public function loadSlidingimg()
{
$crud = new grocery_CRUD();
$crud->set_table('tbl_slideimg');
$crud->columns('imgname','active');
$crud->set_subject('Frontpage Sliding Images');
$crud->display_as('imgname','Image name');
$crud->set_field_upload('imgname','uploads');
$crud->display_as('active','Active Flag');
$crud->callback_after_update(array($this,'rename_slideimg_db'));
$crud->callback_after_insert(array($this,'rename_slideimg_db'));
$output = $crud->render();
$this->_admin_output($output);
}
}
Please provide solution for this issue
It happens because the action that took you to that page was a POST request. The browser wants to save you from unintentional double-ordering/paying/whatever when you go "back", when loading that page would need to do the form posted again.
How to work that around? I usually redirect the user to a page which was loaded by a GET request, it can be even the same page, see this answer: https://stackoverflow.com/a/3968038/357403
Or, you could modify the history: https://developer.mozilla.org/en-US/docs/Web/API/History_API
Also, please see the PRG pattern: https://en.wikipedia.org/wiki/Post/Redirect/Get
Try this code format:
$this->load->view('admin/home.php', 'refresh', $data);
or
redirect('home', 'refresh');

$this->Session->read('Config') prints nothing in cakephp

while I try to print $this->Session->read('Config') , it prints nothing.
I am using cakephp 2.5.3
I searched from net ,read many blogs, posts and comments and tried to solve it out but my problem still persists.Though $this->Session->setFlash() works fine but other session values are not working.
App::uses('Controller', 'Controller');
class AppController extends Controller {
var $components = array('Session','RequestHandler');
public function beforeFilter() {
parent::beforeFilter();
debug($this->Session->read('Config'));//null
debug($this->Session->read('Config.userAgent'));//null
debug($this->request->clientIp());// ::1
}
}
please check that at the time of login you create a session or not.
that is $this->Session->write('variable name','variable value').
if you didn't do this you will get null through your code.
the same code give me information about logged-in user in my system.
So be sure that you have created a session,
at the time of login with some variable name and value combination.
please have a look on this:-
public function login() {
if ($this->request->is('post') || $this->request->is('put')) {
$user_detail = $this->User->find('first',array('conditions'=>array('username'=>$this->request->data['User']['username'], 'password'=>$this->Auth->password($this->request->data['User']['password'])),'recursive'=> -1,'fields'=>array('deleted'))); // gettin user details
if(isset($user_detail) && !empty($user_detail)){
if($user_detail['User']['deleted']== '0'){
if ($this->Auth->login()) {// checking user is authorize for login or not?
$this->Session->write('user_role', $this->Auth->user('role_id'));// if yes create session variable with the given variable name and value.
$is_incharge = $this->check_incharge($this->Auth->user('id'));
if ($is_incharge) {
$this->Session->write('is_user_incharge', true);
} else {
$this->Session->write('is_user_incharge', false);
}
$this->redirect($this->Auth->loginRedirect);
} else {
$this->Session->setFlash(__('Invalid User name or Password.'));
$this->redirect($this->Auth->logoutRedirect);
}
}else{
$this->Session->setFlash(__('You are deleted.Can not login.'));
$this->redirect($this->Auth->logoutRedirect);
}
}else{
$this->Session->setFlash(__('Invalid User name or Password..'));
$this->redirect($this->Auth->logoutRedirect);
}
}
}

Categories