博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
REQUEST_URL PHP_SELF SCRIPT_NAME区别
阅读量:3636 次
发布时间:2019-05-21

本文共 2727 字,大约阅读时间需要 9 分钟。

http://www.cnblogs.com/zcy_soft/archive/2010/10/16/1853239.html

$_SERVER[PHP_SELF], $_SERVER[SCRIPT_NAME], $_SERVER['REQUEST_URI'] 在用法上是非常相似的,他们返回的都是与当前正在使用的页面地址有关的信息,这里列出一些相关的例子,帮助确定哪些是在你的脚本最适合的。

$_SERVER[’PHP_SELF’]

  • http://www.yoursite.com/example/ — – — /example/index.php
  • http://www.yoursite.com/example/index.php — – — /example/index.php
  • http://www.yoursite.com/example/index.php?a=test — – — /example/index.php
  • http://www.yoursite.com/example/index.php/dir/test — – — /dir/test

当我们使用$_SERVER['PHP_SELF']的时候,无论访问的URL地址是否有index.php,它都会自动的返回 index.php.但是如果在文件名后面再加斜线的话,就会把后面所有的内容都返回在$_SERVER['PHP_SELF']。

$_SERVER['REQUEST_URI']

  • http://www.yoursite.com/example/ — – — /
  • http://www.yoursite.com/example/index.php — – — /example/index.php
  • http://www.yoursite.com/example/index.php?a=test — – — /example/index.php?a=test
  • http://www.yoursite.com/example/index.php/dir/test — – — /example/index.php/dir/test

$_SERVER['REQUEST_URI']返回的是我们在URL里写的精确的地址,如果URL只写到”/”,就返回 “/”

$_SERVER['SCRIPT_NAME']

  • http://www.yoursite.com/example/ — – — /example/index.php
  • http://www.yoursite.com/example/index.php — – — /example/index.php
  • http://www.yoursite.com/example/index.php — – — /example/index.php
  • http://www.yoursite.com/example/index.php/dir/test — – — /example/index.php

在所有的返回中都是当前的文件名/example/index.php

http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/09/13/2175071.html

对于下面这个网址:

http://www.test.com/index.php/foo/bar.html?c=index&m=search

我们可以得到 $_SERVER['PATH_INFO'] = ‘/foo/bar.html’,而此时 $_SERVER['QUERY_STRING'] = 'c=index&m=search';

通 常,我们最初开始PHP程序编写的时候,都会使用诸如: http://www.test.com/index.php?c=search&m=main 这样的URL,这种URL不仅看起来非常奇怪,而且对于搜索引擎也是非常不友好的。很多搜索引擎收录的时候,都会忽略Query String之后的内容,google虽然不会忽略Query String,但是对于其他不含Query String的页面,会给于比较高的PR值。

示例说明

    location /newapp/ {                                                                                                                                

        root /home/users/pengzhi/odp/webroot;                                                                                                          
        index mp.php;                                                                                                                                  
        fastcgi_pass $php_upstream;                                                                                                                    
        #fastcgi_index   mp.php;# 已经排除请求fast_cig的url是 / 则添加index.php的情况                                                                  
        include fastcgi.conf;                                                                                                                          
        rewrite ^/newapp(/[^\?]*)?((\?.*)?)$ /newapp/index.php$1$2 break; #跳出只处理 server块内容 即使其他location更加匹配重写后的uri                                                                            
        #last 会重新处理这个server 不会出来后面的                                                                                                      
      }        
      http://xxxxxx:8125/newapp/Main/index3
     "SCRIPT_FILENAME": "/home/users/pengzhi/odp/webroot/newapp/index.php",
     "PATH_INFO": "/Main/index3",h
     "SCRIPT_NAME": "/newapp/index.php",
     "REQUEST_URI": "/newapp/Main/index3",
     "DOCUMENT_URI": "/newapp/index.php/Main/index3",
     "DOCUMENT_ROOT": "/home/users/pengzhi/odp/webroot",
     "ORIG_PATH_INFO": "/newapp/index.php/Main/index3", 
     "ORIG_SCRIPT_NAME": "/newapp/index.php/Main/index3",
     "ORIG_SCRIPT_FILENAME": "/home/users/pengzhi/odp/webroot/newapp/index.php/Main/index3",
     "PATH_TRANSLATED": "/home/users/pengzhi/odp/webroot/Main/index3",
     "PHP_SELF": "/newapp/index.php/Main/index3",

总结一下 SCRIPT_NAME + PATH_INFO = PHP_SELF

你可能感兴趣的文章
JavaWeb框架 - Spring注解部分:
查看>>
SpringBoot使用外部的Tomcat: bean with name 'defaultValidator' defined in class path resource
查看>>
SpringBoot上传文件413问题:
查看>>
Java多线程 - AQS简单实现:
查看>>
建造者模式:
查看>>
适配器模式:
查看>>
LinkedList源码分析
查看>>
美团Java一面面经
查看>>
疏漏总结(九)——http与https
查看>>
疏漏总结(十)
查看>>
线程池
查看>>
servlet的其他作用,git的使用方法
查看>>
Oracle数据库sql*plus常用命令
查看>>
Oracle中表的简单查询
查看>>
Linux-进程管理
查看>>
Linux-ssh服务及服务管理、文件传输
查看>>
Linux-网络配置
查看>>
开发中浏览器兼容的问题总结
查看>>
Vue初体验
查看>>
Vue学习之二(vue指令)
查看>>