Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

devkon98's avatar

How to log out session from discord API PHP

Hello i have this code that login with discord and it works but i also need to make the log out. I tried to make with session destroy but doesnt work correct. This is my code:

<?php
   if (!isset($_SESSION)) {
   session_start();
   }
   $context = stream_context_create(array(
       'http' => array('ignore_errors' => true),
       ));
   
   // print the array for testing
   
   	//var_dump($data);
   	
   	$idValues = file_get_contents('https://bht.bet/api/dzWJjyef5PIbA1nMjQPW2KTTy1gfqUiw/hunts/#ID', false, $context);
   		$hunts = json_decode($idValues);
   		$z = 0;	
   		
   if(isset($_POST['discord'])) {
   		$clientId = '1090332606035394670';
$clientSecret = 'ncY-AgLNNGvkfB73eivKAiKdY9wWPoAj';

// Set the redirect URI for after authorization
$redirectUri = 'http://localhost:81/api/discord.php';

// Start the session
session_start();

// Check if an access token is already set
if (isset($_SESSION['access_token'])) {
    // User is already logged in, so redirect them to your application's home page
	// 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'];

	$username = $_SESSION['username']; 

    header('Location: bonushunt.php');
    exit;
}

// Check if the user has authorized your app
if (isset($_GET['code'])) {
    // Exchange the authorization code for an access token
    $tokenUrl = 'https://discordapp.com/api/oauth2/token';

    $postData = array(
        'grant_type' => 'authorization_code',
        'client_id' => $clientId,
        'client_secret' => $clientSecret,
        'redirect_uri' => $redirectUri,
        'code' => $_GET['code']
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $tokenUrl);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);

    $response = json_decode($response, true);

    // Store the access token in the session
    $_SESSION['access_token'] = $response['access_token'];
	
	$_SESSION['username'] = $response['email'];
	
	echo $response['email'];
	//print_r($response);
	die();

    // Redirect the user to your application's home page
    header('Location: http://localhost:81/api/bonushunt.php');
    exit;
}

// User is not logged in and has not authorized your app, so redirect them to the Discord authorization page
$authorizationUrl = 'https://discordapp.com/api/oauth2/authorize?' . http_build_query(array(
    'client_id' => $clientId,
    'redirect_uri' => $redirectUri,
    'response_type' => 'code',
    'scope' => 'identify email'
));

header('Location: ' . $authorizationUrl);
exit;	
   }
   		
   ?>
0 likes
1 reply
LaryAI's avatar
Level 58

To log out a user from Discord API in PHP, you need to unset the session variables that store the access token and username. You can do this by calling the session_unset() function. Here's an example of how you can modify the code to log out the user:

<?php
session_start();

if (isset($_SESSION['access_token'])) {
    // User is logged in, so unset the session variables
    session_unset();
    session_destroy();
}

// Redirect the user to the login page
header('Location: http://localhost:81/api/login.php');
exit;
?>

In this example, we check if the access_token session variable is set. If it is, we unset all session variables and destroy the session. Then we redirect the user to the login page. Note that you need to replace http://localhost:81/api/login.php with the URL of your login page.

Please or to participate in this conversation.