PHP中"PHP_SELF、SCRIPT_NAME、REQUEST_URI"有什么区别?

        最近用PHP获取网址,百度用到了这三个函数,之前只知道PHP_SELF和SCRIPT_NAME,百度一看居然还有个类似的。

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

        1、$_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’]。

        2、$_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只写到”/”,就返回 “/”

        3、$_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

 

        在我看来,都差不多,都能用。记录一下。

 

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
Don't give up just because of what people said. Use that as your motivation to push harder.
别因为别人说的话而放弃,把那些话当做加倍努力的动力
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片快捷回复

    暂无评论内容