2014-03-05 89 views
0

是否可以在php中創建一個唯一的URL結構而不是表單動作。 我已經創建了我的網站的事件部分,值從數據庫檢索和顯示id使用SQL查詢。在這裏,我想他們的頭銜,而不是形式的活動ID 例如創建網址而不是表單操作(像wordpress slug而不是pageid)

// $row['title'] these values are fetched from database 
<a href="eventshow.php?no='. $row['no'] .'">' .$row['title']. '</a> 

i want to develop a URL like below instead of above one 

<a href="eventshow.php/eventtitle.php>' .$row['title']. '</a> 

像WordPress塞,而不是網頁和帖子ID

+0

您肯定有語法錯誤,請檢查您的引號。 – bobkingof12vs

回答

0

這取決於獲取一個值。如果您使用的是Apache,您可以使用.htaccess來重寫您的網址並將它們指向PHP腳本。

RewriteEngine on 
RewriteCond $1 !^(index\.php|robots\.txt|) 
RewriteRule ^(.*)$ /index.php/$1 [L] 

這將需要一個url像example.com/eventshow/1234,重定向到您的index.php,然後服務器變量$_SERVER['PATH_INFO']設置爲eventshow/1234。從那裏你可以解析PATH_INFO來確定應該調用哪個函數以及應該傳遞哪些值。 $_SERVER['PATH_INFO']最大的問題是它是通過無用的斜槓/所以你想確保修剪它們。

在您的例子我會寫這樣的:

的index.php

if(!empty(trim($_SERVER['PATH_INFO'],"/"))){ 
    //clean extraneous slashes and explode 
    $request = array_filter(explode("/",trim($_SERVER['PATH_INFO'],"/")); 
}else{ 
    //if no entity specified call index entity, I put this in to handle example.com/ requests. 
    $request = array("Index"); 
} 
$func = array_shift($request); 
$result = call_user_func_array($func, $request); 

給出的URL example.com/eventshow/1234最後一行將調用相當於:

$result = eventshow(1234); 

您可能還希望在調用$ func之前做一些理智的檢查,檢查函數是否存在,添加權限檢查以及輸入過濾器。

+0

謝謝先生@Tyson Nothwest – Raj

+0

我可以有你的電子郵件ID Mr. @Tyson – Raj

+0

這是怎麼回事? [email protected] –

相關問題