2010-02-12 138 views
3

我建立了我的網站簡單的管理方面,我想的URL在一定程度上是這樣的:PHP交換機GET請求

http://mysite.com/admin/?home 
http://mysite.com/admin/?settings 
http://mysite.com/admin/?users 

但我不知道我怎麼會檢索正在請求的頁面然後顯示所需的頁面。我在開關嘗試這樣:

switch($_GET[]) 
{ 
    case 'home': 
     echo 'admin home'; 
     break; 
} 

但我得到這個錯誤:

Fatal error: Cannot use [] for reading in C:\path\to\web\directory\admin\index.php on line 40

有沒有解決這個辦法嗎?我想避免給GET請求設置一個值,如:

http://mysite.com/admin/?action=home 

如果你知道我的意思。謝謝。 :)

回答

11

使用$_SERVER['QUERY_STRING'] –包含後位?

switch($_SERVER['QUERY_STRING']) { 
    case 'home': 
     echo 'admin home'; 
     break; 
} 

你甚至可以進一步採取這種方法,並有網址是這樣的:

http://mysite.com/admin/?users/user/16/ 

剛使用explode()將查詢字符串拆分爲段,獲取第一個並通過休息作爲方法的參數:

$args = explode('/', rtrim($_SERVER['QUERY_STRING'], '/')); 
$method = array_shift($args); 

switch($method) { 
    case 'users': 
     $user_id = $args[2]; 

     doSomething($user_id); 
     break; 
} 

此方法在很多采用MVC模式的框架中很流行。完全擺脫?的另一個步驟是在Apache服務器上使用mod_rewrite,但我認爲這個問題有點超出範圍。

+0

什麼,如果有更多的增值經銷商除了頁面瓦爾? – Sarfraz 2010-02-12 18:58:44

+0

你甚至不需要mod_rewrite; '$ _SERVER ['PATH_INFO']'將包含URL中腳本之後的所有內容。 – 2010-02-12 19:02:19

+0

@Ignacio,這是真的,但你需要那裏的'index.php'部分。 – 2010-02-12 19:03:43

1

$_SERVER['QUERY_STRING']

2

還有那些提到的,另一種選擇是key($_GET),這將返回$ _GET數組的第一個關鍵這將意味着它會使用網址與其他參數

www.example.com/?home&myvar = 1;

的一個問題是如果您已將陣列指針修改爲key,則可能首先在陣列上首先使用reset()返回元素數組指針當前指向的鍵。

0

通過使用$_SERVER['REQUEST_URI']變量,可以使鏈接「看起來更好」。

這將允許您使用的網址,如:

http://mysite.com/admin/home 
http://mysite.com/admin/settings 
http://mysite.com/admin/users 

使用的PHP代碼:

// get the script name (index.php) 
$doc_self = trim(end(explode('/', __FILE__))); 

/* 
* explode the uri segments from the url i.e.: 
* http://mysite.com/admin/home 
* yields: 
* $uri_segs[0] = admin 
* $uri_segs[1] = home 
*/ 

// this also lower cases the segments just incase the user puts /ADMIN/Users or something crazy 
$uri_segs = array_values(array_filter(explode('/', strtolower($_SERVER["REQUEST_URI"])))); 
if($uri_segs[0] === (String)$doc_self) 
{ 
    // remove script from uri (index.php) 
    unset($uri_segs[0]); 
} 
$uri_segs = array_values($uri_segs); 

// $uri_segs[1] would give the segment after /admin/ 
switch ($uri_segs[1]) { 
    case 'settings': 
     $page_name = 'settings'; 
     break; 
    case 'users': 
     $page_name = 'users'; 
     break; 
    // use 'home' if selected or if an unexpected value is given 
    case 'home': 
    default: 
     $page_name = 'home'; 
     break; 
} 
1

是不是最「優雅」的方式來做到這一點,但最簡單的形式來回答你問題是..

 

    if (isset($_GET['home'])): 
     # show index.. 
    elseif (isset($_GET['settings'])): 
     # settings... 
    elseif (isset($_GET['users'])): 
     # user actions.. 
    else: 
     # default action or not... 
    endif; 

1

的PHP代碼:

switch($_GET){ 
case !empty($_GET['home']): 
    enter code here 
break; 

case !empty($_GET['settings']): 
    enter code here 
break; 

default: 
    enter code here 
break; 

}

+0

你的答案如何改進以前接受和upvoted的答案?只有代碼的答案是可以的,但向代碼添加註釋會增加其對未來讀者的實用性。 – Yaroslav 2012-10-11 11:44:52

+0

case true: case false: – Zerquix18 2014-07-01 02:41:17