2013-08-02 23 views
1

我創建其中每個用戶都擁有自己的個人資料(個性化網址)PHP的社會工程,如:子文件夾到PHP個性化網址

www.mysite.com/myname 

,我用這個代碼:

1.profile。 PHP

<?php 
ob_start(); 
require("connect.php"); 
if(isset($_GET['u'])){ 
    $username = mysql_real_escape_string($_GET['u']); 
    if(ctype_alnum($username)){ 
     $data = mysql_query("SELECT * FROM members WHERE username = '$username'"); 
     if(mysql_num_rows($data) === 1){ 
      $row = mysql_fetch_assoc($data); 
      $info = $row['info']; 
      echo $username."<br>"; 
     }else{ 
      echo "$username is not Found !"; 
     } 
    }else{ 
     echo "An Error Has Occured !"; 
    } 
}else{ 
    header("Location: index.php"); 
}?> 
  1. 的.htaccess:

    個選項+了FollowSymLinks

    RewriteEngine敘述上

    的RewriteCond%{REQUEST_FILENAME} .PHP -f

    重寫規則^([^。] +)$ $ 1.PHP [NC]

    的RewriteCond%{ REQUEST_FILENAME}> 「」

    重寫規則^([^。] +)$ profile.php?U = $ 1 [L]

和此代碼的作品,如果我鍵入www.mysite.com/username它顯示用戶的配置文件。

現在我要求創建一個子文件夾到虛榮URL ..我的意思是如果我輸入www.mysite.com/username/info 它回聲存儲在數據庫中的用戶名信息..任何想法?

+1

爲什麼使用depreciated mysql_query()函數?使用MySQLi或PDO。 – Kenzo

回答

0

我強烈建議重寫一切,一個腳本調用前端控制器:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule^front_controller.php [L] 

然後你就可以處理URL中front_controller.php,找出加載的頁面。例如:

<?php 

// If a page exists with a `.php` extension use that 
if(file_exists(__DIR__ . $_SERVER['REQUEST_URI'] . '.php'){ 
    require __DIR__ . $_SERVER['REQUEST_URI'] . '.php'; 
    exit; 
} 

$uri_parts = explode('/', $_SERVER['REQUEST_URI']); 
$num_uri_parts = count($uri_parts); 

// For compatability with how you do things now 
// You can change this later if you change profile.php accordingly 
$_GET['u'] = $uri_parts[0]; 

if($num_uri_parts) == 1){ 
    require __DIR__ . 'profile.php'; 
    exit; 
} 

if($num_uri_parts) == 2){ 

    if($uri_parts[1] === 'info'){ 
     require __DIR__ . 'info.php'; 
     exit; 
    } 

    // You can add more rules here to add pages 
} 
+0

it don' t顯示任何東西!既不配置文件也不是信息! – ArsanGamal

+0

我解決了一些問題,但「此網頁有一個重定向循環」出現並將我重定向到index.php ..任何想法? – ArsanGamal

+0

我不確定。這將是由於這一行:'RewriteCond%{REQUEST_FILENAME}!-f'在你的.htaccess中無法正常工作。確保你的.htaccess和'index.php'在同一個文件夾中。 – Paulpro

1

添加

RewriteRule ^([^.]+)/info url/to/info/page/info.php?u=$1 [NC, L] #L = last [don't match any other rewrites if this matches] 

之前

RewriteRule ^([^.]+)$ $1.php [NC] 

之所以要添加,然後是第二個,同時匹配的用戶名/信息,但重定向到的個人資料頁面。

+0

它不起作用! – ArsanGamal

+0

這是什麼問題? – JohnnyFaldo

+0

500內部服務器錯誤 – ArsanGamal

相關問題