What are some common methods for tracking which posts a user has viewed in a PHP forum?

One common method for tracking which posts a user has viewed in a PHP forum is to store the post IDs in a database table along with the user ID. When a user views a post, the post ID and user ID can be inserted into this table. This allows the forum to query the table to retrieve the posts that a specific user has viewed.

// Assuming you have a database connection established

// Function to insert viewed post into database
function insertViewedPost($userId, $postId) {
    $query = "INSERT INTO viewed_posts (user_id, post_id) VALUES ($userId, $postId)";
    mysqli_query($connection, $query);
}

// Function to retrieve viewed posts for a specific user
function getViewedPosts($userId) {
    $query = "SELECT post_id FROM viewed_posts WHERE user_id = $userId";
    $result = mysqli_query($connection, $query);
    $viewedPosts = [];
    while($row = mysqli_fetch_assoc($result)) {
        $viewedPosts[] = $row['post_id'];
    }
    return $viewedPosts;
}