$ _SESSION变量未携带到其他页面

时间:2014-06-26 15:25:57

标签: php session

这是我的第一页。 $ _SESSION变量正常工作。在进行var转储时,它具有我需要的信息。

<?php
// Start a session.  This is necessary to hold on to  a few keys the callback script will also need
session_start();

// Include the TumblrOAuth library
require_once('tumblroauth/tumblroauth.php');

// Define the needed keys
$consumer_key = "";
$consumer_secret = "";

// The callback URL is the script that gets called after the user authenticates with tumblr
$callback_url = "http://tumblrstats.sytes.net/info/chooseBlog.php";

// Let's begin.  First we need a Request Token.  The request token is required to send the user
// to Tumblr's login page.

// Create a new instance of the TumblrOAuth library.  For this step, all we need to give the library is our
// Consumer Key and Consumer Secret
$tum_oauth = new TumblrOAuth($consumer_key, $consumer_secret);

// Ask Tumblr for a Request Token.  Specify the Callback URL here too (although this should be optional)
$request_token = $tum_oauth->getRequestToken($callback_url);

// Store the request token and Request Token Secret as out chooseBlog.php script will need this
$_SESSION['request_token'] = $token = $request_token['oauth_token'];
$_SESSION['request_token_secret'] = $request_token['oauth_token_secret'];

// Check the HTTP Code.  It should be a 200 (OK), if it's anything else then something didn't work.
switch ($tum_oauth->http_code) {
  case 200:
    // Ask Tumblr to give us a special address to their login page
    $url = $tum_oauth->getAuthorizeURL($token);

    // Redirect the user to the login URL given to us by Tumblr
    header('Location: ' . $url);


    break;
default:
    // Give an error message
    echo 'Could not connect to Tumblr. Refresh the page or try again later.';
}
exit();
?>

这第二页就是它破碎的地方。 $ _SESSION变量上的var dump没有提供任何内容,因此在尝试访问它时遇到未定义的索引错误。

<?php
//Load library
session_start();
require_once('auth/tumblroauth/tumblroauth.php');
require_once('../tumblr/lib/Tumblr/API/Client.php');
include('functions.php');
include('blogClass.php');

// Define the needed keys
$consumerKey = "";
$consumerSecret = "";

// Create instance of TumblrOAuth. Give consumer key and secret.
$tumblrOAuth = new TumblrOAuth($consumerKey, $consumerSecret, $_SESSION['request_token'], $_SESSION['request_token_secret']);

//Retrieve an Access Token.
$accessToken = $tumblrOAuth->getAccessToken($_REQUEST['oauth_verifier']);

// Remove Request Token and Secret
unset($_SESSION['request_token']);
unset($_SESSION['request_token_secret']);

// Make sure nothing went wrong.
if (200 == $tumblrOAuth->http_code)
{
  //Everything is good
}
else
{
    die('Unable to authenticate');
}

//Present our variable more cleanly
$token       = $accessToken['oauth_token'];
$tokenSecret = $accessToken['oauth_token_secret'];

//Create a new instance of our client
$tumblrClient = new \Tumblr\API\Client($consumerKey, $consumerSecret);
$tumblrClient->setToken($token, $tokenSecret);

//Convert stdObject to Array
$userInfo = objectToArray($tumblrClient->getUserInfo());

//User Data
$userName = $userInfo['user']['name'];
$userLikeNum = $userInfo['user']['likes'];
$userFollowingNum = $userInfo['user']['following'];
$userDefPostFormat = $userInfo['user']['default_post_format'];

//General Individual Blog List
$blogNameList = array();
$blogNameID = array();
$blogID = 0;
foreach ($tumblrClient->getUserInfo()->user->blogs as $blog)
{
    $blogNameString = $blog->name;
    //Without ID
    $blogNameList[] = $blogNameString;
    //WithID
    $blogNameID[] = $blogNameString;
    $blogNameID[$blogNameString] = $blogID;
    //Add 1 to Blog ID
    $blogID++;
}
?>

<html lang="en-US">
<head>
    <title>Your Tumblr Stats</title>
    <link rel="stylesheet" type="text/css" href="../css/style.css">
</head>
<body>
<div id="chooseBlog">
    <?php
        foreach($blogNameList as $value)
        {
            $imgUrl = objectToArray($tumblrClient->getBlogAvatar($value, 128));
            echo "<div id='$value' class='blogSelector'><img src=$imgUrl><div id='innerContainer' class='blogInfo'><a href='http://www.$value.tumblr.com' id='info'>$value.tumblr.com</a></div></div>";
        }
    ?>
</div>
</body>
</html>

编辑:这是抛出的错误。

注意:未定义的索引:第14行/home/www/tumblr/htdocs/info/chooseBlog.php中的request_token注意:未定义的索引:/home/www/tumblr/htdocs/info/chooseBlog.php中的request_token_secret在线14

1 个答案:

答案 0 :(得分:1)

第一页(您的第一个文件)和最后一个文件(第二个文件)之间的所有页面都必须session_start()否则您将丢失PHP会话。

第一页是否访问第二页?