douzhi9395 2013-11-16 14:40
浏览 82
已采纳

通过php启动tightvnc查看器

I'm trying to start vncviewer on Raspberry Pi using PHP, where RPi is the client and my VNC server runs MS Windows.

In PHP I'm using this script to start the vncviewer

$cmd="echo password | vncviewer -autopass " .$ip;
shell_exec($cmd);

It works when I only run the shell script from RPi, but when try it from PHP I get this error:

Error: Can't open display:

Is there any workaround for this error ?

  • 写回答

1条回答 默认 最新

  • donglv1831 2013-11-19 19:07
    关注

    If you're using X11, you probably need to set your DISPLAY variable.

    X11 clients (programs) that need to open windows on an X11 server (display) use this variable to determine where the client should find the server. The client/server model is used here because the server provides window display capabilities to the client.

    X includes the ability to display windows on remote servers across a network, though 99.9% of the time you'll be displaying things on the main display on the machine running the program, so usually, DISPLAY=localhost:0.0.

    The catch is that if DISPLAY is not set, a program doesn't know where to open its windows, even if there's an X server running on the same machine. And when programs are run by cron or the system startup scripts or in fact anything that was not launched by X itself (including a PHP script run by your web server), they usually don't have DISPLAY set to anything.

    So .. as you noted in comments, setting DISPLAY=:0 will tell vncviewer where to send its windows. This is a shortform of DISPLAY=localhost:0.0. The hostname, if excluded, is assumed to be localhost. And the .0 at the end refers to the screen number, which also defaults to zero. Check out the X documentation for more details.

    And as you've also noted in comments, in the environment where X is running (i.e. DISPLAY is already set), you may need to run xhost + to allow programs not launched by X to open windows.

    To set an environment variable in PHP, use something like:

    putenv("DISPLAY=:0");
    

    before the code that launches VNC.

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

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line