0

i am having a php file that is saving data into database, i want the file to keep on running even if the browser is closed. this is what i tried: js.php file:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>   
<script type="text/javascript">

  setInterval(function() {
    $.get('http://localhost/cryptopiamodelp/tst.php/', function(data) {
      //do something with the data
      alert('Load was performed.');
    });
}, 5000);
</script>

tst.php file:

<?php

$con = mysqli_connect('localhost','root','','cryptopiamodel') or die(mysql_error());
$sqli = "INSERT INTO test VALUES ('')" or die(mysqli_error());
mysqli_query($con,$sqli) or die(mysqli_error($con));
?>

but this only works when the browser is opened, is this possible in php to execute js.php file when the browser is closed so that the data is saved into database continuously? Thanks in advance.

j08691
  • 204,283
  • 31
  • 260
  • 272
hdrajani
  • 73
  • 1
  • 2
  • 10

2 Answers2

0

You can set up a cron job. A cron job allows you to setup almost any kind of schedule.Depending on where you are hosting your web application a cron job can be set differently.

For example if your site hosting comes with a cPanel, they have an option to define a cron job. You can set up a schedule and give path to your php file.

abhinav pandey
  • 574
  • 2
  • 8
  • 21
-1

You can use ignore_user_abort to avoid php hanging the execution upon browser disconnection.

Then, you can use set_time_limit to change the default time limit (30 sec) to no limit or any other value that you need:

ignore_user_abort(true);
set_time_limit(0);

Read http://php.net/manual/en/function.ignore-user-abort.php

mzcarlos
  • 211
  • 3
  • 12