Answer the question
In order to leave comments, you need to log in
The session is lost. What to do?
Here is the code:
<?php
session_start();
$client_id = '51016'; // ID приложения
$client_secret = 'wTf757oEM4'; // Защищённый ключ
$redirect_uri = 'http://'; // Адрес сайта
$url = 'http://oauth.vk.com/authorize';
$params = array(
'client_id' => $client_id,
'redirect_uri' => $redirect_uri,
'response_type' => 'code'
);
echo $link = '<p><a href="' . $url . '?' . urldecode(http_build_query($params)) . '">Аутентификация через ВКонтакте</a></p>';
if (isset($_GET['code'])) {
$result = false;
$params = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'code' => $_GET['code'],
'redirect_uri' => $redirect_uri
);
$token = json_decode(file_get_contents('https://oauth.vk.com/access_token' . '?' . urldecode(http_build_query($params))), true);
if (isset($token['access_token'])) {
$params = array(
'uids' => $token['user_id'],
'fields' => 'uid,first_name,last_name,screen_name,sex,bdate,photo_big',
'access_token' => $token['access_token']
);
$userInfo = json_decode(file_get_contents('https://api.vk.com/method/users.get' . '?' . urldecode(http_build_query($params))), true);
if (isset($userInfo['response'][0]['uid'])) {
$userInfo = $userInfo['response'][0];
$result = true;
}
}
if ($result) {
echo 'Всё ок';
$_SESSION['user'] = $userInfo;
}
}
?>
Answer the question
In order to leave comments, you need to log in
put the entire token receiving block into a condition:
if ($_SESSION['user']=='') {
//получаем токен
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question