Welcome Guest, Not a member yet? Register   Sign In
trying to run function just once
#11

(This post was last modified: 06-22-2021, 04:42 PM by richb201.)

[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
Reply
#12

@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.
Reply
#13

(This post was last modified: 06-22-2021, 07:14 PM by richb201.)

(06-22-2021, 06:58 PM)John_Betong Wrote: @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.

Thanks John. Do you mean a session variable for the $hasRun? Yes, I can try that. The only issue is that where do I set $_SESSION['RUNONCE']=0? If I set it in my code it will get reset to 0 eveytime _init runs? Can I set it in config.php or another system level file? How about index.php?
proof that an old dog can learn new tricks
Reply
#14

(This post was last modified: 06-22-2021, 08:42 PM by John_Betong.)

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:
//=======================================================
public function initController
(
 
RequestInterface  $request
 
ResponseInterface $response
 
LoggerInterface  $logger
)
{
  session_start();
  $_SESSION['RUNONCE'] = $_SESSION['RUNONCE'] ?? 0;
  $_SESSION['RUNONCE'] = ++$_SESSION['RUNONCE'];

// Do Not Edit This Line
parent::initController($request$response$logger);
...
... 

Edit:
Just tried setting the script in index.php and it also works OK
Reply
#15

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 )
Reply
#16

(This post was last modified: 06-23-2021, 03:27 AM by craig.)

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.
Reply
#17

(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.

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.
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
Reply
#18

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
$runOnce session('runOnce');

if (
$runOnce !== true) { 
  // run your code
  some_function();

  // save the value to the session data
  session()->set('runOnce'true);

CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#19

(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.

Example:
PHP Code:
// get the value
$runOnce session('runOnce');

if (
$runOnce !== true) { 
  // run your code
  some_function();

  // save the value to the session data
  session()->set('runOnce'true);

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
Reply
#20

(This post was last modified: 06-23-2021, 04:17 PM by richb201.)

(06-22-2021, 03:24 PM)John_Betong Wrote: The script I supplied is PHP.

Where or what is _init()
_init is a function that I got from the CI3 samples. It gets called from __construct() and I use it to set up env variables. I probably should move that type of stuff out of there to reduce delays. My plan was that once I get the code working I will go through and clean it up. Smile
proof that an old dog can learn new tricks
Reply




Theme © iAndrew 2016 - Forum software by © MyBB