在 linux/apache 服务器上的 cron 作业上运行 script.php,但限制对 php 文件的公共访问
我有这个 script.php 文件,我想在我的 linux/apache 服务器上作为 cron 作业运行。
但是,我不希望公众访问 www.mycompanyname.com/script.php 并同时运行脚本。
我们怎样才能防止这种情况发生呢?我们如何限制脚本只能访问服务器? 是使用 chmod 还是在 .htaccess 文件中设置某些内容?
有什么建议吗?
I have this script.php file which i want to run as a cron job on my linux/apache server.
However, i do not want public to access www.mycompanyname.com/script.php and also run the script concurrently.
How can we prevent that? How can we restrict the script to the server's access only?
Is it using chmod or setting something inside .htaccess file, something along the line?
Any advice ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将其作为
script.php
中 PHP 的第一行执行...如果有人通过 HTTP 访问您的脚本,
PHP_SAPI
将是cgi
我相信,而不是cli
,导致你的脚本立即退出。当然,这依赖于您的 cron 调用
php script.php
。您也可以发送...
...或者当然,将其留在您的网络根目录之外。
You can do this as the first line of PHP in
script.php
...If someone hits your script via HTTP, the
PHP_SAPI
will becgi
I believe, and notcli
, causing your script to exit straight away.Of course, this relies on your cron calling
php script.php
.You could also send...
... or of course, leave it outside your web root.
如果您将脚本放在 webroot 文件夹之外,则将无法通过您的网络服务器访问该脚本。
例如
您的网站根目录位于 /var/www/public_html/
您将 script.php 放在该文件夹之外,例如:/var/www/
If you put the script outside of the webroot folder it will not be accessible through your webserver.
e.g.
your webroot is at /var/www/public_html/
you put the script.php outside of that folder, for example: /var/www/
您可能有一个类似
public_html
目录,其中包含所有 phps.txt 文件。只需将其放在该目录之外即可。You probably have something like a
public_html
directory, in which you have all the phps. Just put it outside of that directory.方法 1.
如果您直接从 Cron 作业执行 PHP,例如 php /path/to/your_script.php,则在 PHP 脚本顶部添加以下行:
方法 2.< /strong>
如果 Cron 作业使用 wget、curl 或 lynx 通过其 URL 运行您的脚本,则将此代码插入 PHP 脚本的顶部(将用户代理字符串更改为只有您知道的字符串):
您还将拥有在 Cron Job 中设置 User Agent;如以下 wget 示例所示:
Method 1.
If you are executing PHP directly from the Cron Job e.g. php /path/to/your_script.php then add the following line at the top of your PHP script:
Method 2.
If the Cron Job uses wget, curl or lynx to run your script via its URL, then insert this code at the top of your PHP script (change the User Agent string to one known only by you) :
You will also have to set the User Agent in the Cron Job; as in this wget example: