0

我似乎遇到了一个问题,即此页面上的 PHP 会话状态以一组定义的参数开始,但在重定向到 Twitter 以进行应用授权后,返回完全不同的会话 ID 和会话状态。当我清除浏览器的 cookie 时,此行为停止并且程序正常运行。但是在下一次 Twitter 授权尝试中,会话恢复到它在清除 cookie 后第一次设置的状态,但只有在从 Twitter 返回之后。

任何帮助,将不胜感激!

<?php

session_start();
echo session_id();

require 'oauth/twitteroauth.php';
require 'Abacus.php';

$twitter_consumer_key = 'OwxQxjghhyBOibNvg4Tg';
$twitter_consumer_secret = '3rBhdAyiLGeTBR6GY4i76vilFqqV2EL5cFQjmt8pJBg';

if (!isset($_SESSION['user'])) { echo "<script type='text/javascript'>window.location.href=(\"$index.php\")</script>"; die(); }

if (isset($_REQUEST['appauth']))
{
    $step = 0;
    if (isset($_REQUEST['step'])) {
        $step = $_REQUEST['step'];
    }

    switch ($_REQUEST['appauth'])
    {
        case 'twitter':
            if ($step == 1)
            {
                $twit_conn = new TwitterOAuth($twitter_consumer_key, $twitter_consumer_secret);
                $temp_cred = $twit_conn->getRequestToken();
                $_SESSION['twitter_temp_oauth_token'] = $temp_cred['oauth_token'];
                $_SESSION['twitter_temp_oauth_token_secret'] = $temp_cred['oauth_token_secret'];
                $redirect_url = $twit_conn->getAuthorizeURL($temp_cred);
                //header("Location: $redirect_url");
                echo "<script type='text/javascript'>window.location.href=(\"$redirect_url\")</script>";
                die();
            }
            else if ($step == 2)
            {
                $twit_conn = new TwitterOAuth($twitter_consumer_key, $twitter_consumer_secret, $_SESSION['twitter_temp_oauth_token'], $_SESSION['twitter_temp_oauth_token']);
                $token_cred = $twit_conn->getAccessToken();
                //$_SESSION['twitter_token_cred'] = $token_cred;

                $db_conn = mysql_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD);
                if (!$db_conn) { die('Could not connect to MySQL database.'); }
                mysql_select_db(DATABASE_NAME, $db_conn);

                mysql_query("DELETE FROM usercontentsources WHERE userid = {$_SESSION['user']['userid']} AND contentsourceid = 4", $db_conn);
                $insert_twitter_cred_sql = "INSERT INTO usercontentsources (userid, contentsourceid, params) VALUES ({$_SESSION['user']['userid']}, 4, '{$token_cred['oauth_token']} {$token_cred['oauth_token_secret']}')";
                mysql_query($insert_twitter_cred_sql, $db_conn);

                mysql_close($db_conn);

                $twit_conn = new TwitterOAuth($twitter_consumer_key, $twitter_consumer_secret, $token_cred['oauth_token'], $token_cred['oauth_token_secret']);
                var_dump($twit_conn->get('account/verify_credentials'));
            }
            break;
        case 'facebook':
            break;
        default:
            break;
    }
}

?>
<!DOCTYPE html>
<html>
    <head>
        <title>Settings - ProjectAbacus</title>
    </head>
    <body>
        <a href='settings.php?appauth=twitter&step=1'>Integrate Twitter</a>
    </body>
</html>
4

1 回答 1

-1

我刚刚遇到了一个类似的问题——事实证明(为了允许一次处理一个会话的多个 Ajax 请求)我们是session_write_close()在线程开始时调用的。

这意味着我们对会话值所做的任何更改都不会被存储,并且在页面重新加载时会话值会恢复到之前的状态。我们正在移动/删除session_write_close()修复它的调用。

于 2014-10-21T14:48:57.293 回答