簡體   English   中英

創建url而不是表單操作(例如wordpress slug而不是pageid)

[英]create url instead of form action (like wordpress slug instead of pageid)

是否可以在PHP中創建唯一的URL結構而不是表單操作。 我已經為我的網站創建了“事件”部分,並且從數據庫中檢索了值,並使用sql查詢按id顯示了值。 在這里,我想通過標題獲取值,而不是表單操作ID示例

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

這取決於。 如果您使用的是Apache,則可以使用.htaccess重寫URL並將其指向PHP腳本。

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

這將采用例如example.com/eventshow/1234類的URL,並將其重定向到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進行完整性檢查,然后檢查該函數是否存在,添加一些權限檢查以及輸入過濾器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM