你加入我的網站。你的用戶名是steve_jobs。我的PHP網站如何處理www.mywebsite.com/customer_name請求?
我想有你在這個網址訪問您的內容:www.mywebsite.com/steve_jobs
這個網址是網址的功能相當於:www.mywebsite.com/user_page.php?user_id=4 ,其中steve_jobs的user_id主鍵爲4.
如何將該功能構建到我的php應用程序中?那可能嗎?
謝謝你的時間。
你加入我的網站。你的用戶名是steve_jobs。我的PHP網站如何處理www.mywebsite.com/customer_name請求?
我想有你在這個網址訪問您的內容:www.mywebsite.com/steve_jobs
這個網址是網址的功能相當於:www.mywebsite.com/user_page.php?user_id=4 ,其中steve_jobs的user_id主鍵爲4.
如何將該功能構建到我的php應用程序中?那可能嗎?
謝謝你的時間。
首先在你的user_page.php中,你需要獲得用戶名而不是user_id,因爲你想使用url的用戶名,你需要創建一個函數來獲取這樣的信息:使用PDO作爲例。
function getInfo()
{
$sth = $this->db->prepare("SELECT * from table where user_id = :getusername")
$sth->execute(array(':getusername' = $_GET['username']));
}
//this goes inside your user_page.php
if(isset($_GET['username']))
{
//retrieve data based on the user_id
}
然後在htaccess中你會添加這段代碼。
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user_page.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user_page.php?username=$1
允許您通過www.site.com/username訪問您的用戶信息(有人糾正我,如果我錯了)。
感謝Attis。我會讓你知道它是如何工作的。我從來沒有使用或聽說過URL重寫,所以我確信我會絆倒我的解決方案。 –
你的解決方案很好,Attis。謝謝。在我問這個問題之前,我從未聽說過「.htaccess」或「url重寫」,所以我找不到被描述爲重複的帖子。然後,這個線程可以 - 證明它的存在 - 是對處理「可變url」的技術的存在的介紹。 –
歡迎您!接受答案,因爲它已經幫助你;) – iBrazilian2
而不是試圖從頭開始,抓住一個像Codeigniter這樣的框架,爲您做到這一點。 – Zarathuztra
使用URL重寫,並從您的數據庫中查找用戶名以更改爲代碼中的數字ID。 – mario
嗨Zarazthuztra。我已經建立了一個php網站。我不是從頭開始的。我必須使用php來解決這個問題。 –