I am new in CodeIgniter. I am getting a problem. My procedure seems logical but it would not work!
I have a controller: User
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('user_model');
}
public function index($slug = FALSE) {
$data['title'] = "User List";
if($slug === FALSE) {
$data['user'] = $this->user_model->get_user();
} else {
$data['user'] = $this->user_model->get_user($slug);
}
if (empty($data['user'])){
show_404();
}
$this->load->library('table');
$this->load->view('user_view', $data);
}
public function authen() {
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('phone', 'phone', 'required');
$this->form_validation->set_rules('password', 'password', 'required');
if ($this->form_validation->run() === FALSE) {
$this->load->view('login_form');
} else {
if($this->user_model->do_login()===TRUE) {
$this->index();
}
$this->authen();
}
}
}
The "authen" method is not working properly with its last conditions. Page does not redirect to controller.
Can anybody help?
Thanks
You can use: redirect('user') it will redirect u to the controller user class
Instead of
$this->index();
try this
header('Location:http://myipaddress/mysite/user');
You are try to load the login page in case there is any authentication error.
For for this you will need to bring user to the login page again.
use redirect('user/login'); and store the error message in session flash.
or display validation errors..
you cannot call controller from a controller. as $this->index();
Related
After right login details,form redirects to same place
This is my controller for the login
The login should be redirected to the dashboard after validation and database check
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('login_model');
if($this->session->userdata('logged_in')) {
redirect(base_url().'dashboard');
}
}
public function index(){
$template['page_title'] = "Login";
if(isset($_POST)) {
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database');
if($this->form_validation->run() == TRUE) {
redirect(base_url().'dashboard');
}
}
$this->load->view('Templates/header', $template);
$this->load->view('Login/login_form');
$this->load->view('Templates/footer');
}
}
code for Dashboard
class Dashboard extends CI_Controller {
public function __construct() {
parent::__construct();
date_default_timezone_set("Asia/Kolkata");
$this->load->model('dashboard_model');
if(!$this->session->userdata('logged_in')) {
redirect(base_url());
}
}
Try This Method
redirect('dashboard');
Try site_url() as well. Make sure there are proper code(error free) of view in dashboard controller.
redirect(site_url()."/dashboard");
On my codeigniter Version 3.1.4 / hmvc login if the form validation password input is empty for some reason the callback function shows first rather than the required message?
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
As shown on message it should show the password required message first rather than callback
Question how can I make sure it shows the correct validation messages
first.
Autoloaded form_validation
$autoload['libraries'] = array('database', 'form_validation');
Controller
<?php
class Login extends MX_Controller {
public $CI;
public function __construct() {
parent::__construct();
$this->load->model('catalog/user/user_model');
$this->form_validation->CI =& $this;
}
public function index() {
$data['type'] = $this->input->post('type');
$data['password'] = $this->input->post('password');
$this->form_validation->set_rules('type', 'Username Or Email', 'trim|required');
$this->form_validation->set_rules('password', 'password', 'required|callback_validatePassword[password]');
if ($this->form_validation->run() == false) {
$data['header'] = Modules::run('catalog/common/header/index');
$data['footer'] = Modules::run('catalog/common/footer/index');
$this->load->view('template/account/login', $data);
} else {
$user_info = $this->user_model->get_user($user_id = '', $this->input->post('type', true));
if ($user_info) {
$remember = $this->input->post('remember') ? TRUE : FALSE;
$this->user->login($user_info['user_id'], $remember);
redirect(base_url("users") .'/'. $user_info['user_id'] .'/'. $user_info['username']);
}
}
}
public function validatePassword($str) {
if ($this->user_model->validate_password($str, $this->input->post('type', true))) {
return TRUE;
} else {
$this->form_validation->set_message('validatePassword', 'Opp\'s somthing wrong with login!');
return FALSE;
}
}
}
application > libraries > MY_Form_validation.php
<?php
class MY_Form_validation extends CI_Form_validation {
public $CI;
}
Instead of this -
$this->form_validation->set_rules('password', 'password', 'required|callback_validatePassword[password]');
kindly use the callback function like this -
$this->form_validation->set_rules('password', 'password', 'required', 'callback_validatePassword');
just keep the callback validation separate
$this->form_validation->set_rules('password', 'password', 'trim|required');
// check for post values is empty or not?
if(!empty($data['password'])) {
$this->form_validation->set_rules('password', 'password', 'callback_validatePassword[password]');
}
I am VERY new to codeigniter so please excuse me if this is a n00bish sort of question...
I have a controller called dashboard.php:
class Dashboard extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->load->view('site_header');
$this->load->view('dashboard');
$this->load->view('site_footer');
}
else
{
//If no session, redirect to login page
echo 'hello there';
//redirect('main', 'refresh');
}
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('home', 'refresh');
}
}
?>
This page loads fine when i access it via localhost/sitename/dashboard.
HOWEVER i am having issues trying to redirect to this controller from another controller of mine. The controller that is calling the redirect is verifyLogin.php (in same directory level)
class VerifyLogin extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
function index()
{
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == FALSE)
{
//Field validation failed. User redirected welcome page
redirect('');
}
else
{
//Go to private area
redirect('dashboard', 'index');
}
}
function check_database($password)
{
//Field validation succeeded. Validate against database
$username = $this->input->post('username');
//query the database
$result = $this->user->login($username, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'id' => $row->ID,
'username' => $row->username,
'stay_logged' => true
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
}
?>
When the redirect() is called i get an error - Unable to load the requested file: dashboard.php
After this error, i can no longer access localhost/sitename/dashboard (i just get that same error).
Some advice would be amazing right now as well as a way of debugging this for future problems.
Cheers!
Use this pattern to redirect
redirect("controllername","function name");
or
redirect(base_url().'index.php/controller/function');
Trying to load a view that doesn't exist. Load dashboard.php to your view folder. I hope it works ;))
By looking at you problem, I think this should help you out use this:
redirect(site_url().'dashboard', 'index');
try this one in verifylogin controller
redirect('Dashboard', 'refresh');
Let me know the result
I have these issue where I want to set a rules on three input forms of type "text", my rule is that at least one of these three has values (either of the three), I have no idea how to set them in CI, because they are altogether executed when run() is triggered, any on of you guys knows how to set these kind of rules to form validation in CI please do share your knowledge.
You can set your own type of validation functions. It is pretty well documented here, but an excerpt would be:
<?php
class Form extends CI_Controller {
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
public function username_check($str)
{
if ($str == 'test')
{
$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
return FALSE;
}
else
{
return TRUE;
}
}
}
?>
callback_username_check is calling the username_check function in the controller
To answer your latest comment
// $data is $_POST
function my_form_validator($data)
{
$data = 'dont worry about this';
// you have access to $_POST here
$field1 = $_POST['field1'];
if($field1 OR $field2 OR $field3)
{
// your fields have value
return TRUE;
}
else
{
// your fields dont have any value
$this->form_validation->set_message('field1', 'At least one of the 3 fields should have a value');
return FALSE;
}
}
I'm not CI programmer, just trying to learn it. Maybe this is wrong approach, please advice.
my controller(not in sub directory) :
class Users extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index($msg = NULL) {
$this->load->helper(array('form'));
$data['msg'] = $msg;
$this->load->view('user/login' , $data);
}
public function process_logout() {
$this->session->sess_destroy();
redirect(base_url());
}
}
And a route for login :
$route['user/login'] = 'users/index';
Problem is when I wanna logout, it shows me 404 because I do not have it in my route :
$route['user/process_logout'] = 'users/process_logout';
and in my view I put logout
When I add that, it works, and that is stuppid to add a route for everything. What I'm I doing wrong, please advice.
Thank you
Don't know why you are trying to implement login feature in index() function. However since you said you are learning CI I'm telling something about _remap() function.
Before that. You can try the following routing:
$route['user/:any'] = 'users/$1';
$route['user/login'] = 'users/index';
If you want to take value immediately after controller segment you need to use _remap() function and this function may be solve your routing problem, i mean you don't need to set routing. Lets implement your code controller 'users' using _remap() function.
class Users extends CI_Controller {
private $sections = array('login', 'logout');
function __construct() {
parent::__construct();
}
public function _remap($method)
{
$section = $this->uri->segment(2);
if(in_array($section, $this->sections))
call_user_func_array(array($this, '_'.$section), array());
else show_404(); // Showing 404 error
}
private function _login()
{
$msg = $this->uri->segment(3);
$this->load->helper(array('form'));
$data['msg'] = $msg;
$this->load->view('user/login' , $data);
}
public function _logout() {
$this->session->sess_destroy();
redirect(base_url());
}
}