How to make a script in php inserted in a page through a "require" run from time to time?

2

In the answers to the question of the link below I posted how I was able to delete from the database the information of the user that does not activate within 24 hours your registration through the link sent to your email:

How to delete the database from the database if the user does not activate it by email within" X "hours?

On my login homepage I put the

$revisa = vinte_e_quatro(); 

Now every time my homepage is loaded the script will run. How do you make it run only once a day, for ** week ...?

DOUBT

My function to check whether or not the user activated your registration in 24h is this:

require 'config.php';  
require 'connection.php'; 
require 'database.php'; 
<?php
//Exclui linhas não ativadas em até 24h
function vinte_e_quatro () {     
$tempo_agora = time(); 
$query = DBDrop('myway', "ativo='0' AND (data_ts + 86400) <= '$tempo_agora' "); 
}
$revisa = vinte_e_quatro();
?>

Should I make a cron job for the page where I created this function and that's it? Just wait for it to run automatically ...

    
asked by anonymous 22.08.2014 / 01:48

3 answers

1

If your server has CPANEL, you can set up a cronjob, just look in the panel and configure how long how long the file or url should run, and it would use something like:

curl -s -o /dev/null http://seusite.com.br/arquivo.php
    
22.08.2014 / 13:32
1

Assuming your code is already working correctly, you can include this query at the top of the home page, for example:

index.php

<?php
require 'config.php';  
require 'connection.php'; 
require 'database.php'; 

//Exclui linhas não ativadas em até 24h
$query = DBDrop('myway', "ativo='0' AND (data_ts + 86400) <= '" . time() . "' "); 

// restante o seu código.
// ...

It's okay to run this query on all requests, unless your database has millions of records of course ...

And consider indexing the active and data_ts fields if the table has many fields.

If you need to configure the task with a cronjob, try the command below:

35 20 * * * lynx -dump http://dominio/arquivo.php

In this example, it runs every day at 8:35 pm

    
22.08.2014 / 14:54
0

You can use cron job, as the staff commented. Or an option you can do to not run the same code every time someone opens your page:

  • Can be done whenever someone activates an account, for example, you delete all the records in the database that have been made more than 24 hours. You can use this check for example:

    date_add(data_ts, interval 1 day) <= now() and ativo = 0

Which will add 1 day in "data_ts" and if the result is smaller than the current date / time, exclude from the database.

    
20.05.2016 / 06:06