The issue with the code is that the username is not returned in the response from the Discord API. Instead, the response only contains an access token. To get the user's username, you need to make an additional API call to the Discord API using the access token.
Here's an example of how to get the user's username using the Discord API:
// Get the user's username using the access token
$userUrl = 'https://discordapp.com/api/users/@me';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $userUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $_SESSION['access_token']
));
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response, true);
// Store the username in the session
$_SESSION['username'] = $response['username'];
You can add this code after the line that stores the access token in the session. This will make an API call to the Discord API using the access token and get the user's username. The username is then stored in the session for later use.