【问题标题】:Show specific content based on current user session PHP根据当前用户会话 PHP 显示特定内容
【发布时间】:2018-01-30 17:53:25
【问题描述】:

我的问题是如何根据用户会话 php 显示特定内容。

我有一个名为 profile.php 的文件。当一个用户点击另一个用户时,第一个用户被重定向到 profile.php 文件。在这个文件中,我希望用户能够看到用户发布的所有帖子。

图片说明:

类似这样的:

<?php 

if ($_SESSION['username'] == ($_GET[‘id’])) { 

//DISPLAY rows with info from Database just like the attached code.

//DISPLAY edit button ONLY if the current user session is the same as the current id of the profile page.
} 

?>

profile.php 代码如下:

<?php 

session_start();
require('connect.php');
if (@$_SESSION["username"]) {

 ?>

 <!DOCTYPE html>
 <html>
 <head>
    <title>Profile page</title>
 </head>
 <body>
<?php include('header.php'); ?>

<center>
<?php echo '<table border="1px;">'; ?>
    <tr>
        <td>
            <span>ID</span>
        </td>

        <td width="400px" style="text-align: center;">
            Name
        </td>

        <td width="80px" style="text-align: center;">
            Creator
        </td>

        <td width="80px" style="text-align: center;">
            Date
        </td>
        <td width="80px" style="text-align: center;">
            Edit
        </td>

    </tr>

</center>

</body>
</html>

<?php



if (@$_GET['id']) {
    $check_d = mysql_query("SELECT * FROM users WHERE id ='".$_GET['id']."'");

    while ($row_d = mysql_fetch_assoc($check_d)) {

        echo "<h1>Post made by: ".$row_d['username']."</h1>";

        $check_u = mysql_query("SELECT * FROM topics WHERE topic_creator='".$row_d['username']."'");
                while ($row_u = mysql_fetch_assoc($check_u)) {
                    $id = $row_u['topic_id'];
                    echo "<tr>";
                    echo "<td>".$row_u['topic_id']."</td>";
                    echo "<td><a href='topic.php?id=$id'>".$row_u['topic_name']."<br /></a></td>";
                    echo "<td>".$row_u['topic_creator']."<br /></td>";
                    echo "<td>".$row_u['date']."<br /></td>";


                    echo "<td><a href='edit.php?edit=$id'>Edit</a><br /></td>";


                    echo "</tr>";
                }
    }
}



 echo "</table>";

if (@$_GET['action'] == "logout")   {
    session_destroy();
    header("Location: login.php");
}

}else {
    echo "You must be logged in.";
}

  ?>

如果有人知道如何解决这个问题,我将不胜感激!

我可以在网上找到的大多数答案都涉及用户级别分布,其中管理员和用户级别是预先确定的。这不是我想要的。我只是希望当前登录的用户能够编辑他们自己的帖子,而不是其他用户的帖子。

我希望这是有道理的,但如果没有,那就问吧!

先谢谢了! // E.

【问题讨论】:

  • 注意:mysql_ 函数已弃用。使用mysqli 或 PDO。不要将用户的值连接到 SQL 字符串中,使用参数。否则你的系统很容易受到注入攻击。
  • 首先,将if ($_SESSION['username'] == ($_GET[‘id’])) { 中的($_GET[‘id’])) { 改成($_GET['id'])) {
  • 为什么要在打印数据的逻辑之前关闭 body 和 html 标签。
  • 你必须写 echo '

标签: php mysql sql session


【解决方案1】:

如果登录的用户不应该编辑其他用户的帖子,那么不要显示编辑栏,那么你可以做简单的检查列编辑如下

    while ($row_d = mysql_fetch_assoc($check_d)) {
        echo "<h1>Post made by: ".$row_d['username']."</h1>";
        $check_u = mysql_query("SELECT * FROM topics WHERE topic_creator='".$row_d['username']."'");
        while ($row_u = mysql_fetch_assoc($check_u)) {
            $id = $row_u['topic_id'];
            echo "<tr>";
            echo "<td>".$row_u['topic_id']."</td>";
            echo "<td><a href='topic.php?id=$id'>".$row_u['topic_name']."<br /></a></td>";
            echo "<td>".$row_u['topic_creator']."<br /></td>";
            echo "<td>".$row_u['date']."<br /></td>";
// Add if condition here
            if($_SESSION['current_logged_in_user_id'] === $row_u['topic_creator_id']) {
                echo "<td><a href='edit.php?edit=$id'>Edit</a><br /></td>";
            }
            echo "</tr>";
        }
    }

但不要使用 mysql_* 函数。出于安全原因使用 mysqli 或 PDO,例如保护自己免受 sql 注入攻击。

【讨论】:

  • 谢谢先生!!哦,我不知道mysql和mysqli之间的区别。我肯定会调查的!欢呼
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多