Welcome Guest, Not a member yet? Register   Sign In
trying to run function just once
#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


Messages In This Thread
trying to run function just once - by richb201 - 06-21-2021, 09:06 AM
RE: trying to run function just once - by ikesela - 06-21-2021, 07:10 PM
RE: trying to run function just once - by paliz - 06-22-2021, 04:19 AM
RE: trying to run function just once - by craig - 06-23-2021, 03:26 AM
RE: trying to run function just once - by richb201 - 06-23-2021, 02:07 PM
RE: trying to run function just once - by paliz - 06-24-2021, 02:37 AM



Theme © iAndrew 2016 - Forum software by © MyBB