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;
}
?>