多语言展示
当前在线:1667今日阅读:176今日分享:34

类discuz_application中_get_script_url方法

获得相对于网站根目录的路径及 PHP 程序文件名称。这个算法想不通为啥这样做,难以理解。测试了四个例子,都执行了if体内第一个if,返回值都是/test/index.php。其他if是在满足啥条件下执行?http://localhost/test/ http://localhost/test/index.php  http://localhost/test/index.php?a=test  http://localhost/test/index.php/a/test
工具/原料

discuz

方法/步骤
1

$scriptName = basename($_SERVER['SCRIPT_FILENAME']);       $_SERVER['SCRIPT_FILENAME'],当前执行程序的绝对路径及文件名。小编这儿返回E:/PHPWWW/test/index.php。basename 返回路径中的文件名部分。小编这儿结果$scriptName =’index.php’。

2

if(basename($_SERVER['SCRIPT_NAME']) === $scriptName) {        $this->var['PHP_SELF'] = $_SERVER['SCRIPT_NAME'];}       $_SERVER['SCRIPT_NAME'],相对于网站根目录的路径及 PHP 程序文件名称。小编这儿返回/test/index.php。小编这儿条件成立。

3

elseif(basename($_SERVER['PHP_SELF']) === $scriptName) {       $this->var['PHP_SELF'] = $_SERVER['PHP_SELF'];}       $_SERVER['PHP_SELF'],相对于网站根目录的路径及 PHP 程序文件名称。小编这儿返回/test/index.php。小编这儿条件成立。

5

elseif(isset($_SERVER['DOCUMENT_ROOT'])&&strpos($_SERVER['SCRIPT_FILENAME'],$_SERVER['DOCUMENT_ROOT']) === 0) {       $this->var['PHP_SELF']=str_replace('\\','/',str_replace($_SERVER['DOCUMENT_ROOT'],'',$_SERVER['SCRIPT_FILENAME']));      $this->var['PHP_SELF'][0]!='/'&&$this->var['PHP_SELF']='/'.$this->var['PHP_SELF'];}       $_SERVER['DOCUMENT_ROOT']根路径。$this->var['PHP_SELF'][0]这个二维数组哪儿来的?这不是赋值语句而是判断语句。虽然没测试过,但是应该不会执行到这儿吧?

6

$_SERVER['SCRIPT_NAME']和$_SERVER['PHP_SELF']区别,从下面例子可以看出区别在url在pathinfo。 $_SERVER['SCRIPT_NAME']http://localhost/test/    /test/index.phphttp://localhost/test/index.php    /test/index.phphttp://localhost/test/index.php?a=test /test/index.phphttp://localhost/test/index.php/a/test /test/index.php$_SERVER['PHP_SELF']http://localhost/test/    /test/index.phphttp://localhost/test/index.php    /test/index.phphttp://localhost/test/index.php?a=test   /test/index.phphttp://localhost/test/index.php/a/test    /test/index.php/a/test

推荐信息