D
D
driverx182018-12-02 12:18:27
PHP
driverx18, 2018-12-02 12:18:27

How to make php script run only from cron?

I have a project, I put a couple of cron scripts on the hosting. In fact, these are ordinary php files, but which are called by cron. However, you can call them not just from the cron, but also directly, let's say some user enters the address of my site /cron_file.php into the URL, then he will run the whole command. And if this command sends mailings to users, then it’s generally sad that a person will click F5 on my script and the mailing will constantly start).
Actually, how to deal with this, write a condition to run only from the cron (I saw that you can pull the info from the $_ENV superglobal), or is it done somehow differently?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
OnYourLips, 2018-12-02
@driverx18

Do not store your console scripts in a directory that is accessible from the web server.
Here is an example project root directory:
bin <= console scripts here, cron runs them
public <= webserver looks here
src <= project code
tests <= tests
vendor <= dependencies

N
neol, 2018-12-02
@neol

If launching only from cron means launching from cli, then

if (php_sapi_name() !== 'cli') {
    exit();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question