Помогите найти ошибку в авторизации через вк
[code]
<?
require_once 'core/system.php';
$header = 'Авторизация через VK';
require_once 'core/head.php';
$client_id = '5345709';
$client_secret = '';
$redirect_uri = 'http://twgw.ru/vk.php';
$url = 'http://oauth.vk.com/authorize';
$params = array(
'client_id' => $client_id,
'redirect_uri' => $redirect_uri,
'response_type' => 'code'
);
echo $link = '<div class="player" ><a href="' . $url . '?' . urldecode(http_build_query($params)) . '">Авторизоваться с помощью VK</a></div>';
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/user.get' . '?' . urldecode(http_build_query($params))), true);
if (isset($userInfo['response'][0]['uid'])) {
$userInfo = $userInfo['response'][0];
$result = true;
}
}
if ($result) {
$lo = mysql_query("SELECT COUNT(`id`) FROM `user` WHERE `nick` = 'vk-".$userInfo['uid']."'"
;
if (mysql_result($lo, 0) > 0) {
$info=mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `nick` = 'vk-".$userInfo['uid']."' limit 1"
);
setcookie('nick', $info['nick'], time() + 84600 * 365, '/');
setcookie('pass', $info['pass'], time() + 84600 * 365, '/');
header('Location: /index.php');
$_SESSION['message'] = 'Добро пожаловать в игру!';
exit();
}else{
$pass=mt_rand (1000, 9999);
$pass='vk-'.$pass.'';
if (isset($userInfo['sex'])){if ($userInfo['sex']=='1'){$sex='2';}else{$sex='1';}}else{$sex='1';}
$log='vk-'.$userInfo['uid'].'';
$pass=md5(md5(md5($pass)));
mysql_query("INSERT INTO `user` SET `nick` = '$log', `pass` = '$pass', `gold` = '0', `pol` = '$sex', `timereg` = '".time()."', `mana` = '1500', `max_mana` = '1500', `health` = '80', `max_health` = '80', `sila` = '80', `lovk` = '80', `zashit` = '80', `imp` = 'start', `impname` = 'Имп', `impstats` = '50'"
;
setcookie('nick', $log, time() + 84600 * 365, '/');
setcookie('pass', $pass, time() + 84600 * 365, '/');
header('Location: /index.php');
$_SESSION['message'] = 'Добро пожаловать в игру!';
exit();
}
}
}
require_once 'core/foot.php';
?>
[/code]