trying to run function just once |
[quote="includebeer" pid="387882" dateline="1624400615"]
Once per what? Once per day? Once per page load? What are you trying to accomplish? If you don't need to run it more than one time, in which context is it called repeatedly? Good question. I need to run it once per user who logs in. It is straight CI3. It starts off with __construct and at the bottom of __construct is a call to _init(). I did program in C for many years (many years ago) and in C, things just run once. I am using Grocery_crud extensively and also ci_simplicity. It seems that in CI every time a user clicks on any of my menu items, _init() runs again. Seemed strange to me too. What I am trying to do is each time a user logs into the system, __construct get called which loads the CI helpers and it runs _init. In init() I call get_from_locker() and it retrieves their image data from S3 to use during their session. When they log off, I delete their image data from my server, just to make sure that the next user who logs in to that server won't see the first users data. Bringing down the users data from S3 can be time consuming so I want to only do it once per user log in.
proof that an old dog can learn new tricks
@richb202,
> Bringing down the users data from S3 can be time consuming so I want to only do it once per user log in. Try using session which I assume is already being called when a user logs in. (06-22-2021, 06:58 PM)John_Betong Wrote: @richb202,
proof that an old dog can learn new tricks
I use native PHP session_start() which is called in app/Controllers/BaseController.php
Once called $_SESSIONS remain active all the time a user is logged in and the session is dropped (I think when a user logs out.). Try checking to see if $_SESSION['RUNONCE'] has been set. File: /app/Controllers/BaseController.php PHP Code: //======================================================= Edit: Just tried setting the script in index.php and it also works OK
It's running every time because you are calling the _init method in the __construct
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Every single page view in a PHP web application is an entirely new instance of the code execution - all the init() and construct() functions will get called. At the end of the script execution (e.g. viewing a page), the classes and variables are destroyed. PHP apps work entirely different to C programs, so that knowledge doesn't transfer exactly.
For your use case, you could move your function call to get_from_locker() to your login handling code, after verifying their password - so you only do it once you have a valid user. Then you don't need to call it again anywhere, and you don't need to do it in any init() or construct() calls. If you do insist on doing it in those places, then use Sessions to keep track of whether or not you've got the image. (06-23-2021, 03:26 AM)craig Wrote: Every single page view in a PHP web application is an entirely new instance of the code execution - all the init() and construct() functions will get called. At the end of the script execution (e.g. viewing a page), the classes and variables are destroyed. PHP apps work entirely different to C programs, so that knowledge doesn't transfer exactly.Thanks so much to all of you. OK, now things are a little clearer. I need to investigate but the way it works now is I have a separate controller that handles the login and then dumps the user in my dashboard. I am changing this to use fusionAuth so I can have them "dumped into" a specific function. I have been battling with fusionAuth's passwordless login for a few months and am just about to give up on that feature.
proof that an old dog can learn new tricks
Craig has the right solution for your use case. You either call your function right after a successful login, or you save the state in the session data. So at the next page load, the application will "remember" is has already called this function. If you use the session, I recommend using the session() function instead of accessing a variable directly with $_SESSION['yourVariable]. This way you don't have to check if the variable exists before accessing the $_SESSION array.
Example: PHP Code: // get the value (06-23-2021, 01:53 PM)includebeer Wrote: Craig has the right solution for your use case. You either call your function right after a successful login, or you save the state in the session data. So at the next page load, the application will "remember" is has already called this function. If you use the session, I recommend using the session() function instead of accessing a variable directly with $_SESSION['yourVariable]. This way you don't have to check if the variable exists before accessing the $_SESSION array.Thanks Beer. I moved the function into index.php (the one in my controller) and this seemed to take care of it since this is not connected to __construct or _init, which I know now runs on every page load. Do you think I still need to watch to see it it was already initialized?
proof that an old dog can learn new tricks
(06-22-2021, 03:24 PM)John_Betong Wrote: The script I supplied is PHP.
proof that an old dog can learn new tricks
|
Welcome Guest, Not a member yet? Register Sign In |