I have made a codeigniter user login library but have run into a small issue. When I login it is not returning any of the functions in the library like $this->get_user_username();
On my dashboard I should be able to do this echo $this->user->get_user_username();
I think I know problem is because the if ($this->validate_password($password)) { is around the query. And stops
me returning any data.
Note: I only would like to be able to set the user_id in session which is working. And just return the others as variables etc.
Question Because I am using if ($this->validate_password($password)) { in my login and once logged in it blocks me from retrieving any data what would be best solution so can still use it and retrieve my data
With out setting it all in sessions as I said I only want user id set
in sessions.
Dashboard Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dashboard extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->library('admin/user');
}
public function index()
{
echo $this->user->get_user_username();
print_r($_SESSION);
$data['top_navbar'] = Modules::run('admin/common/top_navbar/index');
$data['column_left'] = Modules::run('admin/common/column_left/index');
$data['header'] = Modules::run('admin/common/header/index');
$data['footer'] = Modules::run('admin/common/footer/index');
$this->load->view('common/dashboard_view', $data);
}
}
User Library
<?php
class User {
private $user_id;
private $username;
public function __construct() {
$this->CI =& get_instance();
}
public function login($username, $password) {
if ($this->validate_password($password)) {
$this->CI->db->where('username', $username);
$user_query = $this->CI->db->get($this->CI->db->dbprefix . 'user');
if ($user_query->num_rows() > 0) {
$row = $user_query->row();
$this->CI->session->set_userdata(array('user_id' => $row->user_id)); // Working
$this->user_id = $row->user_id;
$this->user_group_id = $row->user_group_id;
$this->username = $row->username;
$this->firstname = $row->firstname;
$this->lastname = $row->lastname;
$this->email = $row->email;
return true;
} else {
return false;
}
} else {
return false;
}
}
public function logout() {
$this->CI->session->unset_userdata('user_id');
$this->user_id = '';
$this->username = '';
}
public function get_user_id() {
return $this->user_id;
}
public function get_user_group_id() {
return $this->user_group_id;
}
public function get_user_username() {
return $this->username;
}
public function get_user_firstname() {
return $this->firstname;
}
public function get_user_lastname() {
return $this->lastname;
}
public function get_user_email() {
return $this->email;
}
public function validate_password($password) {
if (password_verify($password, $this->stored_hash())) {
return $password;
} else {
return false;
}
}
public function stored_hash() {
$this->CI->db->where('username', $this->CI->input->post('username'));
$query = $this->CI->db->get($this->CI->db->dbprefix . 'user');
if ($query->num_rows() > 0) {
$row = $query->row();
return $row->password;
} else {
return false;
}
}
}
Problem Solved I thought post my own answer, I had to add another section in the construct area of user library to make it work
And then use session userdata for where check.
public function __construct() {
$this->CI =& get_instance();
if ($this->CI->session->userdata('user_id') == TRUE) {
$this->CI->db->where('user_id', $this->CI->session->userdata('user_id'));
$user_query = $this->CI->db->get($this->CI->db->dbprefix . 'user');
if ($user_query->num_rows() > 0) {
$row = $user_query->row();
$this->user_id = $row->user_id;
$this->user_group_id = $row->user_group_id;
$this->username = $row->username;
$this->firstname = $row->firstname;
$this->lastname = $row->lastname;
$this->email = $row->email;
return true;
} else {
return false;
}
}
}
Library
<?php
class User {
private $user_id;
private $username;
public function __construct() {
$this->CI =& get_instance();
if ($this->CI->session->userdata('user_id') == TRUE) {
$this->CI->db->where('user_id', $this->CI->session->userdata('user_id'));
$user_query = $this->CI->db->get($this->CI->db->dbprefix . 'user');
if ($user_query->num_rows() > 0) {
$row = $user_query->row();
$this->user_id = $row->user_id;
$this->user_group_id = $row->user_group_id;
$this->username = $row->username;
$this->firstname = $row->firstname;
$this->lastname = $row->lastname;
$this->email = $row->email;
return true;
} else {
return false;
}
} else {
$this->logout();
}
}
public function login($username, $password) {
if ($this->validate_password($password)) {
$this->CI->db->where('username', $username);
$user_query = $this->CI->db->get($this->CI->db->dbprefix . 'user');
if ($user_query->num_rows() > 0) {
$row = $user_query->row();
$this->CI->session->set_userdata(array('user_id' => $row->user_id));
$this->user_id = $row->user_id;
$this->user_group_id = $row->user_group_id;
$this->username = $row->username;
$this->firstname = $row->firstname;
$this->lastname = $row->lastname;
$this->email = $row->email;
return true;
} else {
return false;
}
} else {
return false;
}
}
public function logout() {
$this->CI->session->unset_userdata('user_id');
$this->user_id = '';
$this->username = '';
}
public function get_user_id() {
return $this->user_id;
}
public function get_user_group_id() {
return $this->user_group_id;
}
public function get_user_username() {
return $this->username;
}
public function get_user_firstname() {
return $this->firstname;
}
public function get_user_lastname() {
return $this->lastname;
}
public function get_user_email() {
return $this->email;
}
public function validate_password($password) {
if (password_verify($password, $this->stored_hash())) {
return $password;
} else {
return false;
}
}
public function stored_hash() {
$this->CI->db->where('username', $this->CI->input->post('username'));
$query = $this->CI->db->get($this->CI->db->dbprefix . 'user');
if ($query->num_rows() > 0) {
$row = $query->row();
return $row->password;
} else {
return false;
}
}
}
Related
Hello I am making a cart but when I click on add to cart link then it says:
Undefined property: App\Cart::$totalPrice
Error: https://ibb.co/ysB5CfG
model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Cart
{
private $contents;
private $totalQty;
private $contentsPrice;
public function __construct($oldCart){
if ($oldCart) {
$this->contents = $oldCart->contents;
$this->totalQty = $oldCart->totalQty;
$this->totalPrice = $oldCart->totalPrice;
}
}
public function addProduct($product, $qty){
$products = ['qty' => 0, 'price' => $product->price, 'product' => $product];
if ($this->contents) {
if (array_key_exists($product->slug, $this->contents)) {
$product = $this->contents[$product->slug];
}
}
$products['qty'] +=$qty;
$products['price'] +=$product->price * $product['qty'];
$this->contents[$product->slug] = $product;
$this->totalQty+=$qty;
$this->totalPrice += $product->price;
}
public function getContents()
{
return $this->contents;
}
public function getTotalQty()
{
return $this->totalQty;
}
public function getTotalPrice()
{
return $this->totalPrice;
}
}
controller:
public function cart()
{
if (!Session::has('cart')) {
return view('products.cart');
}
$cart = Session::has('cart');
return view('product.cart', compact('cart'));
}
public function addToCart(Product $product, Request $request, $qty= null)
{
if(empty(Auth::user()->email)){
$data['email'] = '';
}else{
$data['email'] = Auth::user()->email;
}
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$qty = $request->qty ? $request->qty : 1;
$cart = new Cart($oldCart);
$cart->addProduct($product, $qty);
Session::put('cart', $cart);
return redirect()->back()->with('flash_message_success', 'Product $product->title has been successfully added to Cart');
}
routes:
Route::get('cart', 'Admin\ProductController#cart')->name('product.cart');
// Add to cart
Route::get('/addToCart/{product}/{qty?}', 'Admin\ProductController#addToCart')->name('addToCart');
You should use get() methods in cart() function in controller file.
public function cart()
{
if (!Session::has('cart')) {
return view('products.cart');
}
$cart = Session::get('cart');
return view('product.cart', compact('cart'));
}
On my multiple upload library, I have a set error function.
On my upload function I use a in_array to check file extensions. If the in_array detects error it displays multiple error messages correct.
The problem I am having is for some reason when I use return FALSE; under the $this->set_error('file_extension_not_allowed') then will on display one message. Not sure why return FALSE limits error messages.
Question: How is it possible to use my return false but be able to display multiple message correct.
<?php
class Multiple_upload {
public $set_errors = array();
public function __construct($config = array()) {
$this->CI =& get_instance();
$this->files = $this->clean($_FILES);
empty($config) OR $this->set_config($config);
}
public function set_config($config) {
foreach ($config as $key => $value) {
$this->$key = $value;
}
return $this;
}
public function upload($field = 'userfile') {
$allowed_extension = explode('|', $this->allowed_types);
if (empty($this->upload_path)) {
$this->set_error('upload_path_not_set', 'upload_path_check');
return FALSE;
}
if (!realpath(FCPATH . $this->upload_path)) {
$this->set_error('upload_path_in_correct', 'location_check');
return FALSE;
}
if (!empty($this->files[$field]['name'][0])) {
foreach ($this->files[$field]['name'] as $key => $value) {
$this->file_name = $this->files[$field]['name'][$key];
$get_file_extension = explode('.', $this->files[$field]['name'][$key]);
$this->get_file_extension_end = strtolower(end($get_file_extension));
$array_1 = array(
$allowed_extension,
);
$array_2 = array(
$get_file_extension[1],
);
if (!in_array($array_2, $array_1)) {
$this->set_error('file_extension_not_allowed', 'extension_check');
return FALSE;
}
}
return $this;
}
}
public function set_error($message, $type) {
$this->CI->lang->load('upload', 'english');
$this->error_message[] = $this->CI->lang->line($message);
return $this;
}
public function display_error_messages($open_tag = '<p>', $close_tag = '</p>') {
foreach($this->error_message as $msg) {
var_dump($msg);
}
}
public function clean($data) {
if (is_array($data)) {
foreach ($data as $key => $value) {
unset($data[$key]);
$data[$this->clean($key)] = $this->clean($value);
}
} else {
$data = htmlspecialchars($data, ENT_COMPAT, 'UTF-8');
}
return $data;
}
}
Maybe this can help...
public function upload($field = 'userfile')
{
$allowed_extension = explode('|', $this->allowed_types);
if (empty($this->upload_path))
{
$this->set_error('upload_path_not_set', 'upload_path_check');
return FALSE;
}
if (!realpath(FCPATH . $this->upload_path))
{
$this->set_error('upload_path_in_correct', 'location_check');
return FALSE;
}
if (!empty($this->files[$field]['name'][0]))
{
$check_error = 0;//added this
foreach ($this->files[$field]['name'] as $key => $value)
{
$this->file_name = $this->files[$field]['name'][$key];
$get_file_extension = explode('.', $this->files[$field]['name'][$key]);
$this->get_file_extension_end = strtolower(end($get_file_extension));
$array_1 = array(
$allowed_extension,
);
$array_2 = array(
$get_file_extension[1],
);
if (!in_array($array_2, $array_1))
{
$this->set_error('file_extension_not_allowed', 'extension_check');
$check_error++;
}
}
if($check_error > 0 )
{
return FALSE;
}
return $this;
}
}
i have a controller function that needs to be redirected to a route with a different function to avoid redundancy of codes. is it possible to put a redirect to a different function?
Here is the code:
public function index()
{
$x = Auth::user()->id;
$id = DB::table('requests')->where('id', $x)->lists('userid');
if (!is_null($id)) {
$frnd = DB::table('users')->whereIn('id', $id)->get();
if (!is_null($frnd)) {
return view('friendlist', compact('frnd'));
} else {
$frnd = null;
return view('friendlist', compact('frnd'));
}
} else {
$frnd = null;
return view('friendlist', compact('frnd'));
}
}
public function respond()
{
$frnds = new Friend;
$id = Auth::user()->id;
$friendid = Request::input('friendid');
$frnds->id = $id;
$frnds->friendid = $friendid;
if (Input::get('accept')) {
$frnds->save();
}
DB::table('requests')->where('id', $id)->where('userid', $friendid)
return // this is where i should redirect to page with function index()
}
Name the index route in routes definition like this
Route::get('home', ['uses' => 'YourController#index', 'as' => 'home']);
Then use redirect method to redirect to this route:
return redirect()->route('home');
For more info on redirects use official docs
http://laravel.com/docs/5.1/responses#redirects
I don't think is a perfect, but someone prefer this way:
private function _index()
{
$x = Auth::user()->id;
$id = DB::table('requests')->where('id', $x)->lists('userid');
if (!is_null($id)) {
$frnd = DB::table('users')->whereIn('id', $id)->get();
if (!is_null($frnd)) {
return view('friendlist', compact('frnd'));
} else {
$frnd = null;
return view('friendlist', compact('frnd'));
}
} else {
$frnd = null;
return view('friendlist', compact('frnd'));
}
}
public function index()
{
$this->_index();
}
public function respond()
{
$frnds = new Friend;
$id = Auth::user()->id;
$friendid = Request::input('friendid');
$frnds->id = $id;
$frnds->friendid = $friendid;
if (Input::get('accept')) {
$frnds->save();
}
DB::table('requests')->where('id', $id)->where('userid', $friendid)
$this->_index();
}
private function for repeated code.
I am trying to convert my model user to a library file and place it in the system libraries. because I have multiple installs of codeigniter.
On my line 16 which is just below my public function login, its saying Call to a member function query() on a non-object in
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User {
private $user_id;
private $username;
public function __construct() {
$CI =& get_instance();
$CI->load->library('session');
$CI->load->library('bcrypt');
$CI->load->database();
}
public function login() {
// Line 16 $user_query = $this->db->query("SELECT * FROM " . $this->input->get('db_prefix') . "user WHERE username = '" . $this->db->escape($username) . "' AND (password = '" . $this->db->escape($password) . "') AND status = '1'");
$result = $this->check_credentials($password);
$this->user_id = $user_query->row['user_id'];
$this->username = $user_query->row['username'];
$user_query = $this->db->get('user');
if($user_query->num_rows == 1) {
return true;
} else {
return false;
}
}
public function check_credentials() {
$user_query = $this->db->get('user');
if ($user_query->num_rows == 1) {
$result = $query->row_array();
if ($this->bcrypt->check_password($password, $result['password'])) {
//We're good
return $result;
} else {
//Wrong password
return array();
}
} else {
return array();
}
}
public function isLogged() {
return $this->user_id;
}
public function getId() {
return $this->user_id;
}
public function getUserName() {
return $this->username;
}
}
After having a break and thinking about it I did re try with what #Mehul Jethloja said
And got it working
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User {
private $user_id;
private $username;
public function __construct() {
$this->CI =& get_instance();
$this->CI->load->library('session');
$this->CI->load->library('bcrypt');
$this->CI->load->database();
}
public function login($username, $password) {
$this->CI->db->where('username', $this->CI->input->post('username'), $username);
$this->CI->db->where('password', $this->CI->input->post('password'), $password);
$result = $this->check_credentials($password);
$this->user_id = $user_query->row['user_id'];
$this->username = $user_query->row['username'];
$user_query = $this->CI->db->get('user');
if($user_query->num_rows == 1) {
return true;
} else {
return false;
}
}
public function check_credentials() {
$user_query = $this->CI->db->get('user');
if ($user_query->num_rows == 1) {
$result = $query->row_array();
if ($this->bcrypt->check_password($password, $result['password'])) {
//We're good
return $result;
} else {
//Wrong password
return array();
}
} else {
return array();
}
}
public function isLogged() {
return $this->user_id;
}
public function getId() {
return $this->user_id;
}
public function getUserName() {
return $this->username;
}
}
I am sitting in my chair for 2 hours and I cannot find what is wrong with this. I'm new to codeigniter things and I only got this error for the first time. Can you guys help me? Any help would be appreciated. Thanks in advance so much ...
****A PHP Error was encountered
Severity: Notice
Message: Undefined property: Facebook_model::$facebook
Filename: models/facebook_model.php
Line Number: 15
Fatal error: *Call to a member function getUser() on a non-object in C:\xampp\htdocs\hpbge\hpbge_files\system\hpbgestrong text_web_app\models\facebook_model.php on line 15*****
<?php
class Facebook_model extends Model {
public function get_user() {
$query = $this->facebook->getUser();
if ($query) {
$data['is_true'] = TRUE;
$data['facebook_uid'] = $query;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function get_access_token() {
$query = $this->facebook->getAccessToken();
if ($query) {
$data['is_true'] = TRUE;
$data['access_token'] = $query;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function get_api_secret() {
$query = $this->facebook->getApiSecret();
if ($query) {
$data['is_true'] = TRUE;
$data['api_secret'] = $query;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function get_app_id() {
$query = $this->facebook->getApiSecret();
if ($query) {
$data['is_true'] = TRUE;
$data['app_id'] = $query;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function get_logout_url() {
$query = $this->facebook->getLogoutUrl(array('next' => base_url()));
if ($query) {
$data['is_true'] = TRUE;
$data['logout_url'] = $query;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function get_signed_request() {
$query = $this->facebook->getSignedRequest();
if ($query) {
$data['is_true'] = TRUE;
$data['signed_request'] = $query;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function set_access_token($access_token) {
$query = $this->facebook->setAccessToken($access_token);
if ($query) {
$data['is_true'] = TRUE;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function set_api_secret($app_secret) {
$query = $this->facebook->setApiSecret($app_secret);
if ($query) {
$data['is_true'] = TRUE;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function set_app_id($app_id) {
$query = $this->facebook->setAppId($app_id);
if ($query) {
$data['is_true'] = TRUE;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
//function is formatted for the following
//https://graph.facebook.com/ID/CONNECTION_TYPE?access_token=123456
function get_facebook_object($object, $facebook_uid, $access_token) {
$fb_connect = curl_init();
curl_setopt($fb_connect, CURLOPT_URL, 'https://graph.facebook.com/'.$facebook_uid.'/'.$object.'?access_token='.$access_token);
curl_setopt($fb_connect, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($fb_connect);
curl_close($fb_connect);
$result = json_decode($output);
if (isset($result->error)) {
$data['is_true'] = FALSE;
$data['message'] = $result->error->message;
$data['type'] = $result->error->type;
$data['code'] = $result->error->code;
return $data;
} else {
$data['is_true'] = TRUE;
$data['data'] = $result->data;
return $data;
}
}
}
It seems that you have not loaded the facebook library put this line in your construct of model file
$this->load->library('facebook', array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET'
));
and make sure that you have put the facebook.php and base_facebook.php in libraries folder under application.