0
我似乎遇到了一個問題,即該頁面上的PHP會話狀態始於一組定義的參數,但在重定向到Twitter以獲取應用程序授權後,會返回一個完全不同的會話ID和會話狀態。當我清除瀏覽器的Cookie時,此行爲將停止並且程序正常運行。但是,在下一次Twitter授權嘗試中,會話將在清除Cookie後恢復到第一次設置時的狀態,但只有在從Twitter返回之後。php會話恢復到以前的狀態
任何幫助,將不勝感激!
<?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>
請不要使用'mysql_ *'函數來編寫新代碼。他們不再維護,社區已經開始[棄用程序](http://goo.gl/KJveJ)。請參閱* [紅盒子](http://goo.gl/GPmFd)*?相反,您應該瞭解[準備好的語句](http://goo.gl/vn8zQ)並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli的)。如果你不能決定哪些,[這篇文章](http://goo.gl/3gqF9)會幫助你。如果你選擇PDO,[這裏是很好的教程](http://goo.gl/vFWnC)。 –
此外,這也不是一個答案:使用PDO,而不是MySQLi;)http://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the-pros-and-cons –