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 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题