You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP预订系统日历数据权限控制:根据登录用户替换事件标题需求实现

Solution to Restrict Calendar Event Visibility by User

Hey Liam, great job figuring out the core approach already—you're right that we need to loop through the $data array and check against the logged-in user's session value. Let's turn that idea into working code, step by step.

First, Critical Setup: Start the Session

Since you're using $_SESSION["username"], you must start the session at the very top of your script (before any output, including whitespace). This is easy to forget as a new PHP developer, so don't skip this!

Modified Script with Logic

Here's the updated code that implements your requirements:

<?php
// Start the session to access the logged-in username
session_start();

// Define your database connection
$connect = new PDO('mysql:host=localhost;dbname=xxx', 'xxx', 'xxx');
$data = array();

// Define which titles are public (keep these visible to everyone)
$publicTitles = ['unavailable', 'holiday'];

// Fetch all events from the database
$query = "SELECT * FROM events ORDER BY id";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();

// Get the logged-in username (fallback to empty string if not set)
$currentUser = $_SESSION["username"] ?? '';

foreach($result as $row) {
    // Determine the correct title to display
    $eventTitle = $row["title"];
    
    // Check if the title is a public entry: keep it as-is
    if (in_array($eventTitle, $publicTitles)) {
        $displayTitle = $eventTitle;
    } 
    // Check if the title matches the current user: keep their username
    elseif ($eventTitle === $currentUser) {
        $displayTitle = $eventTitle;
    } 
    // All other cases: replace with 'booked'
    else {
        $displayTitle = 'booked';
    }

    // Add the modified event to the data array
    $data[] = array(
        'id' => $row["id"],
        'title' => $displayTitle,
        'start' => $row["start_event"],
        'end' => $row["end_event"]
    );
}

echo json_encode($data); 
?>

Key Logic Breakdown

Let's walk through what's happening here:

  1. Session Start: session_start() initializes the session so we can access $_SESSION["username"] set by your login script.
  2. Public Titles Array: We define $publicTitles to list entries that should be visible to all users—you can add more to this array later if needed.
  3. Current User Check: $currentUser = $_SESSION["username"] ?? '' safely gets the logged-in username (the ?? '' prevents errors if the session isn't set, though you should ensure your login script handles this properly).
  4. Title Decision: For each event, we:
    • Keep public titles exactly as they are
    • Keep the current user's own username visible
    • Replace all other user names with 'booked'

Testing with Your Example

If Mary is logged in ($_SESSION["username"] = 'Mary'), the output will match exactly what you requested:

[
{"id":"365","title":"booked","start":"2021-08-19 20:00:00","end":"2021-08-19 20:40:00"},
{"id":"366","title":"booked","start":"2021-08-19 19:00:00","end":"2021-08-19 19:40:00"},
{"id":"367","title":"Mary","start":"2021-08-20 10:00:00","end":"2021-08-20 10:40:00"},
{"id":"368","title":"Mary","start":"2021-08-20 12:00:00","end":"2021-08-20 12:40:00"},
{"id":"369","title":"booked","start":"2021-08-20 15:00:00","end":"2021-08-20 15:40:00"}
]

Quick Notes for New PHP Developers

  • Make sure your login script correctly sets $_SESSION["username"] when a user logs in.
  • session_start() must be the first line of code (no HTML, whitespace, or echoes before it—otherwise you'll get a "headers already sent" error).
  • The in_array() function checks if a value exists in an array—perfect for our public titles list.

内容的提问来源于stack exchange,提问作者Liam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 06:09:08