douqin6785 2011-09-09 21:07
浏览 63
已采纳

PHP exec在直接调用脚本时有效,但不是由jQuery ajax调用

I have a PHP script which calls exec(). I've been having trouble all day with some code calling the same script working and some not (exec() returns a 127 error code).

I have finally worked out that the code that is not working is the code that is being called from jQuery on my web page:

$('#next_button').click(function(event) {
    $.get('download_forms.php', function(data) {
        alert(data);
    });
});

However, if I type the url for download_forms.php into the address bar of my browser, then exec() will execute properly. I have tries to run other scripts that call exec() from jQuery to test and they all fail, but work if typed into the address bar.

I don't see why this would be an issue. Whether I type the url into Firefox's address bar, or whether pressing the button on my webpage, an HTTP request will be made.

Does anybody know what the difference could possibly be?

Note: I have tried different commands in exec() and they are all failing from my jQuery (note all the rest of the PHP code runs fine) but work when the script address is typed directly into the address bar.

Many thanks

Update

This is my download_forms.php code. The initial exec() was just to see if exec() worked at all. As above, it only executes properly if typed directly into the address bar.

include ('inc/session.inc.php');
require_once('Downloader.php');
exec('id', $output, $r);
echo var_dump($output);
echo($r);
try {
    $downloader = new Downloader();
    $saveMessages = $downloader->saveToDatabase();
    // exec() in the combineAndDownloadForms() method
    $downloadMessages = $downloader->combineAndDownloadForms();
} catch(Exception $e) {
    echo $e->getMessage();
}

Further Update

I made a hyperlink through from my webpage to the download_forms.php page (ie a <a>), but exec() still doesn't execute. At least I know it's nothing to do with ajax.

  • 写回答

1条回答 默认 最新

  • dsms21398 2011-09-16 08:11
    关注

    For what it's worth, I fixed this issue and all of the above was a red herring.

    I hadn't noticed that the page I was on was using the secure https protocol, so when the download_forms.php script was being used, it was also accessed using https and it seems that the exec() and passthru() functions won't execute commands on the server in these circumstances, which makes sense.

    I changed the script so that it changed the protocol of the download_forms.php url to ordinary http and it's now functioning fine.

    HTH

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示