I've searched the ends of the earth and back for this solution, and for everyone else, the fix was to autoload the database class in autoload.php. However, I am already doing this, so I don't know where else to go with it.
Controller:
class Login extends CI_Controller{
function validate_credentials(){
$this->load->model('membership_model');
// call the member function from membership_model.php
$q = $this->membership_model->validate();
if($q){
// user info is valid
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('site/members_area');
}else{
$this->index();
}
}
}
Model:
class Membership_model extends CI_Model{
function validate(){
// this method is called from the login.php controller
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$q = $this->db->get('members');
if($q->num_rows == 1){
return true;
}
return false;
}
}
Any help on this please, I'm going round in circles with this
My next thought is that php.ini needs configuring on my host server, to load the mysql modules - but I would very surprised by this?
Just to clarify - the line:
if($q->num_rows)
Is the problem, saying that I am trying to reference a property of a non object
Actually
if($q->num_rows)
should be
if($q->num_rows()) // It returns the number of rows returned by the query.
So you can use
if($q->num_rows()) // In this case it'll be evaluated true if rows grater than 0
{
// code when rows returned
}
num_rows is a function.
if ($query->num_rows() > 0) {
}
So you need
if($q->num_rows() > 0) {
return true;
}
Solved - I hadn't assigned a User to the database, therefore couldn't access it.
Relief
Related
While trying to delete rows by id from db as below , I am getting the error- count(): Parameter must be an array or an object that implements Countable. I think no id is passed to the delete() function in my code and am not sure how to pass it. Please help me with ur suggestions.
Controller
public function delete($id){
$data = PersonalDetails::where('id',$id)->delete();
// dd($data);
if(count($data)){
return response()->json(['message'=>'Successfully Deleted']);
}
else{
return response()->json(['message'=>'Delete Failed']);
}
}
Route
Route::group([
'namespace'=>'App\Http\Controllers',
'middleware' => 'api',
], function ($router) {
Route::post('delete/{id}', 'PersonalDetailsAdmin#delete');
}
The ->delete() method already returns the count of the deleted rows, so the solution would be:
public function delete($id){
$count = PersonalDetails::where('id',$id)->delete();
// dd($data);
if($count > 0 ){
return response()->json(['message'=>'Successfully Deleted']);
}
else{
return response()->json(['message'=>'Delete Failed']);
}
}
enter image description here
Im new in Laravel i don't how to figure out the mistake
here is my laravel code:
public function login(Request $request)
{
$gen_user = GenUser::where('username',$request->username)->where('confirmed',0)->first();
if($gen_user){
return response()->json(3);+6
}
if($this->auth->attempt($request->only('username','password'))){
$gen_user = GenUser::join('gen_people','gen_users.gen_person_id','=','gen_people.id')
->where('gen_users.id',\Auth::user()->id)
->select(['gen_people.first_name','gen_people.middle_name','gen_people.last_name','gen_people.first_name','gen_users.id','gen_users.username','gen_users.confirmed'])
->first();
// dd($gen_user);
$gen_user->name = $gen_user->first_name." ".$gen_user->middle_name." ".$gen_user->last_name;
if($gen_user->confirmed == 1){
return response()->json($gen_user);
}elseif($gen_user->confirmed == 2){
return response()->json(2);
}else{
return response()->json(3);
}
}else{
return response()->json(false);
}
}
Can someone help me...thanks in advance...
When you use this keyword you mean refer to exactly current class.but in your that controller there is not any auth method or property.
Create authentication(or if you use passport it make it automaticly) and migrate.
After that you can use
Auth::attempt(['username'=>request('username'),'password'=>request('password')])
I have the following code I added to a MY_Controller that is being extended by all my controllers:
public function _remap($method, $params = array())
{//exit($this->router->fetch_class());
if (array_search($method, $this->private_methods) !== false && !$this->logged_in)
{
$this->session->set_flashdata('message', array(
'message' => 'You must login to access the requested area',
'error' => 1
)
);
redirect('/');
}
else if (method_exists($this, $method))
{
$this->$method($params);
}
else
{
redirect('/');
}
}
The issue being created is that the call $this->$method($params) is condensing the params in to an Array. So a method such as the following breaks:
function some_method($param1, $param2, $param3)
Is there a way to break this array back into individual items for functions like this?
I was trying to do the same and find the same problem with
$this->$method($params);
I found another option
call_user_method_array($method,$this,$params);
Which worked but is already deprecated.
Function in PHP deprecated, what should I use now?
But hopefully that is the new one.
call_user_func_array(array($this,$method), $params);
I have tried this and it worked for me
public function _remap($method,$params = array())
{
if (method_exists($this, $method)) {
if($method == 'delete_photo' || $method == 'delete_video'){
call_user_func_array(array($this,$method), $params);
}
else{
$this->$method();
}
}
else {
$this->index($method);
}
}
Now you just have to replace "delete_photo" & "delete_video" with your methods names that contains parameters, and of course you can add as much methods as you want.
I'm using the count_all_results() function to return a user's number of languages spoken. But when I try to pass the number to the view, I keep getting a php undefined variable (for $lang_cnt). Below is my code:
Model
function countLanguages($id) {
$this->db->where('user_id', $id)->from('languages');
return $this->db->count_all_results();
}
Controller
function showLangCount() {
$data['lang_cnt'] = $this->language_model->countLanguages($id);
$this->load->view('lang_view', $data);
}
View
<p>This user speaks <?php echo $lang_cnt; ?> languages.</p>
One problem is that your model function takes two arguments:
function countLanguages($id, $cnt_languages)
But when you call it you are only passing one argument:
$this->language_model->countLanguages($cnt_languages);
And an even bigger problem, as Rocket points out, is that countLanguages doesn't return anything. Try this:
function countLanguages($id) {
$this->db->where('user_id', $id)->from('languages');
return $this->db->count_all_results();
}
Always check your model functions if they return value or not. Try this:
function showLangCount() {
if($this->language_model->countLanguages($id))
{
$data['lang_cnt'] = $this->language_model->countLanguages($id);
}
else
{
$data['lang_cnt'] = NULL;
}
$this->load->view('lang_view', $data);
}
Its better to use:
return $query->num_rows();
to return the number of rows effected...
I searched the whole Internet and either there is no one mentioning my problem, or I'm stupid, or maybe it's just a bad day for coding.
What's the situation:
controller "source"
model "source"
model "login"
The "login" model is loaded from autoload.php, then in each controller's constructor I have $this->login->check(), which is checking if the user is logged in (obviously). Then in some of the methods I'm using the "source" model to connect to the database.
I tried loading both of the models from the autoload array, I also tried to load them in the way described here, but it's obviously for an old CI version (the thread is from 2008) and I tried all the possible ways I had in my mind.
Anyway, the result is this:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Source::$login
Filename: controllers/source.php
Line Number: 10
Fatal error: Call to a member function check() on a non-object in ...\application\controllers\source.php on line 10
Any ideas what I'm missing or how to fix it...? I'm stuck for hours and I don't have any ideas what I could do...
Edit 1: here is the code from the "source" controller:
class Source extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('login');
$this->login->check();
}
function index() {
// Pagination config, getting records from DB
$this->load->view('templates/layout', $data);
}
function add() {
$this->load->model('source', '', true);
$btn = $this->input->post('btn');
if(isset($btn)) {
// More form validation
if($this->form_validation->run() == TRUE) {
if($btn == "Add") {
// here I am supposed to use the source model...
}
}
}
$data['page'] = 'source_add';
$this->load->view('templates/layout', $data);
}
}
?>
Edit 2: login.php:
<?php
class Login extends CI_Model {
function __construct() {
parent::__construct();
}
function authenticate($username, $password) {
// the login script comes here
}
function logged() {
if($this->session->userdata('logged') == true) {
return true;
} else return false;
}
function check() {
if(!$this->logged()) {
redirect('/authentication');
}
}
}
?>
Conventionally, the classname of Models should end with _model, so it not collides with controllers with the same name, so try changing
class Login extends CI_Model {
to
class Login_model extends CI_Model {
I resolved this issue by utilizing the hooks and turned the login process into a controller, thereby being able to access user information and setting access levels.
First I added the following to the hooks.php file in the config folder
$hook['post_controller_constructor'][] = array('function' => 'check_login','filename' => 'authority.php','filepath' => 'hooks');
Then I have the following functions in a hook file called authority.php
[EDIT]Having reviewed this I am going to change it to a pre_controller_constructor and see if I can remove what seems to be a double page flash on initial construct.[/EDIT]
function check_login(){
$CI =& get_instance();
$is_logged_in = $CI->session->userdata('is_logged_in');
if(!$is_logged_in){
$unauth_pages = array(your unauthorized pages go here);
if(!in_array($CI->router->class,$unauth_pages)){
$CI->session->set_userdata('before_login_url',current_url());
redirect('login');
}
}
}
function check_authority(){
$CI =& get_instance();
if($CI->session->userdata('usergroupID') == 'SUPADMIN'){return;}
$page = $CI->router->class ;
$method = $CI->router->method;
$method = ($method=='index')?'':$method;
$unauth_pages = array(your unauthorized pages go here);
if(in_array($page,$unauth_pages))return;
$user_group = $CI->session->userdata('usergroupID');
$CI->load->model('user_model');
if($user_group == 'ADMIN' || $user_group == 'USER'){
if($CI->session->userdata('timezone') == ''){
date_default_timezone_set('Canada/Pacific');
} else {
date_default_timezone_set($CI->session->userdata('timezone'));
}
}
if( !$CI->user_model->authorized_content($CI->session->userdata('usergroupID'),$page, $method)){
redirect('unauthorized');
}
}
With the above I dont have to worry about checking on each page but instead utilize the ci framework to do the checking for me.. if its not in the unauth page array then it is a page that requires authorization checking.
Hope this works for you.