How to populate SCRIPT_FILENAME and SCRIPT_PATH server vairables to php-cgi from the command line

梦想与她 提交于 2019-12-11 14:19:16

问题


I'm trying to run cron php script from command line. The server does not have PHP CLI but php-cgi exists. I tried to pass PHP directly to php-cgi but this does not work because two server environment variables are missed ($_SERVER['SCRIPT_FILENAME'] and $_SERVER['SCRIPT_PATH']). I tried to fill variables using following recommendation but these variables are not populated. What is missed in this answer? How to make php-cgi populate these server values? The question is not platform specific, but execution script looks like this currently

#!/bin/bash

export REDIRECT_STATUS=200
export GATEWAY_INTERFACE="CGI/1.1"
export SCRIPT_FILENAME=/usr/local/www/owncloud/cron.php
export REQUEST_METHOD="GET"

/usr/local/bin/php-cgi 

Please note that I'm not asking for workaround, I already added helper php file which fill necessary values. But this is temporary solution because next software update could show another issue with php-cgi call

UPDATE

It seems the issue is not with php-cgi execution but with the SCRIPT_FILENAME and SCRIPT_PATH server variables only. So solution works in common cases when you do not need them. Also the issue might be version specific (it works with PHP version prior to 5.5) or platform specific


回答1:


I searched SO with question to these specific server variables and found following statement here:

You are just juggling variables now. SCRIPT_FILENAME is a part of the CGI spec. It will not be available if PATH_INFO is unavailable. As for REQUEST_URI, it's apache's mod_rewrite specific. – LiraNuna

So exporting PATH_INFO variable also populates values from SCRIPT_FILENAME and SCRIPT_PATH environment variables. Please note that SCRIPT_FILENAME is needed still to point php-cgi to php input file. Below is the final script

#!/bin/bash

export REDIRECT_STATUS=200
export GATEWAY_INTERFACE="CGI/1.1"
export REQUEST_METHOD="GET"
export SCRIPT_FILENAME=/usr/local/www/owncloud/cron.php
export SCRIPT_PATH=cron.php
export PATH_INFO=$SCRIPT_FILENAME

/usr/local/bin/php-cgi 



回答2:


Here is what i tested. i used root account on my home PC. My IP is masked from the output. cannot post this much as comment so posting as answer.

The script

#!/bin/bash

export REDIRECT_STATUS=200
export GATEWAY_INTERFACE="CGI/1.1"
export SCRIPT_FILENAME=/root/t.php
export REQUEST_METHOD="GET"

/usr/bin/php-cgi

PHP file

<?php
print_r($_SERVER);
?>

Output I get

X-Powered-By: PHP/5.3.8
Content-type: text/html

Array
(
    [HOSTNAME] => HomePC
    [SELINUX_ROLE_REQUESTED] =>
    [SHELL] => /bin/bash
    [TERM] => xterm
    [HISTSIZE] => 1000
    [SSH_CLIENT] => xx.xx.xx.xx 58196 22
    [SELINUX_USE_CURRENT_RANGE] =>
    [SSH_TTY] => /dev/pts/3
    [USER] => root
    [LS_COLORS] => rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.tbz=01;31:*.tbz2=01;31:*.bz=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
    [SCRIPT_FILENAME] => /root/t.php
    [PATH] => /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
    [MAIL] => /var/spool/mail/root
    [PWD] => /root
    [LANG] => en_US.UTF-8
    [REDIRECT_STATUS] => 200
    [SELINUX_LEVEL_REQUESTED] =>
    [HISTCONTROL] => ignoredups
    [HOME] => /root
    [SHLVL] => 2
    [LOGNAME] => root
    [SSH_CONNECTION] => xxx.xxx.xx.xxx 58196 192.168.1.2 22
    [GATEWAY_INTERFACE] => CGI/1.1
    [LESSOPEN] => ||/usr/bin/lesspipe.sh %s
    [REQUEST_METHOD] => GET
    [G_BROKEN_FILENAMES] => 1
    [_] => /usr/bin/php-cgi
    [PHP_SELF] =>
    [REQUEST_TIME] => 1379952948
)


来源:https://stackoverflow.com/questions/18962960/how-to-populate-script-filename-and-script-path-server-vairables-to-php-cgi-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!