PHP_SELF、 SCRIPT_NAME、 REQUEST_URI區別,requesturi

來源:互聯網
上載者:User

PHP_SELF、 SCRIPT_NAME、 REQUEST_URI區別,requesturi

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

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


PHP預定義變數$_SERVER中的PHP_SELF與SCRIPT_FILENAME有什詳細不同?

SCRIPT_FILENAME指向當前執行指令碼的絕對路徑;PHP_SELF當前正在執行指令碼的檔案名稱,兩者經常混淆,舉一個例子你就明白了,假如D盤web檔案夾為根目錄,web下的htdocs檔案夾有檔案a.php,語句如下:
<?php
include(common/b.php);
?>,
htdocs檔案下common目錄b.php,語句如下:
<?php
echo $_SERVER['PHP_SELF'];
echo "<br />";
echo $_SERVER['SCRIPT_FILENAME'];
?>
運行a.php得出結果:
/htdocs/in.php
D:/web/htdocs/in.php
可以看出PHP_SELF只輸出以根目錄為開始網站檔案路徑,但是SCRIPT_FILENAME輸出的是所在硬碟的檔案絕對路徑,可以認為一個是輸出網路檔案路徑,一個是檔案所在路徑.

SCRIPT_NAME指的是當前指令檔名,比如b.php有echo $_SERVER['SCRIPT_NAME'];那麼輸出是/htdocs/common/b.php,明白了,會直接把當前語句所在指令檔路徑輸出。
 
$_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']這條語句的意思?

有時候$PHP_SELF沒有定義,這個與INI設定有關,

當$PHP_SELF不存在的時候,就要設定
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.