I got an users table on my database with a "Online" field, it gets the value "1" when the user loggin, and changes to 0 when the user logout. The problem is that if the user close the tab and don't click on the "Logout" button he stays Online forever.
So I would like to hook some kind of function so when the session expires it changes the value of this "Online" field - on the database - to "0".
I'm open to suggestions of course, because I don't what is the right approach for this.
You can do this by extending CI_Session
Create a php file inside application/core/MY_Session.php
class MY_Session extends CI_Session
{
public function __construct() {
parent::__construct();
}
function sess_destroy() {
//update the Online filed as required
$this->CI->db->update('YOUR_TABLE', array('YOUR_DATA'), array('YOUR_CONDITION'));
//call the parent
parent::sess_destroy();
}
}
Change the online field into datetime field,
Update the field using a central controller you're extending from, or use a hook to trigger the update of the field each time there's a request (You may put the code in the session validation function that you use to make sure a user is logged-in before triggering actions).
Then you can use the timediff SQL function to see if the user is active or not.
You have a specific problem. But I have one idea for this.
You can go on
system/libraries/Session.php
and update the function function sess_destroy() - line 398
In this function you can change the status on Database, and in this way you always will change the status when session destroy.
If you use unset_userdata(), than you need to change the function function unset_userdata($newdata = array() line 481 on the same file.
Related
When i'm trying to set session value in function then using this value it's fine but when i'm trying to reuse the same value which saved in the session with it's key i got undefined key in the array of the session why that's happen and how to handle that i tried to set middleware in the route but it gave me the same error
note: i redirect from my domain to another domain i know that's the problem occur after redirecting how to handle that i need standard solution i used $_cookie and it's worked but i want to use session ???
I'm tring to use the session values from function to another in the same controller after redirecting to another domain and back to my domain can someone tell me how to handle that and you must know that i don't controll the response from the external domain and this domain don't return the data that i want ???
the question here i need session to not expired if i go to this domain then back to my domain ?
try to capture the id after the creation
public function setCaptureOrderId($id) {
Session::put('catpureId', $id);
Session::save();
}
can use this getCaptureOrderId function
public function getCaptureOrderId() {
return $_SESSION['orderID'];
}
then redirect
public function http_create(){
return Redirect::to($url);
}
finally after the redirecting
getCaptureOrderId don't return anything and undefined key
I want to refresh current page after any user update
I'm trying to do something like that in User Model :
public static function boot()
{
self::updated(function ($model) {
return back(); //or redirect(Request::url())
});
}
but it wasn't working.
How can I refresh the page if any user updated
In general, the model event functions creating/created/updating/updating/saved/saving should only ever return false or nothing (void). Returning false in for instance updating (that is: before the model is persisted in the database) cancels the model update (this works for all propagating events, see https://laravel.com/docs/9.x/events#stopping-the-propagation-of-an-event).
To "refresh" the current page after a user update, you have to be more specific about what you require. The back() function that you use (or the aliases redirect()->back() or even app('redirect')->back()) is (to my knowledge) to be used in controllers and it uses the Referer header or a property in the user's session to determine to which page to send the client to. This is most often used with validation errors, so that you can send the user back to the page they came from along with any possible validation error messages.
Using back() (or any other request completions like return response()->json('mydata')) inside events is wrong and it doesn't even work since the result of such events is not being used to complete the request. The only valid thing you "could" do is to try validation inside an event, which in turn could throw ValidationExceptions and is therefore automatically handled.
What you should do is use the controller method that actually handles the request that updates the user:
// UserController.php
/** (PUT) Update a user */
public function update(Request $request, User $user)
{
if($user->update($this->validate($request, [/* validations */])) {
return redirect()->back();
// or even be explicit and use `return redirect()->route('users.show', $user);`
}
// `update()` returned false, meaning one of the model events returned `false`
// (there is no exception thrown here).
session()->flash('alert-info', 'Something went wrong');
return redirect()->back();
}
So I am trying to create a function that allows a user to delete their account: hence I want the function to delete all data in the database related to their profile, etc.
In view I have this button:
Delete Account
Which lead to my controller "Profiles" which has the following code:
function remove_user()
{
$this->load->model("profiles_model");
$email = $this->session->userdata('email');
$this->profiles_model->remove_user($email);
$this->load->view('templates/header');
$this->load->view('pages/homepage');
$this->load->view('templates/footer');
}
Which calls my model Profiles_model which has the following code:
function remove_user($email)
{
$this->db->where('email', $email);
$this->db->delete('profiles');
}
The table where the data is stored is "profiles".
I am also using sessions to pull the user's email.
When I execute, I get this error message: "Message: Call to undefined method Profiles_model::remove_user()"
I dont understand, as I use almost identical code to "Update" user data, and it works perfectly.
So I figured it out...my closing "}" tag on my model was ABOVE my function I was trying to call!!!!!!!!! There is 4 days wasted I will never get back again.
I have a multi tenant App. My system database I have models- User, Billing, FrontEnd ... and using policies I'm able to show, hide and prevent viewing and actions by tenant.
Each tenant has a database with models- Member, Event, Item ...
I set each model database based on the Auth::user()->dbname in the _construct method. This allows me to set my dbname to a clients database for tech support.
class Item extendsw Model {
public function __construct() {
parent::__construct();
if(Auth::user()->dbname) {
Config::set('database.connections.tenant.database', auth()->user()->dbname);
$this->connection = 'tenant';
}
}
This all works as planned until I add and Observer for a client model e.g. Member
I now get an error on any Observer call.
Trying to get property on non object Auth::user()->dbname.
Where should I be registering the Observer? I tried AppServiceProvider and NovaServiceProvider.
I think that happens because the observer instantiates your User model before the request cycle has started and that means that your User instance does not exist yet neither has been bound in the Auth facade.
Thus, Auth::user() returns null and you are trying to get a property from it.
A way to solve the issue may be to check if the user instance exists or not:
public function __construct() {
parent::__construct();
if (optional(Auth::user())->dbname !== null) {
Config::set('database.connections.tenant.database', auth()->user()->dbname);
$this->connection = 'tenant';
}
}
The optional helper return the value of the accessed property (dbname in your case) if and only if the argument is not null, otherwise the whole call will return a null value instead throwing an exception.
If that is not the case, maybe update the question with the error stacktrack and the code/action that triggers the error
I am creating test case for my CodeIgniter app. However I just found something that I thought should not be happen :
in login.php controller :
public function logout()
{
$this->session->sess_destroy();
redirect('/');
}
So I just created a test to just make sure that session is really destroyed :
public function test_logout()
{
$this->CI = set_controller('login');
// make sure that all session is destroyed
$this->CI->session->set_userdata('test_session', 'some_value');
$this->CI->logout();
// userdata 'test_session' should be removed!
$this->assertTrue(($this->CI->session->userdata('test_session')==null || $this->CI->session->userdata('test_session')==''));
}
However I find that upon running the test case, my test case fails! Upon debug on the last line of test case, I found that the userdata is still exist with value = 'some_value'. I thought that sess_destroy should also delete all the set user data, as per what they described in their website documentation:
This function should be the last one called, and even flash variables will no longer be available. If you only want some items destroyed and not all, use unset_userdata().
I am using Kenji's CIUnit for unit testing.
Is this the correct behaviour or is there something that I missed?
Just found that CIUnit routes the Session to CIU_Session instead of original CodeIgniter's CI_Session. It miss a line that CI_Session does :
$this->userdata = array();
So turns out this is CIUnit's issue instead of CodeIgniter's. Create an issue in their bitbucket page.