让 PHP 处理在后台进行

发布于 2024-12-11 22:49:20 字数 923 浏览 0 评论 0 原文

我正在开发一个 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筱武穆 2024-12-18 22:49:21

(假设你的服务器是 *nix)如果你想使用 exec() 你需要放置一个 hashbang”指向 PHP 可执行文件并授予其执行权限。

或者(这可能是更好/更便携的方法),更改

$grabber = $base."grabber/grabber.php";    
exec($grabber);

$grabber = "php ".$base."grabber/grabber.php";    
exec($grabber);

...就好像您从终端运行它一样。

但是,我怀疑这能否解决问题 - 我认为答案更有可能是以下之一:

  • grabber.php 中的解析错误。请记住,主要 PHP 版本之间存在细微的语法差异 - 如果您的旧/新主机上的 PHP 版本不同,这可能就是问题所在。
  • 由于 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

$grabber = $base."grabber/grabber.php";    
exec($grabber);

to

$grabber = "php ".$base."grabber/grabber.php";    
exec($grabber);

...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:

  • A parse error in 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.
  • A call to a function that was defined on your old host but not on your new host, because of a difference in PHP version or installed extensions
  • grabber.php was corrupted during the move between servers

Try it with the include_once, but do ini_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 from grabber.php you think may be relevant and I will expand this answer.

依 靠 2024-12-18 22:49:20

这很可能是文件位置或权限的问题。应该存在某种错误,或者代码没有停止,但您没有正确检查,或者 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; add error_reporting(E_ALL); ini_set('display_errors', true); somewhere above the include_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文