Steam接入网站登录时触发Trying to access array offset on value of type bool错误求助
Hey there, let's break down what's causing those notice errors and get your Steam login working again!
What's the Root Cause?
Those repeated notices are telling you exactly what's wrong: on lines 67-71 of your index.php, you're trying to access array keys (like steamid32, personaname) on a variable that's actually a boolean value (almost certainly false).
In your code, that variable is $steamprofile—which means the call SteamAPI::GetUserData($_SESSION['gex_steamid64']) is failing and returning false instead of the expected user data array. This happens for a few common reasons:
- The
$_SESSION['gex_steamid64']value stored in the session is invalid, empty, or not a valid 17-digit SteamID64. - The Steam API request is failing (network issues, invalid API key, rate limiting, or Steam's API being down temporarily).
- The
SteamAPI::GetUserDatamethod has a bug where it returnsfalseinstead of handling errors gracefully.
Step-by-Step Fix
Let's add safeguards to your code to handle this failure case, plus add debugging to pinpoint the exact issue:
Validate the Session SteamID64 first
Before calling the Steam API, make sure the session variable exists and is a valid SteamID64:elseif (isset($_GET['login_started'])) { // First, ensure we have a valid SteamID64 in the session if (empty($_SESSION['gex_steamid64']) || !is_string($_SESSION['gex_steamid64']) || !preg_match('/^\d{17}$/', $_SESSION['gex_steamid64'])) { // Redirect back to login with an error if the SteamID is invalid header('Location: /login?error=invalid_steam_id'); exit; } // Fetch Steam user data $steamprofile = SteamAPI::GetUserData($_SESSION['gex_steamid64']); // Check if the API call returned a valid array if (!is_array($steamprofile) || empty($steamprofile)) { // Log the failure for debugging (check your server's error logs later) error_log("Failed to fetch Steam user data for ID: " . $_SESSION['gex_steamid64']); // Redirect to login with an error message for the user header('Location: /login?error=steam_api_failure'); exit; } // Now safely access the array keys (no more notices!) $steamid32 = $steamprofile['steamid32']; $nick = $steamprofile['personaname']; $avatar_small = $steamprofile['avatar']; $avatar_medium = $steamprofile['avatarmedium']; $avatar_large = $steamprofile['avatarfull']; // ... rest of your code }Debug the SteamAPI::GetUserData Method
If the error persists, dig into theSteamAPIclass to see why it's returningfalse:- Check if the method handles HTTP errors (like 403 Forbidden for invalid API keys, or 429 Too Many Requests for rate limits).
- Ensure it's correctly parsing Steam's JSON response into an array (instead of returning
falseif the response is malformed). - Add logging inside the method to capture the raw API response, so you can see exactly what Steam is sending back.
Verify Session Handling
Double-check that$_SESSION['gex_steamid64']is being set correctly during the initial Steam login flow. If this variable is missing or incorrect, the API call will always fail.
Quick Debugging Tip
To see exactly what $steamprofile contains before the error hits, add a temporary log statement right after the API call:
$steamprofile = SteamAPI::GetUserData($_SESSION['gex_steamid64']); error_log(var_export($steamprofile, true)); // This will write the value to your server's error log
Check your server's error logs (usually in /var/log/apache2/ or /var/log/nginx/ depending on your web server) to see if it's false, an empty array, or something else entirely.
内容的提问来源于stack exchange,提问作者Thomas.

