php中$_SERVER[PHP_SELF] 和 $_SERVER[SCRIPT_NAME]之间的区别
2016-12-19 16:03
453 查看
“PHP_SELF”
当前正在执行脚本的文件名,与 document root 相关。举例来说,在 URL 地址为 http://www.jb51.net/test.php/foo.bar 的脚本中使用 $_SERVER['PHP_SELF'] 将会得到 /test.php/foo.bar 这个结果。__FILE__ 常量包含当前(例如包含)文件的绝对路径和文件名。
“SCRIPT_NAME”
包含当前脚本的路径。这在页面需要指向自己时非常有用。__FILE__ 包含当前文件的绝对路径和文件名(例如包含文件)。
主要的原因如像:$_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
找到更好的原因是:
今天在Dreamhost上安裝一套PHP程序時發現連接的地址都會多出一個cgi-system來,但是程序的config並沒有問題,查了查資料才發現是SCRIPT_NAME和PHP_SELF不同造成的問題。
通常在本機測試 $_SERVER['SCRIPT_NAME'] 跟 $_SERVER['PHP_SELF'] 大概看不出有什麼不同,因為大部分的PHP不是以CGI模式運行的。
但 DreamHost 上的 PHP 是以 CGI 方式運行,二者就有明顯不同的差異。
echo $_SERVER['SCRIPT_NAME']; // (/cgi-system/php.cgi)
echo $_SERVER['PHP_SELF']; // (/admin/test.php)
从http://lists.nyphp.org/pipermail/talk/2005-July/015339.html 发现了一个说明。老外说的。
SCRIPT_NAME solves all the problems mentioned
in this thread - it's just the script name, without any extra garbage
that might be tacked on by the user. PHP_SELF explicitly includes that
extra garbage, so solutions in this thread that involve stripping the
garbage off of PHP_SELF to make it safe are really, really missing the
point - just use SCRIPT_NAME instead. Please don't use FORM ACTION=”";
according to the spec, what the browser does with that is undefined, so
even if it works in current browsers, it might not work in future ones
当前正在执行脚本的文件名,与 document root 相关。举例来说,在 URL 地址为 http://www.jb51.net/test.php/foo.bar 的脚本中使用 $_SERVER['PHP_SELF'] 将会得到 /test.php/foo.bar 这个结果。__FILE__ 常量包含当前(例如包含)文件的绝对路径和文件名。
“SCRIPT_NAME”
包含当前脚本的路径。这在页面需要指向自己时非常有用。__FILE__ 包含当前文件的绝对路径和文件名(例如包含文件)。
主要的原因如像:$_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
找到更好的原因是:
今天在Dreamhost上安裝一套PHP程序時發現連接的地址都會多出一個cgi-system來,但是程序的config並沒有問題,查了查資料才發現是SCRIPT_NAME和PHP_SELF不同造成的問題。
通常在本機測試 $_SERVER['SCRIPT_NAME'] 跟 $_SERVER['PHP_SELF'] 大概看不出有什麼不同,因為大部分的PHP不是以CGI模式運行的。
但 DreamHost 上的 PHP 是以 CGI 方式運行,二者就有明顯不同的差異。
echo $_SERVER['SCRIPT_NAME']; // (/cgi-system/php.cgi)
echo $_SERVER['PHP_SELF']; // (/admin/test.php)
从http://lists.nyphp.org/pipermail/talk/2005-July/015339.html 发现了一个说明。老外说的。
SCRIPT_NAME solves all the problems mentioned
in this thread - it's just the script name, without any extra garbage
that might be tacked on by the user. PHP_SELF explicitly includes that
extra garbage, so solutions in this thread that involve stripping the
garbage off of PHP_SELF to make it safe are really, really missing the
point - just use SCRIPT_NAME instead. Please don't use FORM ACTION=”";
according to the spec, what the browser does with that is undefined, so
even if it works in current browsers, it might not work in future ones
相关文章推荐
- svn + post-commit 实时测试php代码
- thinkphp自定义类
- php+sqlserver之如何连接sqlserver数据库
- php利用file_get_contents函数抓取网页指定内容
- NTP服务
- NTP服务
- 太公分肉算法
- Ubuntu 16 Install NTP
- PHP保存base64
- 搞不清FastCgi与PHP-fpm之间是个什么样的关系?
- 初识PHP(10)
- php读取xml文件,并存入数据库
- JS与PHP在函数传参方面有点不同,PHP形参与实参个数要匹配,而JS就灵活多了,可以随意传参,实参比形参少或多都不会报错
- tableLayoutPanel的使用
- php开启openssl的方法
- PHP PDO的setAttribute函数
- php 浏览器URL加密函数urlencode 和rawurlencode函数
- php 两段文本对比,不同的文字显示高亮
- windows 上搭建 sftp 服务器 --freesshd
- CuteFtp注册码-序列号