2013-06-27 35 views
-1

我需要實現一個功能:PHP,處理的唯一URL

  1. 用戶希望在網站上註冊
  2. 用戶按下一個按鍵「註冊我快」
  3. 腳本生成像一個獨特的鏈接「 mydomain.com/user/asdfsdfsdfswe3525fsdsdfs」,它顯示給用戶
  4. 如果以後用戶點擊此鏈接 - 我們authentificate用戶,並顯示索引頁(因此,例如,用戶可以收藏這個鏈接,並用它來代替用戶名通)

我有一些限制:

  1. 鏈接必須是像 「mydomain.com/user/asdfsdfsdfswe3525fsdsdfs」。沒有GET參數。
  2. 用戶進行身份驗證和頁面加載後 - 用戶應該看到在這個瀏覽器登錄URL。所以我不能將用戶重定向到所需的頁面/腳本

我不明白如何通過這樣的鏈接使用php認證用戶。 如何避免404錯誤?

+0

查找國防部重寫 – 2013-06-27 22:33:36

+0

「如何避免404錯誤?」帶有RewriteRules的'.htaccess'文件。 – Sammitch

+0

限制#1,您將需要使用mod_rewrite將querystring參數轉換爲「漂亮」url – Matt

回答

0

國防部重寫我做的方式,這將使它簡單,但很難在同一時間。

的.htaccess檔中的_ __ _ __ _ __ _ __ _ ____

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase /main site folder/ 

    RewriteRule (.*)\.xml(.*) $1.php$2 [nocase] 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 

    RewriteRule ^(.*)$ index.php/$1 
</IfModule> 

/classes/simplifyURL.php FILE_ _ __ _ __ _ __ _ __ _ _

<?php 

class simpleUrl{ 

    var $site_path; 

    function __construct($site_path){ 
     $this->site_path = $this->removeSlash($site_path); 
    } 

    function __toString(){ 
     return $this->site_path; 
    } 

    private function removeSlash($string){ 
     if($string[strlen($string) - 1] == '/') 
     $string = rtrim($string, '/'); 

     return $string; 
    } 
    function segment($segment){ 

    $url = str_replace($this->site_path, '', $_SERVER['REQUEST_URI']); 
    $url = explode('/', $url); 
    if(isset($url[$segment])) 
     return $url[$segment]; 
    else 
     return false; 
    } 
} 

?> 

頂部的index.php _文件_ __ _ __ _ ___

<?php 
include 'classes/simplifyURL.php'; 

$url = new simpleUrl('/main site folder'); 
$url1 = $url->segment(1); 
$url2 = $url->segment(2); 
$url3 = $url->segment(3); 
$url4 = $url->segment(4); 

?> 

包括網頁到index.php中基於關閉URL_ _ __ _

  <?php 

$pages_dir = 'pages'; 



if (!empty($url1)) { 

    $pages = scandir($pages_dir, 0); 

    unset($pages[0], $pages[1]); 



    $p = $url1; 



    if (in_array($p.'.inc.php', $pages)) { 

    include($pages_dir.'/'.$p.'.inc.php'); 

    } else { 

    echo 'Sorry, page was not found.'; 

    } 

    } else { 

    include($pages_dir.'/home.inc.php'); 

} 

?> 

這是你必須make.Anyway一個巨大的轉變,讓我解釋一下上面的部分。它需要url的第一位的值,所以緊跟在域後面並檢查文件夾頁面,如果它能找到它,它會顯示它。所以對於你的情況,你需要創建一個名爲user.inc.php的文件。將身份驗證放入它中,並使用$ url2變量來訪問網址的結尾,以獲取您擁有的長代碼。其他明智的動態頁面。希望這有助於

這是很多的工作,我看不出什麼錯與GET

+0

添加所需的mod重寫,並且您可能會獲得upvote – 2013-06-27 22:49:59