Stop PHP script execution through externally updated variable in Laravel 9 - php

I'm trying to stop the execution of an php script in runtime triggered by the user.
My planned approach is to make an ajax call to the server, setting a session variable which is then looked up by the first already running script every now and then to determine if the process should stop or continue running. Hope that makes sense:)
I already found some examples of people doing the same here or with files instead of sessions here.
Now my problem is that I would like to use the session for my approach instead of the solution doing it with temporary files and the mentioned approach with sessions doesn't seem to work for me in Laravel 9.
The result I'm looking for:
start first php script (runs maybe 30 seconds)
make ajax call to server & set session variable stop_execution = true
the first php script which is still running detects the change in stop_execution === true & stops execution.
The behaviour I get:
start first php script (runs maybe 30 seconds)
make ajax call to server & set session variable stop_execution = true
the first php script which is still running doesn't detect the change in stop_execution === true & runs until it finishes by itself.
the next time I run the first php script again it immediately detects the change in stop_execution === true & stops execution.
My thought on why this is happening is that the session variables doesn't get refreshed inside the first script after checking them for the first time. Maybe there is a way to force pull all new changes from the session variables while the first script is running? Did somebody have the same issues with Laravel? It seems like this is working with session variables when not using Laravel. I think it has something to do with Laravel and how the sessions are handled.
I would appreciate any advice 😊
Thanks a lot!
Code:
First script executed at the beginning
private function longRunningFunction()
{
// check session variable every iteration and exit while loop if === true
while ($condition === true && ! session('cancel_computation')) {
// do stuff ...
}
// reset session variable to false after finishing
session(['cancel_computation' => false]);
return $result;
}
Second script executed on ajax call
public function cancelComputationFunction()
{
// set session variable to be true
session(['cancel_computation' => true]);
}

I would not advice you to use sessions for this.
They are initialized on script start and I have never seen somebody re-fetch them in the same script.
Nor am I able to find such functionality when researching online.
What you could do though, is to utitlize the Cache facade.
It is very well suited for what you want and it very lightweight no matter which driver you choose to use under the hood in Laravel.
So instead of:
public function cancelComputationFunction()
{
// set session variable to be true
session(['cancel_computation' => true]);
}
You could do something like this:
public function cancelComputationFunction()
{
// set cache variable to be true
Cache::put('cancel_computation_' . session()->getId(), true);
}
And likewise inside the long-running part of the script.

Related

How to share variable in PHP from webpage script to script running permanently from the command line

I have a script running permanently from command line on a web server.
There is a loop checking new stuff in DB.
To save some load I need to add a condition not to check the DB if there is no reason.
When the change occurs I need immediate reaction, however it might happen that there is no change for a few hours.
And yes, it is not possible to do it in the webpage_script.php :)
The idea is to use some kind or a SUPERGLOBAL variable and in webpage_script.php save TRUE to that variable and then check it in that condition in permanently_running_script_on_the_same_server.php.
However I didn't find any variable that can be used for that reason... when I try to use session_id('whateverisinhereblahblab') - to share the session, the webpage_script.php will not get loaded as the session is probably continually occupied...
webpage_script.php
$the_shared_variable['whatever'] === FALSE;
if ($something_happens){
$the_shared_variable['whatever'] === TRUE;
}
permanently_running_script_on_the_same_server.php
while (true) {
if($the_shared_variable['whatever'] === TRUE){
}
}

How to Execute a PHP task and put on hold/sleep another task that is being executed right after until the first one is completed/half through?

I have a PHP Code that does some tasks.
Lets say someone executes the code by doing so https://localhost/code.php.
I have an employee that executes the script over curl from a separate server, what is the best way to prevent him from launching the script twice, before the (already running) script is actually completed/finished goes to the end.
TLDR: I would need a function, to wait until the task/code (that's running now) completes and the secondary task that is trying to be launched has given (sleep for few seconds or until the first tasks completes).
TLDR2: Looking for function [The title says it]
Any ideas? thanks.
While a session won't work with cURL, the idea is valid -- you need to set something persistent outside of your script. So, how about writing to a local file, or writing to a database?
if ( file_exists('lock.txt') ) die;
file_put_contents ('lock.txt', 'This file prevents script execution', LOCK_EX);
(... your script code here...)
unlink ('lock.txt');
If you know that there is only one user who will hit your server you can simply use session data.
<?php
session_start();
if (true === $_SESSION["NOT_FINISHED"] ?? false) {
die("Previous job is not finished yet!");
} else {
$_SESSION["NOT_FINISHED"] = true;
// start whatever job need to be done here
...
// when job is done and finished lets release out busy flag
unset( $_SESSION["NOT_FINISHED"]);
}

Stop PHP with ajax

I have a JavaScript functions which calls a PHP function through AJAX.
The PHP function has a set_time_limit(0) for its purposes.
Is there any way to stop that function when I want, for example with an HTML button event?
I want to explain better the situation:
I have a php file which uses a stream_copy_to_stream($src, $dest) php function to retrieve a stream in my local network. The function has to work until I want: I can stop it at the end of the stream or when I want. So I can use a button to start and a button to stop. The problem is the new instance created by the ajax call, in fact I can't work on it because it is not the function that is recording but it is another instance. I tried MireSVK's suggest but it doesn't worked!
Depending on the function. If it is a while loop checking for certain condition every time, then you could add a condition that is modifiable from outside the script (e.g. make it check for a file, and create / delete that file as required)
It looks like a bad idea, however. Why you want to do it?
var running = true;
function doSomething(){
//do something........
}
setInterval(function(){if(running){doSomething()}},2000); ///this runs do something every 2 seconds
on button click simply set running = false;
Your code looks like:
set_time_limit(0);
while(true==true){//infinite loop
doSomething(); //your code
}
Let's upgrade it
set_time_limit(0);
session_start();
$_SESSION['do_a_loop'] = true;
function should_i_stop_loop(){
#session_start();
if( $_SESSION['do_a_loop'] == false ) {
//let's stop a loop
exit();
}
session_write_close();
}
while(true==true){
doSomething();
should_i_stop_loop(); //your new function
}
Create new file stopit.php
session_start();
$_SESSION['do_a_loop'] = false;
All you have to do now is create a request on stopit.php file (with ajax or something)
Edit code according to your needs, this is point. One of many solutions.
Sorry for my English
Sadly this isn't possible (sort of).
Each time you make an AJAX call to a PHP script the script spawns a new instance of itself. Thus anything you send to it will be sent to a new operation, not the operation you had previously started.
There are a number of workarounds.
Use readystate 3 in AJAX to create a non closing connection to the PHP script, however that isn't supported cross browser and probably won't work in IE (not sure about IE 10).
Look into socket programming in PHP, which allows you to create a script with one instance that you can connect to multiple times.
Have PHP check a third party. I.E have one script running in a loop checking a file or a database, then connect to another script to modify that file or database. The original script can be remotely controlled by what you write to the file/database.
Try another programming language (this is a silly option, but I'm a fan of node). Node.js does this sort of thing very very easily.

Prevent Code or Function from Executing More Than Once

Is there a way to prevent a code-block or a function within a code from running more than once even if I re-execute (or reload) the PHP file?
I mean, can I restrict someone from executing a php script more than once? I can't seem to find the way to do this.
Yes, you can use a $_SESSION variable to determine if the code has been executed. The session variable will be set until the user closes their browser. If you want to extend it further than that, you can set a cookie. Please see the following links for more details.
Session Variables
Cookies
If you are using sessions, then you can set a flag in the user's session array after the code has executed:
function doSomething(){
if (empty($_SESSION['completed'])){
//Do stuff here if it has not been executed.
}
$_SESSION['completed'] = TRUE;
}
You should also check the sesison variable to see if the task has been executed previously. This assumes that the user can accept a session cookie.
I have an app that does that.
What we did was create a table in the db called version, and stored a version number in there. When the script is ran, it compared the version number in the database with that in the php script. And perform whatever it needs to "upgrade" it to the new version, and then updates the version number in the database.
Of couse, if the version table does not exist, the code will create it and mark it as storing version zero.
Just put a counter in the function. If the counter is greater that 0, then don't do anything. The counter variable should be static so it "remembered" across multiple calls.
function sample() {
static $call_counter = 0;
if ( $call_counter>0 ) {
return;
}
...
$call_counter++;
}
As for making sure a file is only executed once, just use "include_once()" instead of "include()".

Why is my PHP variable not changing when my Javascript code runs?

I have a counter timer and I tried to block a simple bypass to download without waiting.
so in main class I declared the boolean
$allow_download = false;
and in Javascript when the time is elapsed
else
{
textDLShow.style.display = 'none';
divDLShow.style.display = '';
"<?php $allow_download = true;?>";
}
and in the second class
if($allow_download == false)
echo "Test";
well, when time is elapsed the boolean is not set with positive value. Any suggestions ??
Thanks for your time !!
JavaScript cannot set the value of a PHP variable since the JavaScript interpreter cannot parse PHP (nor is the variable in the same interpreter anyway, since the JavaScript is run on the client's browser rather than on the server.)
In order to do this you will need to make a new request with JavaScript that your PHP code can read in order to set $allow_download to true and then serve up the download.
You'll want to read up on ajax, document.createElement (because one way to do this might be to create an iframe pointing at the download location after the time has elapsed) and setTimeout.
PHP is run on your server, the variables you create are no longer in scope when your client side javascript runs. There is no way to fix this unless you want to cook up some heavier stuff like storing in a database which IP addresses can download which files at what time.
php can run javascript, but javascript can never run php, take it for granted
the best thing you can do is run an ajax script to kind of call the php on the background
but really why bother going with all that, you can include the javascript in a php function and let it do the job

Categories