0

索引.php

<?php
session_start();
require 'components/database.php';
require 'components/user.php';
$_SESSION['viewer_id'] = $_GET['viewer_id'];
echo $_SESSION['viewer_id'];
?>
<!DOCTYPE html>
<html lang="ru">
<head>
    <title>Entropia Universe - Приложение</title>
    <meta charset="utf-8">
    <link href="template/default/css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="template/default/css/style.css" rel="stylesheet" media="screen">
    <script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
    <script src="http://vk.com/js/api/xd_connection.js" type="text/javascript"></script> <!--Подключаем API-->
    <script src="template/default/js/bootstrap.min.js"></script>
<script type="text/javascript" charset="utf8" >
$(document).ready(function() { /*Когда загрузится вся страница*/
 VK.init({apiId:3822299},function() { 
  console.log("init");

  }, function() { 
     // API initialization failed 
     // Can reload page here 
}, '5.0');
var viewer_id = 0;
var auth_key = 0;
var image ='';

/*Записываем в переменные id посетителя и ключ*/
viewer_id = '<?=$_SESSION['viewer_id']?>';
auth_key = '<?=$_SESSION['auth_key']?>';
VK.api("users.get", {uids:viewer_id,fields:"photo_big"}, function(data) {
$('#user-avatar').html("<img src='" + data.response[0].photo_big + "' class='img-polaroid'>");
//$("#info").html("<img src='" + data.response[0].photo_big + "'>" + data.response[0].first_name + ' ' + data.response[0].last_name + ' ' + viewer_id); /*в div с id=info записываем аватарку, имя, фамилию*/
}); 
});
</script>
</head>
<body>
<?php
    $db = new Database('144.76.6.45','5432','eu','eu','eu123');
    $db->querySelect("SELECT * FROM users WHERE vk_id = ".$_SESSION['viewer_id']."");
    $row = $db->STH->fetch();
    if(empty($row)){
        require 'template/default/not_logged.php';
    }
    else {
        require 'template/default/logged.php';
    }
?>
</body>
</html>

从 API 我通过 GET 方法获取变量。请不要问什么API,没必要写。好的.. 使用 get['viewer_id'] 值创建一个 session['viewer_id']。一切正常,代码有效,会话显示正常。当我转到其他页面时,会话仍然可见,但是当我返回 index.php 时会话 ['viewer_id'] 消失,它显示登录表单,但它不应该。

谁能告诉我有什么问题?

4

1 回答 1

0

将您的代码更改为:

if(isset($_GET['viewer_id'])
    $_SESSION['viewer_id'] = $_GET['viewer_id'];
echo $_SESSION['viewer_id'];

如果$_GET['viewer_id']未设置,您不会在会话中覆盖您的值。

于 2013-09-01T20:27:51.403 回答