How Can I Solve the issue When I am running this code I am getting the following error I have already loaded session library still i am getting this error
This is my controller
public function __construct(){
parent::__construct();
}
public function index() {
$this->load->database();
$this->load->helper('form', 'url');
$this->load->library('session');
}
public function process() {
$this->load->model('login_model');
$result = $this->login_model->validate();
if (!$result) {
$msg = '<font color=red>Invalid username and/or password.</font><br />';
$this->load->view(base_url().'index',$msg);
} else {
if ($_SESSION['utype'] == 2 || $_SESSION['utype'] == 1) {
redirect(base_url() . 'admin/dashboard');
}
if ($_SESSION['utype'] == 3 && !empty($_POST['urlValue'])) {
redirect($_POST['urlValue']);
}
if (empty($_POST['urlValue'])) {
redirect('/');
}
}
}
This is my model
public function validate() {
$username = $this->security->xss_clean($this->input->post('email'));
$password = $this->security->xss_clean($this->input->post('password'));
$pwd = base64_encode($password);
$this->db->where('email', $username);
$this->db->where('password', $pwd);
$query = $this->db->get('adduser');
$row = $query->row();
if (count($row) > 0) {
$row = $query->row();
$data = array(
'uid' => $row->id,
'uname' => $row->firstname,
'utype' => $row->usertype,
'uemail' => $row->email,
'validated' => true
);
$this->session->set_userdata($data);
return $row;
}
return $row;
}
I am getting this problem:
Severity: Notice
Message: Undefined property: Index::$session
Filename: core/Model.php
Line Number: 77
Backtrace:
File: D:\xampp\htdocs\savepaise\application\models\Login_model.php
Line: 42
Function: __get
File: D:\xampp\htdocs\savepaise\application\controllers\Index.php
Line: 31
Function: validate
File: D:\xampp\htdocs\savepaise\index.php
Line: 315
Function: require_once
Fatal error: Call to a member function set_userdata() on a non-object in
D:\xampp\htdocs\savepaise\application\models\Login_model.php on line 42
A PHP Error was encountered
Severity: Error
Message: Call to a member function set_userdata() on a non-object
Filename: models/Login_model.php
Line Number: 42
I think you need to load all library inside the controller constructor function not only in index function
public function __construct(){
parent::__construct();
$this->load->database();
$this->load->helper('form', 'url');
$this->load->library('session');
}
Update 1 : you can use autoload like this
Go to applications/config/autoload.php and in there you can edit what you need.
They are in arrays and seperated by packages, libraries, helpers, config, languages and models.
Example
$autoload['libraries'] = array('database', 'session');
$autoload['helper'] = array('url', 'html', 'form');
Related
Wanted to seek a help on these errors.
controller name: Account_login.php
model name: account_login_model.php
Message: Undefined property: Account_login::$login
Filename: controllers/account_login.php
Line Number: 34
Backtrace:
File: C:\xampp\htdocs\labexercise009\application\controllers\account_login.php
Line: 34
Function: _error_handler
File: C:\xampp\htdocs\labexercise009\application\controllers\account_login.php
Line: 21
Function: run
File: C:\xampp\htdocs\labexercise009\index.php
Line: 315
Function: require_once
Call to a member function model() on a non-object
Message: Call to a member function model() on null
Filename: C:\xampp\htdocs\labexercise009\application\controllers\account_login.php
Line Number: 34
Backtrace:
File: C:\xampp\htdocs\labexercise009\application\controllers\account_login.php
Line: 21
Function: run
File: C:\xampp\htdocs\labexercise009\index.php
Line: 315
Function: require_once
Here's my code:
Controller
?php
class Account_login extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$data['title'] = 'Account Login';
$this->load->view('account_login', $data);
}
public function verify()
{
$this->form_validation->set_rules('txtuser', 'Username', 'required');
$this->form_validation->set_rules('txtpass', 'Password', 'required|callback_check_user');
if ($this->form_validation->run() == TRUE) {
echo 'Success';
} else {
$this->index();
}
}
public function check_user()
{
$username = $this->input->post('txtuser');
$password = $this->input->post('txtpass');
$this->login->model('account_login_model');
$login = $this->account_login_model->login($username, $password);
if ($login) {
return true;
} else {
if (isset($_SESSION['error_count'][$username])) {
$_SESSION['error_count'][$username] += 1;
} else {
$_SESSION['error_count'][$username] = 1;
}
$isBlocked = $this->account_login_model->isBlocked($username);
if ($isBlocked) {
$this->form_validation->set_message('check_user', 'Account is temporarily blocked.');
} else if (isset($_SESSION['error_count'][$username]) && $_SESSION['error_count'][$username] > 2) {
$this->account_login_model->block($username);
$this->form_validation->set_message('check_user', '3 consecutive failed login attempts. Account Blocked.');
} else {
$this->form_validation->set_message('check_user', 'Invalid Username/Password');
}
return false;
}
}
}
Model
<?php
class Account_login_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function login($username, $password)
{
$condition_array = array(
'user_name' => $username,
'user_pass' => $password
);
$rs = $this->db->get_where('users', $condition_array);
$row_count = count($rs->row_array());
if ($row_count > 0) {
return $rs->row_array();
} else {
return FALSE;
}
}
public function isBlocked($username)
{
$condition_array = array(
'user_name' => $username,
'acc_isBlocked' => 1
);
$rs = $this->db->get_where('accounts', $condition_array);
$row_count = count($condition_array);
if ($row_count > 0) {
return true;
} else {
return FALSE;
}
}
public function block($username)
{
$this->load->library('email');
$email = $this->account_lookup($username, 'acc_email');
$this->email->from('lslayugan#feutech.edu.ph', 'Your Website');
$this->email->to($email);
$this->email->subject('Account Blocked');
$message = $this->load->view('account_blocked', null, TRUE);
$this->email->message($message);
$this->email->send();
$this->db->where('acc_username', $username);
return $this->db->update('accounts', array('acc_isBlocked' => 1));
}
public function account_lookup($username, $return)
{
$rs = $this->db->get_where('account', array('acc_username' => $username));
$row = $rs->row();
return $row->$return;
}
}
might be change like this
$this->load->model('account_login_model');
instead of
$this->login->model('account_login_model');
I have a difficulty on my program. The error say :
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_DB_mysqli_result::$level
Filename: controllers/Auth.php
Line Number: 30
Backtrace:
File:
C:\xampp\htdocs\PKLTelkom\Telkom2\application\controllers\Auth.php
Line: 30 Function: _error_handler
File: C:\xampp\htdocs\PKLTelkom\Telkom2\index.php Line: 315 Function:
require_once
Here's my controller named "Auth" :
<?php
/**
*
*/
class Auth extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('m_login');
}
public function index()
{
$this->load->view("login");
}
public function AksiLogin()
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$passwordx = md5($password);
$login = $this->m_login->data_login($username, $passwordx);
$tes = count($login);
if ($tes > 0) {
//ambil detail data
$row = $this->m_login->data_login($username, $passwordx);
$level = $row->level;
//daftarkan session
$data_session = array('level' => $level);
$this->session->set_userdata($data_session);
//direct page
if ($level == 'superadmin') {
redirect('superadmin');
}
else if ($level == 'admin') {
redirect('admin');
}
}
else {
$this->index();
}
}
public function logout()
{
$this->session->unset_userdata("login");
$this->session->unset_userdata("username");
redirect ('index.php/auth');
}
}
?>
Here's my models named "M_Login" :
<?php
class M_login extends CI_Model
{
function data_login($username, $password)
{
$this->db->where('username', $username);
$this->db->where('password', $password);
return $this->db->get('akun');
}
}
?>
Looks like your return $this->db->get('akun'); on "M_login" model doesn't return object with the name level.
Try changing this line on "M_Login" model :
<?php
class M_login extends CI_Model
{
function data_login($username, $password)
{
$this->db->where('username', $username);
$this->db->where('password', $password);
return $this->db->get('akun')->row(); // change this line
}
}
?>
I use CakePHP 1.3
I'm trying to learn CakePHP and I was trying to redirect a controller. But I get a warning and I can't do redirecting to other action at all.
Here's the printed warning
Warning (2): array_merge() [function.array-merge]: Argument #2 is not an array [CORE/cake/libs/router.php, line 1078]
Warning (2): array_merge() [function.array-merge]: Argument #1 is not an array [CORE/cake/dispatcher.php, line 158]
Warning (2): call_user_func_array() expects parameter 2 to be array, string given [CORE/cake/dispatcher.php, line 204]
Here's my login_controller.php for sample of the controller which doing redirect to other action
<?php
class LoginController extends AppController {
var $uses = array();
function index() {
$result = 'empty';
if(isset($this->data)) {
$user = $this->data['user_txt'];
$pass = md5($this->data['pass_txt']);
$action = array('controller'=>'users','action'=>'login');
$params = array('user'=>$user,'pass'=>$pass);
$get_login = $this->requestAction($action,$params);
if($get_login===true) {
$result = 'exist';
}
}
$this->set('result', $result);
}
}
?>
And this is user_controller.php code
<?php
class UsersController extends AppController {
function index() {
}
function login() {
$user = $this->request->params['user'];
$pass = $this->request->params['pass'];
$query = $this->User->find('all', array(
'conditions' => array('id'=>$user,'password'=>$pass)
));
$this->layout = false;
if (isset($query) && !empty($query)) {
return true;
}
else {
return compact($query);
}
}
}
?>
Any help can do, thanks
I have a model which returns a username of the person that has logged into the website to a controller. I am trying to save the username into a variable which i can user to then insert back into another table, however i am having no luck saving the data. Below is my model and controller classes.
Model:
function is_loggedin()
{
$session_id = $this->session->userdata('session_id');
$res = $this->db->get_where('logins',array('session_id' => $session_id));
if ($res->num_rows() == 1) {
$row = $res->row_array();
return $row['name'];
}
else {
return false;
}
}
Part of my Controller:
public function index()
{
$loggedin = $this->authlib->is_loggedin();
if ($loggedin === false)
$this->load->view('login_view',array('errmsg' => ''));
else
{
$this->load->view('postquestion_view',array('username' => $loggedin));
$user = $loggedin['username'];
}
}
public function askquestion()
{
$qtitle = $this->input->post('title');
$qdetails = $this->input->post('details');
$qtags = $this->input->post('tags');
$qcategory = $this->input->post('category');
$quser = $user;
Error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: user
Filename: controllers/postq.php
Line Number: 47
Here the error message is very clear. The variable $user in the last line of the function -action- askquestion() snippet is not defined. Basically, you have to read more about variables scope.
In your current situation, the code of index action should be in constructor and the variable user should be an object property. i.e it should defined globally in your controller's class and then takes its value from the constructor something like the following general demo:
<?php
class Blog extends CI_Controller {
public $user = false;
public function __construct()
{
parent::__construct();
// Your own constructor code
}
public function askquestion()
{
$qtitle = $this->input->post('title');
$qdetails = $this->input->post('details');
$qtags = $this->input->post('tags');
$qcategory = $this->input->post('category');
$quser = $this->user; //NOTICE THIS LINE
}
?>
I'm working on a model that tracks user data and stores it in a session, where appropriate. Here's the basic structure of it:
public function __construct() {
parent::__construct();
$this->load->database();
$this->load->library('session');
if (!is_null($this->session->userdata('user'))) {
$arrData = $this->session->userdata('user');
$this->_netID = $arrData['_netID'];
$this->_fName = $arrData['_fName'];
$this->_eventMgr = $arrData['_eventMgr'];
$this->_accessMgr = $arrData['_accessMgr'];
$this->_accessAcnt = $arrData['_accessAcnt'];
$this->_accessEvnt = $arrData['_accessEvnt'];
$this->_accessCMS = $arrData['_accessCMS'];
$this->_accessReg = $arrData['_accessReg'];
$this->_accessRep = $arrData['_accessRep'];
$this->_accessPay = $arrData['_accessPay'];
$this->_okEvents = $arrData['_okEvents'];
}
}
public function __get($name) {
switch($name) {
default:
if (function_exists('parent::__get')) {
return parent::__get($name);
} else {
return $this->$name;
}
}
}
public function __set($name,$val) {
switch($name) {
default:
if (function_exists('parent::__set')) {
return parent::__set($name,$val);
} else {
$this->$name = $val; }
}
}
The issue I'm having is right in the constructor. Whenever it hits the seventh line (checking if the user key is null) it errors out, saying:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: SessionUser::$session
Filename: models/sessionuser.php
Line Number: 83
Fatal error: Call to a member function userdata() on a non-object in /usr/cwis/data/www-data/melioraweekenddev/system/application/models/sessionuser.php on line 38
Any ideas on why?
My guess is you're trying to load a library from within a model which is not directly possible.
Try instead:
public function __construct() {
parent::__construct();
$CI =& get_instance(); //Loads the codeigniter base instance (The object your controller is extended from. & for php4 compatibility
$CI->load->database();
$CI->load->library('session');
if (!is_null($CI->session->userdata('user'))) {
...