我認爲最好的方法是採用MVC風格的URL操作,而不是使用參數。
在你的htaccess使用,如:
<IfModule mod_rewrite.c>
RewriteEngine On
#Rewrite the URI if there is no file or folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
然後在你的PHP腳本,你要開發一個小型的類來讀取URI和它分割成段,如
class URI
{
var $uri;
var $segments = array();
function __construct()
{
$this->uri = $_SERVER['REQUEST_URI'];
$this->segments = explode('/',$this->uri);
}
function getSegment($id,$default = false)
{
$id = (int)($id - 1); //if you type 1 then it needs to be 0 as arrays are zerobased
return isset($this->segments[$id]) ? $this->segments[$id] : $default;
}
}
使用像
http://mysite.com/posts/22/robert-pitt-shows-mvc-style-uri-access
$Uri = new URI();
echo $Uri->getSegment(1); //Would return 'posts'
echo $Uri->getSegment(2); //Would return '22';
echo $Uri->getSegment(3); //Would return 'robert-pitt-shows-mvc-style-uri-access'
echo $Uri->getSegment(4); //Would return a boolean of false
echo $Uri->getSegment(5,'fallback if not set'); //Would return 'fallback if not set'
現在MVC還有平時喜歡http://site.com/controller/method/param,但在非MVC風格的應用程序,你可以做http://site.com/action/sub-action/param
希望這有助於你與你的應用向前發展。
+1或更好的使用MVC;)。 – 2010-06-29 21:57:21
是的,我會解釋說,對他來說,但似乎他已經中途拋出他的應用程序,所以只給了最好的答案,而無需重新編碼所有的應用程序。 – RobertPitt 2010-06-29 21:59:19
@RobertPitt - 是的,我把這個網站放在2004年,隨着時間的推移,如果我不得不再做一遍,我會使用一個框架。但從現在開始,像這樣的影響會更小。非常感謝。 – 2010-06-29 22:03:50