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

2023-08-19php开发问题
5

本文介绍了如何使用 cron 作业运行 php 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我每天都使用 cron 作业在 php 页面上运行电子邮件计划.当我使用链接运行页面时,php 代码运行良好.

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

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

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

解决方案

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

您需要使用如下命令:

php/home/USER/public_html/cron.php

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

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

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

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.

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.

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

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 runs commands as they would be ran via the shell, so running PHP would use local paths.

You need to use a command like:

php /home/USER/public_html/cron.php

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

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 作业运行 php 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17