如何为twitter web app登录设置cookie?

时间:2013-12-09 06:47:28

标签: php authentication cookies twitter

此代码用于为Web应用程序设置用于Twitter身份验证的cookie。

<?php
if(!isset($_COOKIE['test'])){
require_once ('codebird.php');
Codebird::setConsumerKey('<key>', '<key>'); 
$cb = Codebird::getInstance();
session_start();

if (!isset($_GET['oauth_verifier'])) {
    // gets a request token
    $reply = $cb->oauth_requestToken(array(
        'oauth_callback' => 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
    ));

    // stores it
    $cb->setToken($reply->oauth_token, $reply->oauth_token_secret);
    $_SESSION['oauth_token'] = $reply->oauth_token;
    $_SESSION['oauth_token_secret'] = $reply->oauth_token_secret;

    // gets the authorize screen URL
    $auth_url = $cb->oauth_authorize();
    header('Location: ' . $auth_url);
    die();

} else {
    // gets the access token
    $cb->setToken($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
    $reply = $cb->oauth_accessToken(array(
        'oauth_verifier' => $_GET['oauth_verifier']
    ));

    // store the authenticated token, which may be different from the request token (!)
    $token = $reply->oauth_token;
    $secret = $reply->oauth_token_secret;
    $screen_name1 = $reply->screen_name;

   // now store these three variables in the db
setcookie("test", $screen_name1, time()+3600*30);

$hostname = 'localhost';
$database = 'twitter;
$username = 'username';
$password = '****';
$conn = mysql_connect($hostname,$username,$password);
if(!$conn){
die("Unable to Connect localhost!".mysql_error());
}
mysql_select_db($database) or die("Unable to select database!".mysql_error());
$query = 'INSERT INTO cookie(cookie_name,screen_name,time) VALUES ("test","'.$screen_name1.'",'.strval(time()).')';
if(!mysql_query($query,$conn))
{
    die('Error: '.mysql_error());   
}

     header("Location: http://www.example.com/user.php?screen_name=".$screen_name1."");
die();
}
}

问题出在die()之后; 当用户登录Web应用程序并退出时。一段时间后,如果他再次打开网页,它应该将他引导到他在Web应用程序上的页面,而无需再次登录。 cookie解决了这个问题。但由于die()函数,它无法从上面的代码部分获取$ screen_name1到下面的部分,导致在用户再次登录时显示空白页面。

else{ 


session_start();
$hostname = 'localhost';
$database = 'twitter';
$username = 'username';
$password = '****';

$conn = mysql_connect($hostname,$username,$password);
if(!$conn){
die("Unable to Connect localhost!".mysql_error());
}
mysql_select_db($database) or die("Unable to select database!".mysql_error());

$value = mysql_query("SELECT screen_name FROM cookie WHERE screen_name='".$screen_name1."'");
$row = mysql_fetch_array($value);

  $screen_name = $row['screen_name'];
 ?>
<html>
<head>
<title><?php echo $screen_name?></title>
<link rel="shortcut icon" href="images/biostories.ico" />
</head>
                  <body class="wrapper">

                  <div class="header">
                        <img src="images/header.jpg" usemap = #example border=0> 
                        <map name=example>
                        <area shape=Rect Coords= 1032,25,1177,90 Href="http://hansacequity.com/"> 
                        </map>
                  </div>

                  <div class="intro">
                    <p>@<?php echo($screen_name)?>  </p>

                    </div>

<?php
mysql_select_db($database) or die("Unable to select database!".mysql_error());
$result = mysql_query('SELECT * FROM bio_archives WHERE screen_name="'.$screen_name.'" ORDER BY timestamp DESC');
while($row = mysql_fetch_array($result))
  { 
    ?>
    <div class="biostories">
    <div class="image">
                   <img src ="images/line.png";  />
                  </div>
    <div class="color">
    <?php echo $row['bio'];?>
    </div>

    <div class="time">
    <?php echo ("Last updated on "."&nbsp;&nbsp;".date('d/m/Y H:i:s',$row['timestamp']));?>
    </div>
                  </div>
                  </body>
                  </html>

<?php
}           
?>
                    <div class="image1">
                    <img src ="images/line.png";  />
                    </div>
                   <?php
}           
?>

如何解决此问题?

1 个答案:

答案 0 :(得分:1)

您可以检索存储在浏览器中的cookie并将其粘贴到else循环的开头并将其存储在变量screen_name1中

$ screen_name1 = $ _COOKIE [“COOKIE NAME”];