codeigniter restful api illegal string offset in several fields - php

I keep on getting this error in my codeigniter micro app restful api. When I post an item only the first letter is get saved with status code 400 being displayed.
here is my model file:
class Cities_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function save($city)
{
$this->db->set($this->setCity($city, null))->insert('cities');
if ($this->db->affected_rows() > 0) {
return $this->db->insert_id;
}
return null;
}
public function update($id, $city)
{
$this->db->set($this->setCity($city))->where('id')->update('cities');
if ($this->db->affected_rows() === 1) {
return true;
}
return false;
}
private function setCity($city)
{
return array(
'id' => $city['id'],
'name' => $city['name']
);
}
}

As you can see setCity function treat $city variable as array. So you need to pass array to setCity function.
class Cities_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function save($city)
{
$this->db->insert('cities',$this->setCity(array('name'=>$cit‌​y,'id'=> null)));
if ($this->db->affected_rows() > 0) {
return $this->db->insert_id();
}
return null;
}
public function update($id, $city)
{
$this->db->where('id',$id)->update('cities',$this->setCity(array('name'=>$cit‌​y,'id'=> $id)));
if ($this->db->affected_rows() === 1) {
return true;
}
return false;
}
private function setCity($city)
{
return array(
'id' => $city['id'],
'name' => $city['name']
);
}
}
another thing is, Codeignitor having method insert_id() to know last insert id.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . '/libraries/REST_Controller.php';
class Cities extends REST_Controller{
public function __construct() {
parent::__construct();
$this->load->model('cities_model');
}
public function index_get(){
$cities=$this->cities_model->get();
if(!is_null($cities))
{
$this->response(array('response'=>$cities),200);
}
else
{
$this->response(array('error'=>'cities cannot be found...'),404);
}
}
public function find_get($id){
if(!$id)
{
$this->respose(null,400);
}
$cit=$this->cities_model->get($id);
if(!is_null($cit))
{
$this->response(array('response'=> $cit),200);
}
else{
$this->response(array('error'=> 'data could not be found...'),404);
}
}
public function index_post(){
// Use validation library, instead of checking just for value.
$this->load->library('form_validation');
$this->form_validation->set_rules('city','City','trim|required');
if($this->form_validation->run() == FALSE)
{
// send back list of validation errors.
$this->response($this->validation_errors(),REST_Controller::HTTP_BAD_REQUEST);
}
$id=$this->cities_model->save($this->post('city'));
if(!is_null($id))
{
$this->response(array('response'=> $id),REST_Controller::HTTP_OK);
}
else
{
$this->response(array('error'=> 'sorry, data could not be saved...'),REST_Controller::HTTP_BAD_REQUEST);
}
}
public function index_put(){
// for put you need to pass id as parameter
// Use validation library, instead of checking just for value.
$this->load->library('form_validation');
$this->form_validation->set_rules('id','ID','trim|required|integer');
$this->form_validation->set_rules('city','City','trim|required');
if($this->form_validation->run() == FALSE)
{
// send back list of validation errors.
$this->response($this->validation_errors(),REST_Controller::HTTP_BAD_REQUEST);
}
$update=$this->cities_model->update($this->post('id'),$this->post('city'));
if(!is_null($update))
{
$this->response(array('response' => 'content updated successfully'),REST_Controller::HTTP_OK);
}
else
{
$this->response(array('error'=> 'sorry, technical error occurred, please try again later...'), REST_Controller::HTTP_BAD_REQUEST);
}
}
public function index_delete($id){
if(!$id)
{
$this->response(null,400);
}
$del=$this->cities_model->delete($id);
if(!is_null($del))
{
$this->response(array('response'=> 'item successfully deleted'),200);
}
else{
$this->response(array('error'=> 'delete operations could not be done...'),400);
}
}
}
here is the model file:
<?php
class Cities_model extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function get($id=null)
{
if(!is_null($id))
{
$query=$this->db->select('*')->from('cities')->where('id',$id)->get();
if($query->num_rows()===1)
{
return $query->row_array();
}
return null;
}
$sql=$this->db->select('*')->from('cities')->get();
if($sql->num_rows()>0)
{
return $sql->result_array();
}
return null;
}
public function save($city)
{
$this->db->insert('cities', array('name'=>$city));
if($this->db->affected_rows()>0)
{
return $this->db->insert_id();
}
return null;
}
public function update($id, $city)
{
$this->db->where('id',$id)->update('cities',$this->setCity(array('name'=>$cit‌​y,'id'=> $id)));
if ($this->db->affected_rows() === 1) {
return true;
}
return false;
}
private function setCity($city)
{
return array('id'=>$city['id'],
'name'=>$city['name']
);
}
public function delete($id)
{
$this->db->where('id',$id)->delete('cities');
if($this->db->affected_rows()===1)
{
return true;
}
return false;
}
}

Related

How can I get the variable in the model for the controller?

I have a $send variable in my model. I want to use it on the controller. But I get an error. How can I call it to the controller?
Model:
public function register_user($send)
{
if($this->emailVerify()) {
$send = array(
'tipo' => 'Error.'
);
return true;
} else {
return false;
}
Controller:
public function __construct()
{
parent::__construct();
$this->send;
}
public function register()
{
$this->load->model('users');
$this->users->register_user();
$this->load->view('signup', $this->send);
}
You can declare a private variable, say send in your model and make getter and setter in your model class in an encapsulated way to get the value in the controller, like below:
Snippet:
Model:
<?php
class Yourmodel extends CI_Model{
private $send;
function __construct() {
parent::__construct();
$this->send = [];
}
public function register_user($send){
if($this->emailVerify()) {
$this->send = array(
'tipo' => 'Error.'
);
return true;
}
return false;
}
public function setSendValue($value){
$this->send = $value;
}
public function getSendValue(){
return $this->send;
}
}
Controller:
<?php
class Controller extends CI_Controller{
private $send;
public function __construct(){
parent::__construct();
$this->send = [];
}
public function register(){
$this->load->model('users');
if($this->users->register_user()){
$this->send = $this->users->getSendValue();
}
$this->load->view('signup', $this->send);
}
}
Replace your code modal and controller as follows:
You need not to declare $send in modal definition, as you are not passing any value while calling the same modal function.
modal positive return can be array $send itself
Catch value of modal function
Modal :
public function register_user()
{
if($this->emailVerify()) {
$send = array(
'tipo' => 'Error.'
);
return $send;
} else {
return false;
}
Controller:
public function __construct()
{
parent::__construct();
$this->send;
}
public function register()
{
$this->load->model('users');
$send = $this->users->register_user();
//print_r($send); // You will get data here
$this->load->view('signup', $this->send);
}
Model
public function register_user($send = "")
{
if($this->emailVerify()) {
$send = array(
'tipo' => 'Error.'
);
return $send;
} else {
return false;
}
Controller
public function __construct()
{
parent::__construct();
$this->send;
}
public function register()
{
$this->load->model('users');
$sendRes = $this->users->register_user(); //now you can use this $send response variable
$this->load->view('signup', $this->send);
}

Codeingiter construct function is not working well

I am trying to create the login system. My login system works 100% perfectly but the problem is that it's not redirecting properly with sessions. When I try the function in index it's working fine but if I move the function to parent construct then it's not fine. I think there is an issue with the parent construct because it's also not loading the library and models in parents. So I have to call everything in each function. Check the code that I tried:
This code works fine:
<?php
/**
*
*/
class Adminpanel04736 extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('session');
}
public function index()
{
if ($this->session->userdata('admin_user')=='')
{
redirect(base_url().'adminpanel04736/admin_login');
}
$this->load->view('admin/dashboard');
}
public function list_of_post()
{
if ($this->session->userdata('admin_user')=='')
{
redirect(base_url().'adminpanel04736/admin_login');
}
$this->load->model('admin_blog_post');
$result['post_list']=$this->admin_blog_post->post_list();
$this->load->view('admin/post_list',$result);
}
// post delete id is comming from post_list php to delete the post
public function post_bin($post_delete_id)
{
if ($this->session->userdata('admin_user')=='')
{
redirect(base_url().'adminpanel04736/admin_login');
}
$this->load->model('admin_blog_post');
$result=$this->admin_blog_post->trash_post($post_delete_id);
if ($result) {
redirect('adminpanel04736/list_of_post'); # code...
}
}
public function my_bin_post()
{
if ($this->session->userdata('admin_user')=='')
{
redirect(base_url().'adminpanel04736/admin_login');
}
$this->load->model('admin_blog_post');
$result['re']=$this->admin_blog_post->trash_bin_post();
$this->load->view('admin/trashed_post',$result);
}
// recycleing the post to repost
public function recycle_post($re)
{
if ($this->session->userdata('admin_user')=='')
{
redirect(base_url().'adminpanel04736/admin_login');
}
$this->load->model('admin_blog_post');
$result=$this->admin_blog_post->repost($re);
if ($result) {
redirect('adminpanel/my_bin_post');
}
}
public function add_new_post()
{
if ($this->session->userdata('admin_user')=='')
{
redirect(base_url().'adminpanel04736/admin_login');
}
$this->load->view('admin/add_new_post');
}
// id is comming from delete post page to delete the post permenently
public function destroy_post($id)
{
if ($this->session->userdata('admin_user')=='')
{
redirect(base_url().'adminpanel04736/admin_login');
}
$this->load->model('admin_blog_post');
$result=$this->admin_blog_post->destroy_post_permenently($id);
if ($result) {
redirect('adminpanel04736/my_bin_post');
}
}
public function post_content()
{
if ($this->session->userdata('admin_user')=='')
{
redirect(base_url().'adminpanel04736/admin_login');
}
$this->load->model('admin_blog_post');
$result=$this->admin_blog_post->adding_the_posts($_POST);
if ($result) {
redirect('adminpanel04736/list_of_post');
}
}
public function admin_login()
{
$this->load->view('admin/lock1');
}
public function loginauthticate()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('admin_password','Password','required');
if ($this->form_validation->run()) {
$this->load->model('admin_blog_post');
$verification=$this->admin_blog_post->login_validation($_POST);
if ($verification) {
$username=$_POST['admin_user'];
$password=$_POST['admin_password'];
$session_data= array(
'admin_user' => $username
);
$this->session->set_userdata($session_data);
redirect(base_url().'adminpanel04736/');
}
else
{
$this->session->set_flashdata('error','Invalid Password');
redirect(base_url().'adminpanel04736/admin_login');
}
}
else
{
$this->admin_login();
}
}
}
?>
But this gives me error redirect too many times. And also doesn't load the library in all functions:
<?php
/**
*
*/
class Adminpanel04736 extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('session');
if ($this->session->userdata('admin_user')=='')
{
redirect(base_url().'adminpanel04736/admin_login');
}
}
public function index()
{
$this->load->view('admin/dashboard');
}
public function list_of_post()
{
$this->load->model('admin_blog_post');
$result['post_list']=$this->admin_blog_post->post_list();
$this->load->view('admin/post_list',$result);
}
// post delete id is comming from post_list php to delete the post
public function post_bin($post_delete_id)
{
$this->load->model('admin_blog_post');
$result=$this->admin_blog_post->trash_post($post_delete_id);
if ($result) {
redirect('adminpanel04736/list_of_post'); # code...
}
}
public function my_bin_post()
{
$this->load->model('admin_blog_post');
$result['re']=$this->admin_blog_post->trash_bin_post();
$this->load->view('admin/trashed_post',$result);
}
// recycleing the post to repost
public function recycle_post($re)
{
$this->load->model('admin_blog_post');
$result=$this->admin_blog_post->repost($re);
if ($result) {
redirect('adminpanel/my_bin_post');
}
}
public function add_new_post()
{
$this->load->view('admin/add_new_post');
}
// id is comming from delete post page to delete the post permenently
public function destroy_post($id)
{
$this->load->model('admin_blog_post');
$result=$this->admin_blog_post->destroy_post_permenently($id);
if ($result) {
redirect('adminpanel04736/my_bin_post');
}
}
public function post_content()
{
$this->load->model('admin_blog_post');
$result=$this->admin_blog_post->adding_the_posts($_POST);
if ($result) {
redirect('adminpanel04736/list_of_post');
}
}
public function admin_login()
{
$this->load->view('admin/lock1');
}
public function loginauthticate()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('admin_password','Password','required');
if ($this->form_validation->run()) {
$this->load->model('admin_blog_post');
$verification=$this->admin_blog_post->login_validation($_POST);
if ($verification) {
$username=$_POST['admin_user'];
$password=$_POST['admin_password'];
$session_data= array(
'admin_user' => $username
);
$this->session->set_userdata($session_data);
redirect(base_url().'adminpanel04736/');
}
else
{
$this->session->set_flashdata('error','Invalid Password');
redirect(base_url().'adminpanel04736/admin_login');
}
}
else
{
$this->admin_login();
}
}
}
?>
Your admin_login is method of Adminpanel04736 class.
But when any method is called (including admin_login) and a user is not already logged, you redirect again to admin_login. So, you get infinite redirects instead of login procedure.
Move admin_login out of the class or check a method name before redirect, something as
function __construct()
{
parent::__construct();
$this->load->library('session');
if ($this->session->userdata('admin_user')=='' and
$this->uri->segment(2) !== 'admin_login'))
{
redirect(base_url().'adminpanel04736/admin_login');
}
}
Before use redirect(), you should load this: $this->load->helper('url');
function __construct()
{
parent::__construct();
$this->load->library('session');
if ($this->session->userdata('admin_user')=='' and
$this->uri->segment(2) !== 'admin_login'))
{
redirect(base_url().'adminpanel04736/admin_login');
}
}

authorization doesn't work yii2

I'm trying to do:
when user authorized go to home page
when user not authorized go to Login page
but now when I put (correct) user email and password that always refresh login page and doesn't log in into system.
User(ActiveRecord)
class User extends ActiveRecord implements IdentityInterface
{
public function setPassword($user_password)
{
$this->password = sha1($user_password);
}
public function validatePassword($user_password)
{
return $this->user_password === sha1($user_password);
}
public static function findIdentity($id)
{
return self::findOne($id);
}
public static function findIdentityByAccessToken($token, $type = null)
{
}
public function getId()
{
return $this->user_id;
}
public function getAuthKey()
{
}
public function validateAuthKey($authKey)
{
}
}
Login Model:
class Login extends Model
{
public $user_email;
public $user_password;
public function rules()
{
return [
[['user_email', 'user_password'],'required'],
['user_email','email'],
['user_password','validatePassword']
];
}
public function validatePassword($attribute,$params)
{
if(!$this->hasErrors())
{
$user = $this->getUser();
if(!$user || !$user->validatePassword($this->user_password))
{
$this->addError($attribute, 'Пароль или пользователь введенны не верно');
}
}
}
public function getUser()
{
return User::findOne(['user_email'=>$this->user_email]);
}
}
?>
SiteController(only login function)
public function actionLogin()
{
if(!Yii::$app->user->isGuest)
{
return $this->goHome();
}
else {
$login_model = new Login();
return $this->render('login',['login_model'=>$login_model]);
}
}
Putting username and password is not enough you should also perform a login
public function actionLogin()
{
if(!Yii::$app->user->isGuest)
{
return $this->goHome();
}
if ($model->load(Yii::$app->getRequest()->post())) {
// you should perform login
\Yii::$app->getUser()->login($model->user, $this->rememberMe ? $model->module->rememberFor : 0);
return $this->goBack();
}
else {
$login_model = new Login();
return $this->render('login',['login_model'=>$login_model]);
}
}

(ASK) Failed on adding and updating data to database mysqli using codeIgniter

So I tried to make a simple crud application. It works well when I try to delete,otherwise it always goes wrong when it comes to adding and updating data. My script didnt succeed on adding or updating data to the database. I'm using code igniter php frameworks here
Here are my script of controller and models for your references
<?php
//script of controller
class Siswa extends CI_Controller
{
private $limit=10;
function __construct()
{
parent::__construct();
$this->load->library(array('table','form_validation'));
$this->load->helper(array('form','url'));
$this->load->model('siswa_model','',TRUE);
}
function index($offset=0,$order_column='id',$order_type='asc')
{
if(empty($offset))
$offset=0;
if(empty($order_column))
$order_column='id';
if(empty($order_type))
$order_type='asc';
$siswas=$this->siswa_model->get_paged_list($this->limit,$offset,$order_column,$order_type)->result();
$this->load->library('pagination');
$config['base_url'] = site_url('siswa/index');
$config['total_rows'] = $this->siswa_model->count_all();
$config['per_page']=$this->limit;
$config['url_segment']=3;
$this->pagination->initialize($config);
$data['pagination']=$this->pagination->create_links();
$this->load->library('table');
$this->table->set_empty(" ");
$new_order=($order_type=='asc'?'desc':'asc');
$this->table->set_heading
(
'No',
anchor('siswa/index/'.$offset.'/nama'.$new_order,'Nama'),
anchor('siswa/index/'.$offset.'/alamat'.$new_order,'Alamat'),
anchor('siswa/index'.$offset.'/jenis_kelamin'.$new_order,'Jenis Kelamin'),
anchor('siswa/index'.$offset.'/tanggal_lahir'.$new_order,'Tanggal Lahir (dd-mm-yyyy)'),
'Actions'
);
$i=0+$offset;
foreach ($siswas as $siswa)
{
$this->table->add_row(++$i,
$siswa->nama,
$siswa->alamat,
strtoupper($siswa->jenis_kelamin)=='M'?
'Laki-laki':'Perempuan',
date('d-m-Y',strtotime(
$siswa->tanggal_lahir)),
anchor('siswa/view/'.$siswa->id,
'view',array('class'=>'view')).' '.
anchor('siswa/update/'.$siswa->id,
'update',array('class'=>'update')).' '.
anchor('siswa/delete/'.$siswa->id,
'delete',array('class'=>'delete',
'onclick'=>"return confirm
('Apakah Anda yakin ingin menghapus data siswa?')"))
);
}
$data['table']=$this->table->generate();
if($this->uri->segment(3)=='delete_success')
$data['message']='Data berhasil dihapus';
else if ($this->uri->segment(3)=='add_succsess')
$data['message']='Data berhasil ditambah';
else
$data['message']='';
$this->load->view('siswaList',$data);
}
function add()
{
$data['title']='Tambah siswa baru';
$data['action']= site_url('siswa/add');
$data['link_back'] = anchor('siswa/index/','Back to list of siswas',array('class'=>'back'));
$this->_set_rules();
if($this->form_validation->run() === FALSE)
{
$data['message']='';
$data['title']='Add new siswa';
$data['message'] = '';
$data['siswa']['id']='';
$data['siswa']['nama']='';
$data['siswa']['alamat']='';
$data['siswa']['jenis_kelamin']='';
$data['siswa']['tanggal_lahir']='';
$data['link_back'] = anchor('siswa/index','Lihat Daftar Siswa',array('class'=>'back'));
$this->load->view('siswaEdit',$data);
}
else
{
$siswa = array('nama'=>$this->input->post('nama'),
'alamat'=>$this->input->post('alamat'),
'jenis_kelamin'=>$this->input->post('jenis_kelamin'),
'tanggal_lahir'=>date('Y-m-d',strtotime($this->input->post('tanggal_lahir'))));
$this->validation->id = $id;
$data['siswa']=$this->siswa_model->add($id,$siswa);
redirect('siswa/index/add_success');
}
}
function view($id)
{
$data['title']='siswa Details';
$data['link_back']= anchor('siswa/index/','Lihat daftar siswas',array('class'=>'back'));
$data['siswa']=$this->siswa_model->get_by_id($id)->row();
$this->load->view('siswaView',$data);
}
function update($id)
{
$data['title']='Update siswa';
$this->load->library('form_validation');
$this->_set_rules();
$data['action']=('siswa/update/'.$id);
if($this->form_validation->run()===FALSE)
{
$data['message']='';
$data['siswa']=$this->siswa_model->get_by_id($id)->row_array();
$_POST['jenis_kelamin'] = strtoupper($data['siswa']['jenis_kelamin']);
$data['siswa']['tanggal_lahir']=date('d-m-Y',strtotime($data['siswa']['tanggal_lahir']));
$data['title']='Update siswa';
$data['message']='';
}
else
{
$id=$this->input->post('id');
$siswa=array('nama'=>$this->input->post('nama'),
'alamat'=>$this->input->post('alamat'),
'jenis_kelamin'=>$this->input->post('jenis_kelamin'),
'tanggal_lahir'=> date('Y-m-d',strtotime($this->input->post('tanggal_lahir'))));
$this->siswa_model->update($id,$siswa);
$data['message']='update siswa success';
}
$data['link_back']= anchor('siswa/index/','Lihat daftar siswas',array('class'=>'back'));
$this->load->view('siswaEdit',$data);
}
function delete($id)
{
$this->siswa_model->delete($id);
redirect('siswa/index/delete_success','refresh');
}
function _set_rules()
{
$this->form_validation->set_rules('nama','Nama','required|trim');
$this->form_validation->set_rules('jenis_kelamin','Jenis Kelamin','required');
$this->form_validation->set_rules('alamat','Alamat','required');
$this->form_validation->set_rules('tanggal_lahir','Tanggal Lahir','required|callback_valid_date');
}
function valid_date($str)
{
if(!preg_match('/^[0-9]{2}-[0-9]{2}-[0-9]{4}$/',$str))
{
$this->form_validation->set_message('valid_date','date format is not valid. dd-mm-yyyy');
return false;
}
else
{
return true;
}
}
}
?>
<?php
//script of model
class Siswa_model extends CI_Model
{
private $primary_key='id';
private $table_name='siswa';
function __construct()
{
parent::__construct();
}
function get_paged_list($limit=10,$offset=0,$order_column='',$order_type='asc')
{
if(empty($order_column) || empty ($order_type))
{
$this->db->order_by($this->primary_key,'asc');
}
else
{
$this->db->order_by($order_column,$order_type);
}
return $this->db->get($this->table_name,$limit,$offset);
}
function count_all()
{
return $this->db->count_all($this->table_name);
}
function get_by_id($id)
{
$this->db->where($this->primary_key,$id);
return $this->db->get($this->table_name);
}
function save($person)
{
$this->db->insert($this->table_name,$person);
return $this->db->insert_id();
}
function update($id,$person)
{
$this->db->where($this->primary_key,$id);
$this->db->update($this->table_name,$person);
}
function delete($id)
{
$this->db->where($this->primary_key,$id);
$this->db->delete($this->table_name);
}
}
?>
function add() {
...........
$siswa = array('nama'=>$this->input->post('nama'),
'alamat'=>$this->input->post('alamat'),
'jenis_kelamin'=>$this->input->post('jenis_kelamin'),
'tanggal_lahir'=>date('Y-m-d',strtotime($this->input->post('tanggal_lahir'))));
$this->validation->id = $id;
$data['siswa']=$this->siswa_model->add($id,$siswa);
redirect('siswa/index/add_success');
$id is not declared anywhere.
You call $this->siswa_model->add($id,$siswa) but the function add() does not exists in siswa_model (it's called save())

Callback Function Error ( Unable to access an error message corresponding to your field name )

I am new to Codeigniter. I have error when i try to check the email is existed or not. I saw lot of post on Stackoverflow and other website. I can't get any result.
When i try with below coding i got below errors
Unable to access an error message corresponding to your field name
Email.(email_check)
Please check my code.
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
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|trim|callback_email_check");
$this->form_validation->set_rules("password","Password","required|md5|trim|callback_password_check");
if($this->form_validation->run())
{
redirect('main/members');
}
else
{
$this->load->view('login');
}
}
public function members()
{
$this->load->model('model_users');
if($this->model_users->can_log_in())
{
return true;
}
else
{
$this->form_validation->set_message('email_check', 'Incorrect Username/Password');
return false;
}
}
}
MODEL
<?php
class Model_users extends CI_Model
{
public function can_log_in()
{
$this->db->where->('email',$this->input->post('email'));
$this->db->where->('password',md5($this->input->post('password')));
$query = $this->db->get('users');
if($query->num_rows == 1)
{
return true;
}
else
{
return false;
}
}
}
?>
I believe you are missing your callback function email_check, and there the set_message should correspond to the function and not the field itself.
You will have to add the 2 callback functions that you are using :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
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|trim|callback_email_check");
$this->form_validation->set_rules("password","Password","required|md5|trim|callback_password_check");
if($this->form_validation->run())
{
redirect('main/members');
}
else
{
$this->load->view('login');
}
}
public function email_check(){
//perform your validation here
if({your_validation_result}){
return true;
}else{
$this->form_validation->set_message('email_check', 'Incorrect Username/Password');
return false;
}
}
public function password_check(){
//perform your validation here
if({your_validation_result}){
return true;
}else{
$this->form_validation->set_message('password_check', 'Incorrect Username/Password');
return false;
}
}
public function members()
{
$this->load->model('model_users');
if($this->model_users->can_log_in())
{
return true;
}
else
{
$this->form_validation->set_message('email_check', 'Incorrect Username/Password');
return false;
}
}
}

Categories