2013-03-22 83 views
2

后,我是新来的PHP,甚至新的节数获取会话中工作重定向

我与Instagram的API的工作,我能够成功授权的应用程序,并重定向到一个页面中显示的内容。

我的主文件夹被称为猴子,它有一个名为Instagram的子文件夹。

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

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

我重定向仅仅是

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

因为我有新的会话,我想我做错了什么。我觉得这很简单,但我想不是哈哈。

在success.php页面后,我建立数组我有这个

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

我认为应该创建一个对我的阵列InstaArray会话。

然后,在猴子的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']; 
    } 
} 

>

+0

你是否在任何屏幕输出之前设置会话? – Alvaro 2013-03-22 15:57:21

+0

我相信如此。 success.php页面只是收集和创建数组,而index.php页面将显示所有内容。我在索引页面上没有任何内容预计这个会话 – Ryan 2013-03-22 15:59:02

+0

错误?检查返回值?例如。 'session_start' - >“如果会话成功启动,则此函数返回TRUE,否则返回FALSE。” – ficuscr 2013-03-22 15:59:19

回答

0

为什么不使用一个ID,然后饼干,而不是会议+数据(这通常是在商店?服务器在临时目录中的文本文件中)?并保留数据库中的所有数据,而不允许客户端访问数据。会议也是暂时的。

请注意,你知道你是否有“全局变量”?!

“请注意,使用一个会话的记录不会被创建,直到一个变量已经使用了session_register()函数注册会议或通过添加一个新的关键$ _SESSION超全局数组中工作时,这无论成立如果会话已使用session_start()函数启动。“

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

+0

我可以吗?数组instArray将会有大量的数据。 – Ryan 2013-03-22 16:05:30

+0

@Ryan,一旦他们登录或通过授权过程,我就会将一个ID存储到cookie中(数字或电子邮件)。将该ID指向包含所有信息的数据库。 另外,如果需要,您可以稍后创建一个CMS来控制此类数据/内容。 – tfont 2013-03-22 16:07:56

+0

bleh,我想到了这个......哈哈,为测试网站做了很多工作。我可能会这样做。 – Ryan 2013-03-22 16:09:24

0

化妆的session_start()PHP

<?php 
    session_start(); 

后第一行,并从任何地方ELE删除页面上

在session_start()应该是你的第一线在index.php中也如success.php

注意:session_start()函数必须ap梨的标签之前:

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

+0

我已经将session_start()移动到每个页面的开头,在打开php之后,仍然没有运气。 – Ryan 2013-03-22 16:04:39

+0

@Ryan是否在success.php中使用了print_r($ instaArray)..?你得到填充数组? – alwaysLearn 2013-03-22 16:07:51

+0

尝试在index.php上使用print_r($ _ SESSION)来检查你的会话是否工作..? – alwaysLearn 2013-03-22 16:11:01

0

我想你需要反序列化()在你的index.php阵列。

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