douxiapi4381 2012-08-11 19:09
浏览 65
已采纳

使用PHP for Dummies将'username'添加到'user_id'的URL中

I am attempting to publish a question which I know has many answers floating around Stack Overflow. However I, for some reason, just cannot seem to get the information in them to click. I will post my sources at the end of the question.

The question being, how can I pull a user_id from a URL & mod_rewrite it show the username.

Instead of this:

domain/user/index.php?user_id=1

I get this:

domain/username

I had customized other SO answers to my needs on my .htaccess file to this:

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On
  RewriteOptions MaxRedirects=1
  RewriteCond %{REQUEST_FILENAME} -f [NC,OR] 
  RewriteCond %{REQUEST_FILENAME} -d [NC] 
  RewriteRule .* - [L]
  RewriteRule ^user/([^/]+)$ index.php?user_id=$1
</IfModule>

The if statement at the top of my user page looks like this:

if (isset($_GET['user_id']) || isset($_GET['username']) && queryUserId($user_id)) {
  // Render Page Content
} else {
  header('Location: sign_up.php');
}

And my queryUsername functions looks like this:

function queryUsername($username) {
    $conn = dbConnect('read');
    $sql = "SELECT * FROM users WHERE user_id = '".$username."'";
    $result = $conn->query($sql) or die(mysqli_error($conn));
    $row = $result->fetch_assoc();
    return $row['username'];
    $username = $row['username'];
}

I have successfully implemented unique URL's for users, and am accessing Account Profiles just fine when appending different user_id's to my URL, so what am I missing to get the username written to the URL instead of variable strings and folder structure?

Cheers!

Other SO Questions:

Directly adding username to URL PHP

Get username from URL in PHP

Using mod rewrite to change URL with username variable

AddedBytes Article

URL Rewriting for Beginners

  • 写回答

1条回答 默认 最新

  • duan0504 2012-08-11 19:16
    关注

    This line is wrong according to your information:

    RewriteRule ^user/([^/]+)$ index.php?user_id=$1
    

    It should at least be:

    RewriteRule ^user/([^/]+)$ /user/index.php?user_id=$1    // for a url like domain/user/username
    

    or using your description:

    RewriteRule ^([^/]+)$ /user/index.php?user_id=$1    // for a url like domain/username
    

    Also, you are not setting any variable with the name of username in the code you have shown, so the check for $_GET['username'] is unnecessary.

    Your check in php should look something like:

    if ( isset($_GET['user_id']) && queryUserId($_GET['user_id']) ) {
    

    Apart from that you should not use the deprecated mysql_* functions and use prepared statements as you have an sql injection problem now.

    Also note that using two return statements after each other only returns the first value.

    Edit: There seems to be some confusion between the user ID and the username. If the value in the url is a username, you'd better call it username in both the .htaccess file and php to avoid confusion with the user ID:

    RewriteRule ^([^/]+)$ /user/index.php?username=$1    // for a url like domain/username}
    

    and

    if ( isset($_GET['username']) && queryUserName($_GET['username']) ) {
    

    in the function (using the deprecated functions just to illustrate...):

    function queryUserName($username) {        
        $conn = dbConnect('read');
        $sql = "SELECT * FROM users WHERE username = '".$username."'";
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序