Why do some PHP installations have $_SERVER['SCRIPT_URI'] and others not?

回眸只為那壹抹淺笑 提交于 2019-12-30 22:54:55

问题


I run two Apache 2 servers. One has PHP5.2 and the other has PHP5.3. Is there a reason why on the 5.3 machine has $_SERVER['SCRIPT_URI']?

Where does this variable come from? It is clearly something that is coming through from the Apache environment and it is not documented in the PHP manual. It is however a handy shortcut over a combination of ['HTTPS'], ['SERVER_NAME'] and ['REQUEST_URI'].

I have tried looking through configuration files, searching SO and the web.


回答1:


According to a post on WebHostingTalk it comes from mod_rewrite:

Add

RewriteEngine On

To the virtual host in your httpd.conf file that you want to turn this on for and then restart apache.




回答2:


I moved to CentOS 7 with Plesk12, PHP 5.6.6 and rewrite on etc. SCRIPT_URI has not been there. And because it is so nice to use in some situations I wrote this workaround:

if(!isset($_SERVER['SCRIPT_URI'])){
    $_SERVER['SCRIPT_URI'] = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

    $pos = strrpos($_SERVER['SCRIPT_URI'],'/');
    if($pos!==false) {
        $_SERVER['SCRIPT_URI'] = substr($_SERVER['SCRIPT_URI'], 0, $pos+1);
    }
}

As I am not a total expert, please review this code to your specific application before plugging it in. On my system it seems to work perfectly. I just put it in the Head-Area of my index.php and others.




回答3:


As far as I know $_SERVER['SCRIPT_URI'] is only available if you're running PHP as a CGI. I suppose that must be the difference in your two PHP installations.



来源:https://stackoverflow.com/questions/8398391/why-do-some-php-installations-have-serverscript-uri-and-others-not

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