With Codeigniter session.
I have created the ci_session table.
In this table I save session datas in the blob datatype.
$sess_data= array(
'logged_in' => TRUE
);
$this->session->set_userdata($sess_data);
For count all online users, I need to get logged_in==1 value in blob datatype.
For example, user 1 is logged in with Chrome web browser. User2 is logged in with Mozilla. In the database there are 2 sessions with the different session id.
How can i get this all logged_in==1 values in this ci_session table?
This is my model function.
Please help me the fill where place in this function?
public function count_online_users(){
$this->db->select('*');
$this->db->from('ci_session');
$this->db->where(...........);
$query = $this->db->get()->num_rows();
return $query;
}
Check this this method you have to follow
First you have to update logged_in = 1 in your users table as your defined name
after you get number of users are logged_in
// Update user in to call this function
$data = array(
'logged_in' => '1'
);
public function update_user($user_id, $data)
{
$this->db->where('id',$user_id);
$this->db->update('users',$data);
}
// After you get logged in user in total value
public function count_online_users()
{
$this->db->select('*');
$this->db->from('users');
$this->db->where('logged_in','1');
$query = $this->db->get()->num_rows();
return $query;
}
You can use javascript and asynchron request for update field. When you close the window, the handler of window.onunload is called
var unloadHandler = function(e){
//here ajax request to close session
};
window.unload = unloadHandler;
To solve the problem of redirection, php side you can use a counter
class someController extends Controller {
public function methodWithRedirection(){
$this->session->set_userdata('isRedirection', 1);
//here you can redirect
}
}
class homeController extends Controller{
public function closeConnection(){
$this->load->library('session');
if($this->session->userdata('isRedirection')!== 1){
//destroy session
}
}
}
Related
I'm trying to send a property, maxId back with my user data. This property is the id of the last row received from the database (cursor-based pagination). I do this like so:
public $maxId = 0;
public function loadMore()
{
//?
}
public function render()
{
$data = User::where('id', '>', $maxId)->limit(10)->get();
$data->maxId = $data->last()->id;
return view('livewire.users', ['users' => $data]);
}
But how can I return (or retain the data on the server) of maxId so I know where to start collecting my data from next time.
Please note I do not wish to implement standard laravel pagination.
i have 3 models: Order, Lancenter and ClientAccount
Lancenters can create Orders.
ClientAccounts stores username and password.
When an order is created, the Lancenter can choose an existant client account or create a new one, so i show up all ClientAccounts related with the current Lancenter.
For security reasons, i don't retrieve the ClientAccount password when a lancenter wants to create a new order.
So, in ClientAccount.php i have this:
protected $hidden = ['password'];
And in Lancenter.php:
public function client_accounts()
{
return $this->hasMany('App\ClientAccount', 'id_lancenter');
}
In LancenterController.php this function retrieves lancenter's client accounts without the password:
public function getClientAccounts()
{
$lancenter = Lancenter::with('client_accounts')->where('id_owner', Auth::id())->first();
return response()->json(['client_accounts' => $lancenter->client_accounts]);
}
The problem is that the created order belongs to a ClientAccount and in this case i need to retrieve also the password of ClientAccount.
So i have in Order.php:
public function client_account()
{
return $this->belongsTo('App\ClientAccount', 'id_client_account');
}
And this function in OrderController.php retrieves new orders:
$orders = Order::with('client_account')->where('status', $status)->get();
return response()->json(['orders' => $orders]);
Obviously, the ClientAccount is retrieved without the password.
I get it by doing a loop to get the ClientAccount of each order in the index function in OrderController, but i supose that there is a cleaner to do it. Hope you can help me.
Update:
this is the loop in OrderController that i'm talking about
public function index($status)
{
$orders = Order->where('status', $status)->get();
foreach($orders as $key => $order) {
$client_account = ClientAccount::find($order->id_client_account)->makeVisible('password')->toArray();
$orders[$key]['client_account'] = $client_account;
}
return response()->json(['orders' => $orders]);
}
i want check session and get current user details only one row i new here plz help
public function profile() {
$this->load->view('header');
$this->load->model('brid_groom_fetch');
$this->session->userdata('uname');
//This below line fetch model db data
$row = $this->brid_groom_fetch->get_program_specific_gender();
//here i have check data is in session..
$session_id = $this->session->userdata(
array('uname' => $row->uname));
print_r($sesid);
}
Why don't you use basic == operator ?
public function __construct() {
parent::__construct();
$this->load->model('brid_groom_fetch');
// you need to load session library
// Have you loaded ?
$this->load->library('session');
}
public function profile() {
$this->load->view('header');
// you do not need to load in all method.
// we have already loaded in __construct()
// $this->load->model('brid_groom_fetch');
// use variable
$uname = $this->session->userdata('uname');
//fetch data from db
$row = $this->brid_groom_fetch->get_program_specific_gender();
if ($row->uname == $uname) {
$session_id = $this->session->userdata('session_id');
var_dump($session_id);
}
}
You are setting userdata with
$this->session->set_userdata(array('uname' => $yourname));
or
$this->session->set_userdata('uname',$yourname);
To check what value is set in the userdata you need to use
$sess_name = $this->session->userdata('uname');
print_r($sess_name);
And then validate this $sess_name you have received.
You can also unset the userdata by using
$this->session->unset_userdata('uname');
I want to unset and set session in model function depend on passed id.
I have language field for every record and should change session considering that.
in sample word i have a link like this : http://www.mysite.com/report/2020
now this article by this id maybe saved by english language but current session is french.so I should change the session.
here is my code but it dos not work.
//function for checking language by id
public function lang($id)
{
$data = $this->db
->select('language')
->from('content')
->where('id',$id)
->get();
if ($data->num_rows > 0) {
return $data;
}else{
return false;
}
}
//function for reading data from db depend on id and language
public function report($id)
{
$cat=2;
$code=$id;
$langu= $this->lang($id)->row()->language;
if($langu==3){
$this->session->unset_userdata('lang');
$this->session->set_userdata('lang','dr');
}
if($langu==2){
$this->session->unset_userdata('lang');
$this->session->set_userdata('lang','pa');
}
if($langu==1){
$this->session->unset_userdata('lang');
$this->session->set_userdata('lang','en');
}
$language= $this->session->userdata('lang');
$lang=$language;
$data = $this->db
->select('*')
->from('content')
->where('category',$cat)
->where('language',$lang)
->where('id',$code)
->get();
if ($data->num_rows > 0) {
return $data;
}else{
return false;
}
}
the best way to session is:
you have to check the query in model, and call the model in controller, if its true set session data within controller....
Setting Session within the model is not a good idea for an MVC architecture.
Sometimes there is really need to set or get session data in model.
Use then Code Igniter instance like below:
$codeIgniter =& get_instance();
$codeIgniter->set_userdata('data', $data);
// or to read data:
$data = $codeIgniter->userdata('data');
Note that using $this to call set_userdata() or userdata() in model will not work correctly.
I need to get the user id of the person logged in, so that i can run a query for that user. Im using ion auth, should i use a session or call it from the db, should i use a helper or not? anyway, heres what im trying to do:
example: how many work orders does logged in user have?
query would be: select * from work_orders WHERE status = "1" AND userid = "%THE LOGGED IN USER"
here is my controller, which doesnt have the "get user id" code:
public function index()
{
$this->load->view('templates/header');
$data['total_open_wo'] = $this->Home_model->total_open_wo();
$data['result'] = $this->Home_model->index();
$this->load->view('home', $data);
$this->load->view('templates/footer');
}
here is my model:
public function total_open_wo() {
$this->db->where('status', "1");
$this->db->where('tid', "NEED_THE_USER_ID");
$num = $this->db->count_all_results('work_orders');
return ($num);
}
Provided the user is logged in you can get the user ID by using the get_user_id() function in ion auth. On a related note in your model you need a $this->db->from() call to set the table to fetch the number of rows from.
public function total_open_wo() {
$userId = $this->ion_auth->get_user_id();
$this->db->where('status', '1');
$this->db->where('tid', $userId);
$this->db->from('work_orders');
$num = $this->db->count_all_results();
return $num;
}
Use the function $this->ion_auth->logged_in() to make sure that the user is logged in before calling the model function.
you can retrieve the logged in user details using user() method.
For example to get user id
echo $this->ion_auth->user()->row()->id;