在同一页面上重定向后,会话变量不会保存

时间:2013-09-01 20:18:18

标签: php session get

的index.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']值创建一个会话['viewer_id']。一切正常,代码正常,会话显示正常。当我转到其他页面时,会话仍然可见,但是当我返回到index.php session ['viewer_id'] dissapears时,它会显示登录表单,但它显示出来。

有人可以告诉我这个问题吗?

1 个答案:

答案 0 :(得分:0)

将您的代码更改为:

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

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