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

Asked

Viewed 275 times

2

In the answers to question of the link below I posted how I managed to delete from the database the user information that not activate in up to 24h your registration through the link sent to your email:

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

On my login homepage I put the function

$revisa = vinte_e_quatro(); 

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

DOUBT

My function to check whether or not the user activated his 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 do a cron job for the page where I created this function and that’s it? Just wait for it to turn automatically...

  • 2

    Cron job is the best alternative to this.

  • I just read here, it’s what I really need, but I have to see how to apply.

  • It will depend on where you are staying

  • I used as much with my domain as only the file name: www.meusite.com.br/vinte_e_quatro.php or vinte_e_quatro.php and I couldn’t, it arrives at the scheduled time and the function doesn’t run unless I access the link in the browser. Look at the manager: goo.Gl/Wkkicp

3 answers

1

Assuming your code is already working correctly, you can include this query at the beginning of the website homepage, 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.
// ...

You have no problem executing this query on all requests, unless your database has millions of records of course...

And consider indexing active fields and data_ts 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 20:35

  • It was already like this in "default.php" anyway. I imagine that there are no problems that this runs every time the page is accessed, but I was curious and want to test the cron job, because it can be useful in other cases. Grateful!

  • How to index? And what utility?

  • @Iwannaknow edit table structure in database and put with fields as index, the search of information gets faster...

  • I was able to add index to the field, but since this makes the query faster why not index all fields? You have to pick the important ones or something?

  • @Iwannaknow The basic idea is to index only fields that will be used in Where clauses, ie data that will be searched/compared during the query, but it is advisable to perform performance tests before after indexing, because a query may slow down with a certain indexed field.

  • I understand, grateful.

  • @Iwannaknow I edited the answer and includes a command option to use in cronjob, try.

Show 2 more comments

1

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

curl -s -o /dev/null http://seusite.com.br/arquivo.php
  • I used as much with my domain as only the file name: www.meusite.com.br/vinte_e_quatro.php or vinte_e_quatro.php and could not, arrives at the scheduled time and the function does not run unless I access the link in the browser.

  • Look at the manager: http://goo.gl/WKKicP

  • This is my server’s Cpanel: http://imageshack.com/a/img537/1350/tmDKCo.png

  • This 'Curl -s -o /dev/null" in the command is from your Cpanel or we have to use it in any cron job?

  • Since your server has the CURL library enabled, you can use it, "/dev/null" is just to not send emails to you every time cron runs, try running the file by the same path, e.g.: /home/SEU-USUARIO/public_html/filename.php. If it doesn’t work anyway, try this way: wget http://seudominio.com/arquivo.php

  • Dude, I tried every way and nothing, I found until this: http://goo.gl/OC2up8 At the end of this forum the guy says I’d have to create another file to call my script. I did it and nothing. I’m also on a free server, I’m using to do tests, so go that’s bugged even...

Show 1 more comment

0

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

  • It can be done whenever someone activates an account, for example, you delete all database records that have been made for 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 less than the current date/time, delete from the database.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.