PHP Issues For Json Read Function

PHP Issues For Json Read Function

So I am generally trying to read from a Json file that has data in it related to "artists" that scans that specific file and reads the line that says

    "suspended": true
    "banned": true

and the profile should and is meant to actually hide the profile and determine if they are banned say they are banned and if suspended say if their suspended while at the same time hiding and restricting viewing their profile.

The issue is that the code itself is not functioning or working, I've tried one of many ways with multiple logics and it has still been showing their profile

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
session_start();
require 'functions.php';

$requestedUser = $_GET['user'] ?? '';
$artists = json_decode(file_get_contents('data/artists.json'), true);
$followersFile = 'data/followers.json';
if (!file_exists($followersFile)) {
    file_put_contents($followersFile, '{}');
}
$followers = json_decode(file_get_contents($followersFile), true);

$email = strtolower($_SESSION['email'] ?? '');
$isLoggedIn = !empty($email);
$isArtist = isset($artists[$email]);

// Find artist by username
$artistEmail = null;
foreach ($artists as $e => $info) {
    if (isset($info['username']) && $info['username'] === $requestedUser) {
        $artistEmail = $e;
        break;
    }
}
if (!$artistEmail || !isset($artists[$artistEmail])) {
    die("Artist not found.");
}
$artist = $artists[$artistEmail];

// Check if the artist is banned
if (isset($artist['banned']) && $artist['banned'] === 'true') {
    echo '<div class="alert alert-danger text-center">🚫 This artist has been banned. Their profile is restricted.</div>';
    exit;
}

$posts = get_posts();
$artistPosts = array_filter($posts, fn($p) => isset($p['author']) && $p['author'] === $artistEmail);

// Handle follow/unfollow request
if ($isLoggedIn && !$isArtist) {
    if (isset($_POST['follow'])) {
        if (!isset($followers[$artistEmail])) {
            $followers[$artistEmail] = [];
        }
        if (!in_array($email, $followers[$artistEmail])) {
            $followers[$artistEmail][] = $email;
            file_put_contents($followersFile, json_encode($followers, JSON_PRETTY_PRINT));
        }
        header("Location: artist.php?user=" . urlencode($requestedUser));
        exit();
    } elseif (isset($_POST['unfollow'])) {
        if (isset($followers[$artistEmail])) {
            $followers[$artistEmail] = array_filter(
                $followers[$artistEmail],
                fn($follower) => $follower !== $email
            );
            file_put_contents($followersFile, json_encode($followers, JSON_PRETTY_PRINT));
        }
        header("Location: artist.php?user=" . urlencode($requestedUser));
        exit();
    }
}

$isFollowing = isset($followers[$artistEmail]) && in_array($email, $followers[$artistEmail]);
$isSubscribed = $isLoggedIn && check_user_subscription($email);
?>

The code block below is the actual function of the website along with this

<?php if (!empty($artist['status']) && $artist['suspended'] === 'true'): ?>
    <div class="alert alert-warning mt-2">⚠️ This artist is currently suspended. Their activity may be limited.</div>
<?php endif; ?>

I know I am doing something specifically wrong since it seems to be there multiple times some help would be appreciated with this headache code.

Answer

let's look at the problem areas

if (isset($artist['banned']) && $artist['banned'] === 'true') {
    echo '<div class="alert alert-danger text-center">🚫 This artist has been banned. Their profile is restricted.</div>';
    exit;
}

In the above, you are comparing to 'true', which is a string and will not be identical to the boolean true that is contained in the JSON.

In the second case:

           <?php if (!empty($artist['status']) && $artist['suspended'] === 'true'): ?>
                <div class="alert alert-warning mt-2">⚠️ This artist is currently suspended. Their activity may be limited.</div>
            <?php endif; ?>

You not only have the same problem as with banned, but in addition you are checking if something called status is not empty, and not checking if suspended is set. That behavior may or may not be the logic you want.

Enjoyed this article?

Check out more content on our blog or follow us on social media.

Browse more articles