如何使用 cron 作业运行 php 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10097609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:22:42  来源:igfitidea点击:

how to run php file using cron jobs

phpcron

提问by Mustafa M Jalal

I have a E-mail schedule runs everyday on php page using cron jobs. The php code workds fine when i run the page using a link.

我有一个电子邮件计划每天使用 cron 作业在 php 页面上运行。当我使用链接运行页面时,php 代码工作正常。

Now when I run the php script using cron jobs, it also works fine but when I put some query the cron jobs won't understand the link.

现在,当我使用 cron 作业运行 php 脚本时,它也可以正常工作,但是当我进行一些查询时,cron 作业将无法理解链接。

for example: http://www.wetube.org/cron.php?id=01001so now if I try to run this everyday using cron job it's doesn't work.

例如:http://www.wetube.org/cron.php?id=01001所以现在如果我尝试每天使用 cron 作业运行它,它就行不通了。

But if we just erase the query it works fine. Do you guys know any code which makes this link work in cron job?

但是如果我们只是删除查询它工作正常。你们知道让这个链接在 cron 工作中工作的任何代码吗?

回答by wyqydsyq

Cron runs commands as they would be ran via the shell, so running PHP would use local paths.

Cron 运行命令就像它们通过 shell 运行一样,因此运行 PHP 将使用本地路径。

You need to use a command like:

您需要使用如下命令:

php /home/USER/public_html/cron.php

php /home/USER/public_html/cron.php

Or if including the query string is necessary, use cURL instead (if it's installed):

或者,如果需要包含查询字符串,请改用 cURL(如果已安装):

curl http://www.wetube.org/cron.php?id=01001

curl http://www.wetube.org/cron.php?id=01001

You might want to look at not exposing your cron scripts to the internet - move them to outside your web directory because if someone finds it they can constantly reload it to spam your cron scripts (i.e. sending lots of emails)

您可能想考虑不要将您的 cron 脚本暴露在互联网上 - 将它们移到您的网络目录之外,因为如果有人发现它,他们可以不断地重新加载它以向您的 cron 脚本发送垃圾邮件(即发送大量电子邮件)

回答by Calvin Grain

I would add hash like

我会添加哈希

curl http://www.wetube.org/cron.php?id=01001&hash=cm349ucKuc023b2ynGyv23ycr23

and in php file

并在 php 文件中

if(isset($_GET['hash']) && $_GET['hash']=='cm349ucKuc023b2ynGyv23ycr23'){
....
stuff to do
....
}

*you can even add specific time/date check when it should be run.
*you can check IP
*generate sha512 (I would recommend) hashes in both cron and php file with the same salt and maybe even time and then check if they are the same - it would be impossible for a hacker to recreate it - except if he somehow gets your original hash setup

*您甚至可以添加特定的时间/日期检查何时应该运行。
*您可以检查 IP
*在 cron 和 php 文件中使用相同的盐甚至时间生成 sha512(我会推荐)哈希,然后检查它们是否相同 - 黑客不可能重新创建它 - 除非他以某种方式获得了您的原始哈希设置