PHP script is automatically called repeatedly

I have a strange problem. I am using javascript ajax (I used jquery). Now the script:

One ajax call invokes a php script, which is basically a lengthy process and sets some session variables.

Later at a few intervals (say, every 2 seconds) I start another ajax call to check the session variables to find out when the process (first run php script) is complete.

The first php script retrieves data from the database and inserts it into a file. In each sample, I count the cycle number and save it in a session variable to save some kind of tracking record. How;

 $i=0;
 $_SESSION['time']=date('m-d-Y H:i:s');
 while(...)
 {
     ini_set('session.use_only_cookies', false);
     ini_set('session.use_cookies', false);
     ini_set('session.use_trans_sid', false);
     ini_set('session.cache_limiter', null);
     session_start();
     $_SESSION['tracksatus']="loop number : ".$i." time is :"$_SESSION['time'];
     session_write_close();

     $i++;

     ......
     ......
 }

Another php script that I call through setIntervalajax just does:

 echo $_SESSION['trackstatus']

The set ajax interval returns me:

  loop number 1 time is m-d-Y H:i:s
  loop number 5 time is m-d-Y H:i:s
  loop number 8 time is m-d-Y H:i:s
  ......

  loop number 1 time is m-d-Y H1:i1:s1
  .....

H: i: s H1: i1: s1

, , php script . 12 . - ( , , .... , ).

, ? , , .

, , , , , . .

+6
5

, , , PHP , script, script .

, session_write_close(); session_start(); .

session_write_close HD, script . session_start HD, , script , script.

, : AJAX OPTIONS URL- CORS. , script HTTP METHOD, HEAD OPTIONS die();

+3

Javascript

var time = Date.now();
$.get('/firstURL?time='+time);
setInterval(function(){
    $.get('/secondURL?time='+time, function(response){
        console.log(response);
    }
}, 1000);

PHP 1- URL

<?php

$id = $_GET['time'];
$count = 0;

while(...) {
    // Do your stuff

    $count++;
    file_put_contents("/tmp/{$id}", $count);
}

?>

PHP 2- URL

<?php

$id = $_GET['time'];
$count = 0;

try {
    $count = file_get_contents("/tmp/{$id}");
} catch(Exception $e) {}

echo $count;

?>
+3

, PHP . php, , , ajax .

function updateCreateProgress($jobStartTime, $progress){
    file_put_contents('/tmp/'.$jobStartTime.'.txt', $progress);
}

function completeProgress($jobStartTime){
    unlink('/tmp/'.$jobStartTime.'.txt')
}

script '/tmp/'.$jobStartTime.'.txt', file_get_contents, .

+3

:

$i=0;
ini_set('session.use_only_cookies', false);
ini_set('session.use_cookies', false);
ini_set('session.use_trans_sid', false);
ini_set('session.cache_limiter', null);
session_start();
 $_SESSION['time']=date('m-d-Y H:i:s');
 while(...)
 {

     $_SESSION['tracksatus']="loop number : ".$i." time is :"$_SESSION['time'];
     session_write_close();
     session_start();
     $i++;

     ......
     ......
 }

$_SESSION session_start();

+1

ajax GET - "cache: false".

, php script . ( GET) .

php . session_write_close(), script , - . , - , .

:

1) script1.php - ajax .

2) script2.php(long job here) - 1.php cron ( ) 2.php cron ( ).

3) script3.php - (ajax).

"" 2.php script3.php flock(), clearstatcache() flush().

0
source

Source: https://habr.com/ru/post/1016742/


All Articles