使SESSION在重定向后工作


Getting SESSION to work after redirect

我是PHP新手,更不熟悉SESSIONS

我正在使用Instagram API,我成功地授权了一个应用程序,并重定向到一个页面来显示内容。

我的主文件夹叫Monkey,它有一个子文件夹叫Instagram。

我的instagram回调url是success.php位于instagram文件夹。当我成功地从Instagram检索访问令牌时,它会重定向到Monkey文件夹中的索引文件。

在我的成功页面上,我正在创建一个名为instaArray的充满数据的数组。我试图将数组从instagram文件夹中的success.php传递到monkey文件夹中的index.php。

我的重定向只是

header( 'Location: ../index.php' );

因为我是新手,我想我做错了什么。我认为这是直截了当的,但我想不是。

在success.php页面上,在我构建数组之后,我得到了这个

session_start();
$_SESSION['instagram'] = $instaArray;

我认为应该创建一个会话,持有我的数组InstaArray。

然后,在Monkey的index。php页面,我有这个

<?php
session_start();
$get_instagram = $_SESSION['instagram'];
print_r($get_instagram);
?>

但是什么都没有发生。我甚至尝试将会话instagram设置为一个简单的数值或1,$_SESSION['instagram'] = 1;然后把它放到索引页,它也不起作用

我是不是做了什么非常非常错误的事情?我已经研究过治疗方法了,但因为是新方法,还是有点让人困惑。

谢谢你的帮助,我希望我能正确地解释一切。

编辑:这是我的success.php页面的完整

<?php
require 'src/db.php';
require 'src/instagram.class.php';
require 'src/instagram.config.php';
// Receive OAuth code parameter
$code = $_GET['code'];
// Check whether the user has granted access
if (true === isset($code)) {
    // Receive OAuth token object
    $data = $instagram->getOAuthToken($code);
    // Take a look at the API response
    $username = $data->user->username;
    $fullname = $data->user->full_name;
    $id = $data->user->id;
    $token = $data->access_token;
    $user_id = mysql_query("select instagram_id from users where instagram_id='$id'");
    if(mysql_num_rows($user_id) == 0) { 
        mysql_query("insert into users(instagram_username,instagram_name,instagram_id,instagram_access_token) values('$username','$fullname','$id','$token')");
    }
    //Set Cookie
    $Month = 2592000 + time();
    setcookie(instagram, $id, $Month);
        // Set user access token
    $instagram->setAccessToken($token);
        // Retrive Data
    $instaData = $instagram->getUserFeed();
    // Create Instagram Array
    $instaArray = array();
    $count = 0;
    // For each Instagram Post
    foreach ($instaData->data as $post) {
        $instaArray[$count]['post_id'] = $post->id;
        $instaArray[$count]['name'] = $post->user->username;
        $instaArray[$count]['profile_img'] = $post->user->profile-picture;
        $instaArray[$count]['img_url'] = $post->images->standard_resolution->url;
        $instaArray[$count]['caption'] = $post->caption->text;
        $instaArray[$count]['like_count'] = $post->likes->count;
        $instaArray[$count]['comment_count'] = $post->comments->count;
        $instaArray[$count]['created_time'] = $post->created_time; //Unix Format
        $count++;
    }
    // Start Session For Array
    session_start();
    $_SESSION['instagram'] = serialize($instaArray);
    header( 'Location: ../index.php' ) ;
} else {
    // Check whether an error occurred
    if (true === isset($_GET['error']))  {
        echo 'An error occurred: '.$_GET['error_description'];
    }
}

?>

为什么不使用ID然后cookie而不是会话+数据(通常存储在服务器上的临时目录中的文本文件中)?并将所有数据保存在数据库中,而不是允许客户端访问这些数据。会话也是临时的。

注意,你知道你是否打开了"globals"吗?

"请注意,当使用会话时,会话记录不会被创建,直到使用session_register()函数或通过向$_SESSION超全局数组添加新键来注册变量。无论会话是否已经使用session_start()函数启动,都是如此。"

参考:


http://www.php.net/manual/en/function.session-register.php

让session_start()在PHP之后的第一行

 <?php
    session_start();

并将其从页面上的任何地方删除。

session_start()应该是index.php中的第一行,就像success.php

一样

注意:session_start()函数必须出现在标签

之前

REF: http://www.w3schools.com/php/php_sessions.asp

我认为你需要在index.php中unserialize()你的数组。

$get_instagram = unserialize($_SESSION['instagram']);