I am working with Codeigniter 2.1.4 and also new to this framework. When ever i am trying to load the model on the controller class, error id 500 being thrown by the browser. Here is the code of the model class file name:
merchant_login.php
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class Merchant_model extends MY_Model {
public $tbl_user = 'users';
function __construct(){
parent::__construct();
$this->load->database('merchant', TRUE, TRUE);
}
function user_login($data)
{
$this->db->from($this->tbl_user);
$this->db->where($data);
$query = $this->db->get();
if($query->num_rows()==1)
{
$this->db->select("CURRENT_LOGIN");
$this->db->from($this->tbl_user);
$this->db->where("USER_ID", $data["USER_ID"]);
$q = $this->db->get();
$res = $q->result();
$current_time = $res[0]->CURRENT_LOGIN;
$last_time = $current_time;
$current_time = date("Y-m-j H:i:s");
$arr = array(
"CURRENT_LOGIN" => $current_time,
"LAST_LOGIN" => $last_time,
"LAST_IP" => $this->input->ip_address()
);
$this->db->where("USER_ID", $data["USER_ID"]);
$this->db->update($this->tbl_user, $arr);
$this->db->from($this->tbl_user);
$this->db->where($data);
$query = $this->db->get();
return $query->row_array();
}
return false;
}
} ?>
Here is the code for controller and the file name is:
"login.php"
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Login extends MY_Controller {
function __construct() {
parent::__construct();
$this->load->library(array("form_validation", "session"));
}
//merchant section start
public function merchant_login() {
$this->session->keep_flashdata("return_url");
$this->form_validation->set_rules('loginName', 'Login', 'trim|valid_email|required');
$this->form_validation->set_rules('loginPassword', 'Password', 'trim|required');
if ($this->session->userdata("merchantLogged") != 1) {
if ($this->form_validation->run() == FALSE) {
$data["error"] = validation_errors();
header('Content-type: application/json');
echo json_encode($data);
} else {
$d['USER_ID'] = $this->input->post("loginName");
$d['USER_PASSWD'] = sha1($this->input->post("loginPassword"));
$this->load->model("merchant_model", "model", TRUE);
$user = $this->model->user_login($d);
if ($user) {
if ($this->input->post("security") == 'not') {
//Cal to a member function of the model class
if ($this->model->user_ip_check($d) == false) {
$this->session->set_userdata('u_id', $this->input->post("loginName"));
$this->session->set_userdata('u_pd', $this->input->post("loginPassword"));
$data['redirect'] = site_url("merchants/ip_security/");
} else {
$usr['type'] = "MR";
unset($user['USER_PASSWD']);
// unset($user['ID']);
unset($user['MM_NAME']);
//$this->load->database('default', TRUE, TRUE);
$this->session->set_userdata($usr);
$this->session->set_userdata($user);
$this->set_merchantLogged(1);
// if($this->session->flashdata("return_url")){
// $data['redirect'] = site_url("user/" . $this->session->flashdata("return_url"));
// }
// else{
$data['redirect'] = site_url("merchant/account");
// }
}
} else {
$d['que'] = $this->input->post("sec_q");
$d['ans'] = $this->input->post("sec_q_answer");
if ($this->model->check_que($d) == true) {
$this->session->unset_userdata('u_id');
$this->session->unset_userdata('u_pd');
$usr['type'] = "MR";
unset($user['USER_PASSWD']);
unset($user['MM_NAME']);
$this->session->set_userdata($usr);
$this->session->set_userdata($user);
$this->set_merchantLogged(1);
$data['redirect'] = site_url("merchant/account");
} else {
$data['redirect'] = site_url("merchants/ip_security");
}
}
} else {
$data["error"] = "Username or password are wrong";
$data["islogin"] = true;
}
header('Content-type: application/json');
echo json_encode($data);
}
} else {
if ($this->session->flashdata("return_url")) {
$data['redirect'] = site_url("merchant/" . $this->session->flashdata("return_url"));
} else {
$data['redirect'] = site_url("home");
}
header('Content-type: application/json');
echo json_encode($data);
}
}
}
?>
class Merchant_model extends MY_Model
replace
class Merchant_model extends CI_Model
class Login extends MY_Controller
replace
class Login extends CI_Controller
syntax error:$this->load->database('merchant', TRUE, TRUE); correct this one
My Suggestion please go through on the codeigniter document it can helpfull you
http://ellislab.com/codeigniter/user-guide/general/controllers.html
change your model file name from merchant_login.php to merchant_model.php
and change in login.php
$this->load->model("merchant_model", "model", TRUE);
to
$this->load->model("merchant_model", "", TRUE);
Related
How do I retrieve (and process) the params I have being passed in from this...
Modules::load('MembersList', $this->input->get(NULL, TRUE);
... into the module its being passed into?
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MemberList extends MX_Controller {
function __construct()
{
parent::__construct();
$this->load->model('memberslist_model');
}
public function index()
{
// $params = how_do_i_retrieve it?
if ( ! isset($list) || $list == 'individuals')
{
$data['module'] = 'Individuals';
$data['results'] = $this->memberslist_model->list_individuals();
$data['count'] = count($data['results']);
$data['view'] = $this->load->view('list_individuals', $data, TRUE);
}
if ( $list == 'families')
{
$data['module'] = 'Families';
}
return $this->load->view('memberslist', $data, TRUE);
}
public function settings()
{
$data['settings'] = $this->memberslist_model->settings();
return $this->load->view('settings', $data, TRUE);
}
}
I understand that it is being passed as URI segments like codeigniter but I've tried everything and I can't get it to work.
I'm not sure which version you use, but you can try this:
$whatEver = Modules::run('MembersList/index', $this->input->get(NULL, TRUE));
class MemberList extends MX_Controller {
function __construct()
{
parent::__construct();
$this->load->model('memberslist_model');
}
public function index($params)
{
print_r($params);
// $params = how_do_i_retrieve it?
if ( ! isset($list) || $list == 'individuals')
{
$data['module'] = 'Individuals';
$data['results'] = $this->memberslist_model->list_individuals();
$data['count'] = count($data['results']);
$data['view'] = $this->load->view('list_individuals', $data, TRUE);
}
if ( $list == 'families')
{
$data['module'] = 'Families';
}
return $this->load->view('memberslist', $data, TRUE);
}
public function settings()
{
$data['settings'] = $this->memberslist_model->settings();
return $this->load->view('settings', $data, TRUE);
}
}
I am testing with Codeigniter3.0.6 on User agent as below function then I try to testing IPhone, and Chrome inspect device mode but I got only number 1.
I want to check if I view this website in Mobile phone it will echo number 2
if in PC browser echo number1 if can't detect browser show 0.
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Main_Controller extends MY_Controller
{
public $data = array();
public function __construct()
{
parent::__construct();
var_dump($this->CheckDevices());
exit();
$this->data['deviceType'] = $this->CheckDevices();
if ($this->data['deviceType'] == 1) {
$this->set_navigation();
}
elseif ($this->data['deviceType'] == 2) {
return false;
}elseif($this->data['deviceType'] == 0){
return false;
}
}
private function CheckDevices()
{
$this->load->library('user_agent');
$agent = '';
if ($this->agent->is_browser()) {
$agent = 1;
} elseif ($this->agent->is_mobile()) {
$agent = 2;
} else {
$agent = 0;
}
return $agent;
}
private function set_navigation()
{
$this->load->library('session');
$this->load->library("nav_libs");
return $this->data['menus'] = $this->nav_libs->navigation();
}
}
?>
you can try this
application/core/My_Core.php
class My_Core extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function detect_dvice(){
$this->load->library('user_agent');
if( $this->agent->is_mobile()){
$_is_mobile = 1;
}
else{
$_is_mobile = 2;
}
return $_is_mobile
}
}
normal controller
application/controllers/Test_controller.php
class Test_controller extends My_Core{
public function __construct(){
parent::__construct();
}
public function index(){
echo $this->detect_dvice();
}
}
I tried redirect() and $this->load->view('target_page') but, no success for me so please help me with this:
My controller is here:
class Login_control extends CI_Controller {
public function index() {
$this->load->model('login_model');
$this->load->helper('url');
if(isset($_POST['Logusername'])|| isset($_POST['Logpassword']))
{
$user = $_POST['Logusername'];
$pass = $_POST['Logpassword'];
$data = $this->login_model->login1($user,$pass);
if($data > 0 )
{
echo '<font color="#00FF00">'. "OK".'</font>';
$this->load->view('testing',$data);
}
else
{ echo '<font color="#FF0000">'. "Login Failed ! Username or Password is Incorrect.".'</font>' ;
}
exit();
}
$this->load->view('signup_view');
}
}
Try this code. You have written echo before redirect so, it might not work.
class Login_control extends CI_Controller{
public function index()
{
$this->load->model('login_model');
$this->load->helper('url');
if(isset($_POST['Logusername'])|| isset($_POST['Logpassword']))
{
$user = $_POST['Logusername'];
$pass = $_POST['Logpassword'];
$data = $this->login_model->login1($user,$pass);
if($data > 0 )
{
redirect('testing');
}
else
{ echo '<font color="#FF0000">'. "Login Failed ! Username or Password is Incorrect.".'</font>' ;
}
exit();
}
$this->load->view('signup_view');
}
}
Try this
In Controller
class Login_control extends CI_Controller{
public function index()
{
$this->load->model('login_model');
$this->load->helper('url');
if(isset($_POST['Logusername']) && isset($_POST['Logpassword'])) # Change || to &&
{
$user = $_POST['Logusername'];
$pass = $_POST['Logpassword'];
$data = $this->login_model->login1($user,$pass);
if($data == 1){ # check is there only one user
echo '<font color="#00FF00">OK</font>';
$this->load->view('testing',$data);
}
else{
echo '<font color="#FF0000">Login Failed ! Username or Password is Incorrect.</font>' ;
}
}
$this->load->view('signup_view');
}
}
In Model
public function login1($user,$pass)
{
$query = $this->db->query("SELECT * FROM user WHERE username= '$user' AND password = '$pass' ");
$result = $query->result_array();
$count = count($result); # get count of result
return $count; # return count to controller
}
As you are developing in codeigniter better user its inbuilt method to get and post data. For authentication u can create one library which will be autoload and check for session like userid if not found then redirect user to login page . You can create one array in that library which will defines the public / authenticated pages on bases on which you prevent user from accessing authenticated pages. You can try this for Controller :
class Login_control extends CI_Controller
{
public function index()
{
$this->load->model('login_model');
$this->load->helper('url');
$Logusername = $this->input->post('Logusername', true);
$Logpassword = $this->input->post('Logpassword', true);
if (!empty($Logusername) && !empty($Logpassword)) {
$user = $Logusername;
$pass = $Logpassword;
$data = $this->login_model->authenticate($user, $pass);
if ($data == TRUE) {
/* User this flashdata to display the message after redirect */
$this->session->set_flashdata('success', 'Logged in successfully');
redirect(site_url("dashboard"));
} else {
/* User this flashdata to display the message after redirect */
$this->session->set_flashdata('success', 'Wrong Username/password');
redirect(site_url());
}
}
$this->load->view('signup_view');
}
}
For Model
public function authenticate($Logusername, $Logpassword)
{
$select_col = "iUserId";
$where = "`vUserName` =?"; //*
$where.=" AND BINARY `vPassword`=?";
$sql = "SELECT " . $select_col . " FROM user WHERE " . $where . "";
$result = $this->db->query($sql, array($Logusername, $Logpassword))->result_array();
if (is_array($result) && count($result) > 0) {
/* Create Session and store userid */
$this->session->set_userdata("iUserId", $result[0]["iUserId"]);
return TRUE;
} else {
return FALSE;
}
}
There are many ways to authenticate the user. Check for hooks in Codeigniter
HOw do I pass $variable from comments() to someFunction()?
class Blog extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
public function comments()
{
$variable = "Hello";
}
public function someFunction()
{
echo $variable;
}
}
** EDIT ** Feel free to point out any other mistakes if you wish
class Home extends CI_Controller {
private $idArray;
function __construct()
{
parent::__construct();
$this->load->model('home_model');
$this->load->library('tank_auth');
$this->load->library('form_validation');
}
public function index() {
$home_data['initial_two'] = $this->home_model->get_two_brands();
$home_data['user_id'] = $this->tank_auth->get_user_id();
$home_data['username'] = $this->tank_auth->get_username();
$this->load->view('home_view', $home_data);
}
public function get_two() {
$get_results = $this->home_model->get_two_brands();
if($get_results != false){
$html = '';
foreach($get_results as $result){
$html .= '<li>'.$result->brand.'</li>';
}
list($result1, $result2) = $get_results;
$idOne = $result1->id;
$idTwo = $result2->id;
$this->idArray = array($result1->id, $result2->id);
//var_dump($this->idArray);
$result = array('status' => 'ok', 'content' => $html);
header('Content-type: application/json');
echo json_encode($result);
exit();
}
}//public function get_two() {
function user_pick() {
$this->form_validation->set_rules('pick', 'Pick', 'required|trim|integer|xss_clean');
$this->form_validation->set_rules('notPick', 'Not Pick', 'required|trim|integer|xss_clean');
//$arr = $this->idArray;
var_dump($this->idArray); // This is NULL
$pick = $_POST['pick'];
$notPick = $_POST['notPick'];
$user_id = $this->tank_auth->get_user_id();
if ($this->form_validation->run() == FALSE)
{
$result = array('status' => 'no', 'content' => "No good!");
header('Content-type: application/json');
echo json_encode($result);
exit();
}else{//if ($this->form_validation->run() == FALSE || $do_input == NULL)
$upload = $this->home_model->user_pick($user_id, $pick, $notPick);
$result = array('status' => 'ok', 'content' => "Thank you!");
header('Content-type: application/json');
echo json_encode($result);
exit();
}//if ($this->form_validation->run() == FALSE || $do_input == NULL)
}
}//class Home extends CI_Controller { closing bracket
/* End of file home.php */
/* Location: ./application/controllers/home.php */
class Blog extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
public function comments()
{
$_SESSION['variable'] = Array('k1'=>'v1','k2'=>'v2') ;
// Store the variable in session so it can be called
// in another page or ajax call
}
public function display()
{
echo $_SESSION['variable'] ;
}
}
// my index.php file
var $blog = new Blog() ;
$blog->comments() ;
//another ajax_called.php file, call in ajax on in another browser tab
var $blog = new Blog() ;
$blog->display() ;
Right now $renderData['username'] won't pass through to the view.
class HomeController extends MY_Controller {
public function index($renderData=""){
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$renderData['username'] = $session_data['username'];
//$this->load->view('pages/home_view', $renderData);
$this->_render('pages/home',$renderData);
}
else
{
//If no session, redirect to login page
redirect('LoginController', 'refresh');
}
}
}
The error I get is...
Which alludes to this code...
<h1>Home</h1>
<h2>Welcome <?php echo $username; ?>!</h2>
Logout
Here is my My_Controller where the _render function is located...
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller{
//Page info
protected $data = Array();
protected $pageName = FALSE;
protected $template = "main";
protected $hasNav = TRUE;
//Page contents
protected $javascript = array();
protected $css = array();
protected $fonts = array();
//Page Meta
protected $title = FALSE;
protected $description = FALSE;
protected $keywords = FALSE;
protected $author = FALSE;
function __construct()
{
parent::__construct();
$this->data["uri_segment_1"] = $this->uri->segment(1);
$this->data["uri_segment_2"] = $this->uri->segment(2);
$this->title = $this->config->item('site_title');
$this->description = $this->config->item('site_description');
$this->keywords = $this->config->item('site_keywords');
$this->author = $this->config->item('site_author');
$this->pageName = strToLower(get_class($this));
}
protected function _render($view,$renderData="FULLPAGE") {
switch ($renderData) {
case "AJAX" :
$this->load->view($view,$this->data);
break;
case "JSON" :
echo json_encode($this->data);
break;
case "FULLPAGE" :
default :
//static
$toTpl["javascript"] = $this->javascript;
$toTpl["css"] = $this->css;
$toTpl["fonts"] = $this->fonts;
//meta
$toTpl["title"] = $this->title;
$toTpl["description"] = $this->description;
$toTpl["keywords"] = $this->keywords;
$toTpl["author"] = $this->author;
//data
$toBody["content_body"] = $this->load->view($view,array_merge($this->data,$toTpl),true);
//nav menu
if($this->hasNav){
$this->load->helper("nav");
$toMenu["pageName"] = $this->pageName;
$toHeader["nav"] = $this->load->view("template/nav",$toMenu,true);
}
$toHeader["basejs"] = $this->load->view("template/basejs",$this->data,true);
$toBody["header"] = $this->load->view("template/header",$toHeader,true);
$toBody["footer"] = $this->load->view("template/footer",'',true);
$toTpl["body"] = $this->load->view("template/".$this->template,$toBody,true);
//render view
$this->load->view("template/skeleton",$toTpl);
break;
}
}
}
Here is an additional function that may be helpful...
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 to login page
$this->load->view('pages/login_view');
}
else
{
//Go to private area
// redirect('home', 'refresh');
redirect('HomeController', 'refresh');
}
}
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
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
}
?>
What am I doing wrong that is preventing the username to be passed to the view?
PS - let me know any additional documentation I can provide.
something like this
protected function _render($view,data, $renderData="FULLPAGE") {
$this->data = $data;
switch ($renderData) {
case "AJAX" :
$this->load->view($view,$this->data);
break;
case "JSON"