我正在开发一个 PHP 网站,并将其移至新服务器。我要迁移到的新服务器不具有 CRON 兼容性。为了弥补这一点,我设计了一个使用时间格式和数据库表等的系统来运行我的代码。
我遇到的问题是这段代码:
if ($lasttime < $pretime)
{
$newtime = strtotime("now");
queryMysql("UPDATE time SET time=".$newtime." WHERE time=".$lasttime);
include_once 'grabber/grabber.php';
}
具体来说,是 include_once 'grabber/grabber.php';
导致了问题。当计时器到来并且此代码运行时,它到达包含,然后代码停止,没有提供错误,因此包含失败。我尝试将其更改为 exec()
但说实话,我并不完全理解 exec()
的工作原理以及它是否是正确的做法。这就是我使用它的方式:
if ($lasttime < $pretime)
{
$newtime = strtotime("now");
queryMysql("UPDATE time SET time=".$newtime." WHERE time=".$lasttime);
$grabber = $base."grabber/grabber.php";
exec($grabber);
}
这不会停止代码并且似乎可以运行,但实际上不起作用,如果 grabber/grabber.php
正确运行,那么我会收到一封电子邮件以确认使用 PHP mail()
函数
如果有人能帮助我解决这个问题或提供一些线索,那就太好了。
谢谢。
I am working on a PHP website, and I am moving it over to a new server. The new server I am moving to does not have CRON compatibility. To compensate for this I have devised a system using time formats and database tables and more to run my code instead.
What I am having a problem with is this bit of code:
if ($lasttime < $pretime)
{
$newtime = strtotime("now");
queryMysql("UPDATE time SET time=".$newtime." WHERE time=".$lasttime);
include_once 'grabber/grabber.php';
}
Specifically it's the include_once 'grabber/grabber.php';
which is causing the problem. When the timer comes round and this code runs, it gets to the include and then the code stops, with no error provided, so the include fails. I have tried changing it to an exec()
but to be honest I don't completely understand how exec()
works and if it is the correct thing to do. This is how I used it:
if ($lasttime < $pretime)
{
$newtime = strtotime("now");
queryMysql("UPDATE time SET time=".$newtime." WHERE time=".$lasttime);
$grabber = $base."grabber/grabber.php";
exec($grabber);
}
This does not stop the code and seems to run but it doesn't actually work, if grabber/grabber.php
runs correctly then I get an email to confirm using the PHP mail()
function
If anyone could help me solve this or shed some light that would be brilliant.
Thanks.
发布评论
评论(2)
(假设你的服务器是 *nix)如果你想使用
exec()
你需要放置一个 hashbang”指向 PHP 可执行文件并授予其执行权限。或者(这可能是更好/更便携的方法),更改
为
...就好像您从终端运行它一样。
但是,我怀疑这能否解决问题 - 我认为答案更有可能是以下之一:
grabber.php
尝试使用
include_once
,但执行ini_set('display_errors',1); error_reporting(-1);
以确保您确实看到任何错误。你怎么称呼你的主脚本?您将如何看到这些错误?使用此信息编辑问题,您认为可能相关的grabber.php
中的任何代码,我将扩展此答案。(Assuming your server is *nix) If you want to use
exec()
you need to place a hashbang at the top of the script that points to the PHP executable and give it execute permissions.Or (this is probably the better/more portable approach), change
to
...as if you were running it from a terminal.
However, I doubt this will solve the problem - I think the answer is more likely to be one of these things:
grabber.php
. Keep in mind that there are slight syntax differences between major PHP versions - if your PHP version is different on your old/new hosts, this may be the problem.grabber.php
was corrupted during the move between serversTry it with the
include_once
, but doini_set('display_errors',1); error_reporting(-1);
to make sure you actually see any errors. How are you calling you main script? How will you see the errors? Edit the question with this info, any code fromgrabber.php
you think may be relevant and I will expand this answer.这很可能是文件位置或权限的问题。应该存在某种错误,或者代码没有停止,但您没有正确检查,或者
grabber.php
本身的代码存在某种问题。添加一些调试行 - 打印文件名,以便您可以检查路径/名称中的错误;添加错误报告(E_ALL); ini_set('display_errors', true);include_once
行上方某处;确保该文件位于您尝试打开它的位置,考虑相对路径等。确保您有权运行该文件。在这种情况下, exec() 不是您所需要的,至少不是您尝试使用它的方式。
如果这没有帮助 - 请提供更多有关如何运行所显示的脚本、
grabber.php
文件中的内容、遇到的错误等的更多信息。This is most probably an issue with the file location or permissions. There should be some kind of error, or the code doesn't stop, but you don't properly check that or there is some kind of an issue with the code in
grabber.php
itself. Add some debugging lines - print the filename, so you can check for errors in the path/name; adderror_reporting(E_ALL); ini_set('display_errors', true);
somewhere above theinclude_once
line; make sure the file is where you're trying to open it from, taking into account relative paths, etc. Make sure you have permissions to run this file.exec() is not what you need in this case, at least not in the way that you're trying to use it.
If that doesn't help - give some more information about how you run the scripts that you've shown, what's in the
grabber.php
file, what errors you get, etc.