使用URL中的用户ID进行导航

时间:2017-06-14 12:14:58

标签: php url userid

所以我正在努力学习更多PHP。

我相信我已成功登录时设置$ _SESSION ['user'],然后我想使用标题将用户定向到profile.php页面。我想要做的是使用URL中的用户ID或用户名查看其他用户个人资料。

例如: /profile.php?id=7 /profile.php/John

if(!isset($_SESSION['user'])){
    header("Location:profile.php?id=" . $_SESSION['user']);

}else if(!empty($_GET['name'])) {

    $username = $_GET['name'];
    $getUserProfile = mysqli_query($connection, "SELECT user.username, user.userID
                                     FROM profile, user
                                     WHERE profile.userID = user.userID
                                     AND user.username = '{$username}'");
    while($getResult = mysqli_fetch_array($getUserProfile)){
        $usersname = $getResult["username"];
        $usersId = $getResult["userID"];

    }
}

我试过这样做,但我无法让它发挥作用。我非常感谢有关如何做到这一点的任何帮助!

1 个答案:

答案 0 :(得分:2)

试试这个:

if(!empty($_SESSION['user']) && empty($_GET['name'])){
    header("Location:profile.php?id=" . $_SESSION['user']);

}else if(!empty($_GET['name'])) {

    $username = $_GET['name'];
    $getUserProfile = mysqli_query($connection, "SELECT user.username, user.userID
                                     FROM profile, user
                                     WHERE profile.userID = user.userID
                                     AND user.username = '{$username}'");
    while($getResult = mysqli_fetch_array($getUserProfile)){
        $usersname = $getResult["username"];
        $usersId = $getResult["userID"];

    }
    header("Location:profile.php?id=" . $usersId);
}