I have a login function in CodeIgniter with this code:
public function login() {
$this->redirect_if_logged($this->login_check());
$this->data['active'] = 'login';
$this->load->model('user_model');
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback__validate_login');
if (!$this->form_validation->run()) {
$this->load->template('login', $this->data);
} else {
redirect('/','refresh');
}
}
And a validation function:
public function _validate_login($password) {
$this->form_validation->set_message('_validate_login', 'Invalid username or password');
return false;
}
The problem is that the custom function is never called, the validator always return true if all rules pass. The validator itself works, I checked it with other rules. It is just ignoring my custom function. What am I missing here?
create MY_Form_validation extends CI_Form_validation in your library
class MY_Form_validation extends CI_Form_validation
{
public function _validate_login($password) {
$this->form_validation->set_message('_validate_login', 'Invalid username or password');
return false;
}
in your controller remove callback
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|_validate_login')
Related
When I load the main page and try to leave the login details blank,I get page you requested is not found.This my code main and login files resp. kindly help
<?php
class Main extends CI_Controller
{
public function index()
{
$this->login();
}
public function login()
{
$this->load->view('login');
}
public function members()
{
$this->load->view('members');
}
public function login_validation()
{
//do validation here,load validation library
$this->load->library('form_validation');
//setting rules for input data
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required|md5');
if($this->form_validation->run())
{
redirect('main/members');
}
else
{
$this->load->view('login');
}
}
}
This means your login.php view does not exist.
You should check your view folder to see if login.php exist there.
Suppose this is my controller. (copied from CI documentation)
<?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;
}
}
}
?>
But username_check($str) function is public. According to CI documentation if i want to make a private method I need to add "_" like this.
private function _utility()
{
// some code
}
But how can I set username_check() as private and callback from form validation set_rules?
Should i use DOUBLE underscore "__" i.e callback__username_check?
You can just declare your private function as you have already done:
function _username_check()
{
// some code
}
And in the validation rule, use:
callback__username_check
As I have seen, this must work just fine!
Remember:
The _ prefix would take care of your function privacy, so you do not really have to use the keyword private to declare the function to let the form_validation class access to that function!
I'm trying to have my login controller in my main page, but when I try to login it goes to a page with just the view/login code.
Here is my controller:
public function home(){
$this->load->model("get_products");
$data['results'] = $this->get_products->getData();
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("content_home", $data);
$this->load->view("content_left");
$this->load->view("content_right");
$this->login();
$this->login_validation();
$this->load->view("site_footer");
}
public function login(){
$this->load->view('login');
}
public function login_validation(){
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|trim');
$this->form_validation->set_rules('password', 'Password', 'required|md5');
if($this->form_validation->run()){
$this->load->view('logged');
}
}
How can I login in the home page, without loading a new page?
No need of 2 functions, use one function like this,
public function login(){
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|trim');
$this->form_validation->set_rules('password', 'Password', 'required|md5');
if($this->form_validation->run() == FALSE){
$this->load->view('login');
} else {
// here you can redirect to main controller instead of loading the view, since ligin part is related here
$this->load->view('logged');
}
}
And you are calling 2 functions in home()
$this->login();
$this->login_validation();
Both will not get called as you are loading a view in first one.
I have codeigniter installed on my localhost
The main.php controller is
class Main extends CI_Controller {
public function index()
{
$this -> login();
}
public function login()
{
$this->load->view('login');
}
public function login_validation() {
$this->load->library('form_validation');
$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('password','Password','required|md5');
if($this->form_validation->run())
{
redirect('main/members');
}
else
{
$this->load->view('login');
}
}
}
The login is working page is coming,but after I fill the username and password,should take me to login/main/login_validation and from there the function login_validation() should either redirect to main/members or show me the login page.But what happens is when I submit the form,the object not found error is coming.Can anyone help me out?
form is
form_open('main/login_validation');
To answer what I think your question is getting at, you're using the form_validation class wrong. You should be testing whether or not the form_validation->run() is TRUE (all fields passed validation) or FALSE (there was an error in one or more of the inputs). Structure you code like such:
public function login_validation() {
$this->load->library('form_validation');
$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('password','Password','required|md5');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('login');
}
else
{
redirect('main/members');
}
}
This is covered in the CodeIgniter documentation here.
in your controller ..you could try this..
function index()
{
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if($this->form_validation->run() == FALSE)
{
//Field validation failed. User redirected to login page
$this->load->view('login');
}
else
{
$this->check_database();
//redirect('home');
}
}
function check_database()
{
//Field validation succeeded. Validate against database
$username = $this->input->post('username');
$password = $this->input->post('password');
//query the database
$query_result = $this->model_login->login($username, $password);
if($query_result)
{
$this->load->view('main/members');
}
else
{
redirect('login');
}
}
in model
function login($username,$password){
$query = $this->db->query("SELECT * FROM `login` WHERE `username`= '".$username."' AND
password='".$password."' LIMIT 1");
if ($query->num_rows() > 0)
return TRUE;
else
return FALSE;
}
i made a controller name of user.php i test complete project on local host it works fine but when i upload online on website it shows error , i submit the form then form load controller called user.php and error on the first line its called parse error i check but still the same will you please suggest.
<?php
class User extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
}
public function create_user()
{
// field name, error message, validation rules
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('city', 'City', 'trim|required');
if($this->form_validation->run() == FALSE)
{
$this->load->view('ar_signup');
}
else
{
$this->load->model('Users_model');
if($this->input->post("language")=="ar")
{
$this->load->view('ar_thanks');
}
else
{
$this->load->view('en_thanks');
}
if($query = $this->Users_model->create_member())
{
$this->load->view('ar_thanks');
}
}
}
}
Please format your code and use IDE. And you will not have these errors. Your formatted code:
<?php
class User extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
}
public function create_user()
{
// field name, error message, validation rules
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('city', 'City', 'trim|required');
if($this->form_validation->run() == FALSE) {
$this->load->view('ar_signup');
} else {
$this->load->model('Users_model');
if($this->input->post("language")=="ar") {
$this->load->view('ar_thanks');
} else {
$this->load->view('en_thanks');
}
if($query = $this->Users_model->create_member()) {
$this->load->view('ar_thanks');
}
}
}
}