php Session恢复到以前的状态

时间:2012-11-27 02:40:50

标签: php session

我似乎遇到一个问题,其中此页面上的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>

1 个答案:

答案 0 :(得分:-1)

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

这意味着我们对会话值所做的任何更改都没有存储,并且会话值在页面重新加载时恢复到之前的状态。我们正在移动/删除session_write_close()来解决此问题。