0

I have a function which adds a new blog post to my database.

After running add_new_post() function the post should be approved by my another function approve_post().

But I don't want to approve post immediately. I want to run approve_post() function in time between 1 to 5 minute (random).

Now I am using this code:

function add_new_post() {
    *my code of adding post*

    $time = rand(60, 300);
    echo $time;
    sleep($time);

    approve_post();
}

But browser shows loading until sleep ends. Also, I found that my Windows machine shows time execution error.

Is there a way to run approve_post() function in the background without showing page loading to the browser?

I would be grateful for а code example.

Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
  • Add post and approve post should be two independent functions. `add_post()` should not even call `approve_post()`. Assuming `add_post()` is called when some button is pressed, create another button for approving, and pass id of the post to the submit button. Mind that the `approve_post()` then must exect at least one parametr - some form of identification of the post. That pretty much is all advice i can give. It would take a lot more code to implement it. – Eugene Anisiutkin Nov 13 '18 at 13:40
  • Can't understand about what button you are talking about. It is automatic action of approving. I just want to run function,I don't need any buttons for approving post. – Nikolai Maksimov Nov 13 '18 at 14:13
  • server side cron jobs – Andrej Buday Nov 13 '18 at 15:34

2 Answers2

0

There are multiple options.

You can use ajax which is required to open a page or you can use a Cron job. I will prefer to use Cron job

The concept will same, for an example. The post table must has created_date ,approve_date and approve_status. So when you create the post it will save the approve_date too.

Example, You create a post on 18:00 then the approve_time will 18:05. Then the background process (ajax or Cron job) will check the approve_status first then execute the approve process when live time same like approve_time

ref : cron job & Ajax

sorry for my English

Sayed Mohd Ali
  • 2,156
  • 3
  • 12
  • 28
Boby
  • 1,131
  • 3
  • 20
  • 52
  • Hm, AJAX is running on the client side. So, if user will close page this function will not execute. I need something server-side. – Nikolai Maksimov Nov 13 '18 at 13:39
  • @NikolaiMaksimov then use cron_job, any reason why you want to `delay` the approve ? – Boby Nov 13 '18 at 13:41
  • The cron side is not clear for me. Do you have any examples based on my question? I have found only cron tasks to do something at specific time, for example each hour. But my time is being specified when **add_new_post()** function runs. – Nikolai Maksimov Nov 13 '18 at 13:41
  • Ajax are is out of the question. Server side solution lies in php, the correct organization of the form and correct usage of form. Cron... is not a great solution in general, because it removes the ability to revew the post before accepting them – Eugene Anisiutkin Nov 13 '18 at 13:41
  • I don't want my users to know that posts are being approved by function, not by real person. So, different delay time adds human-like behavior. – Nikolai Maksimov Nov 13 '18 at 13:44
  • Well then independant script for accepting posts and a cronJob is the answer. `approve_post()` should not be called from `add_post()` if you don't want to delay the execution of `add_post()` – Eugene Anisiutkin Nov 13 '18 at 13:49
  • @EugeneAnisiutkin can you please give some code examples or link it it is possible? I can't understand how to fit cron here. – Nikolai Maksimov Nov 13 '18 at 13:51
  • @Nikolai Maksimov See answer below, links are there. – Eugene Anisiutkin Nov 13 '18 at 13:52
  • just run the cron every minute to run a file that checking the post table. if the criteria true, you can run approve query. – Boby Nov 13 '18 at 13:53
  • @MyNameIs I found out that cron is limited to minimum 1 minute of delay. Don't you know any ways to leverage time? It is just for knowledge. – Nikolai Maksimov Nov 13 '18 at 14:10
0

First, you need to increase the maximum execution time of a PHP script by using the following link https://www.plus2net.com/php_tutorial/max-ex.php

Then use sleep() function to delay code execution as:

<?php
echo date('H:i:s');
sleep(15);
flush();
echo "<br>";
echo date('H:i:s');
?>

Reference link

Or you can CronJob

Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51