if(!isset($_COOKIE['oauth_token'])){
session_start();
if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {header('Location: ./clearsessions.php');}
$access_token = $_SESSION['access_token'];
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
setcookie('oauth_token', $access_token['oauth_token'], time()+60*60*24*30);
setcookie('oauth_token_secret', $access_token['oauth_token_secret'], time()+60*60*24*30);
}
//else use the cookies
else{
$oauth_token = $_COOKIE['oauth_token'];
$oauth_token_secret = $_COOKIE['oauth_token_secret'];
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_token, $oauth_token_secret);
}
Here is the snippet of code I am using, basically I want to store a long term cookie for the user so they don’t have to login to Twitter and go through the long process of authorizing my web app every time they connect. So I am attempting to use cookies with this library (twitteroauth) for some reason the page won’t load with this section of code, if I was to take out the if else and just leave the $_SESSION part in the code works without a hitch.
I’m quite confused as I’ve checked and the cookies are successfully getting stored, any ideas?
I think your ‘check’ for the cookie is incorrect (not code wise, but logic wise), let me remove all the code exect for the
if–else-statement:Your logic says ‘if the cookie exists use sessions, else use the cookie’ and I think you meant to say ‘if the cookie does not exist use sessions, else use the cookie’. You have to invert your check in the first
if-statement.Update I’ve re-read the section you posted over and over again and, as you say your script does not have any output with that piece of code, the only thing I can come up with is that your application crashes on
new TwitterOAuth(...);. Can you try to run your code without those two lines (temporarily remove them both and see if you have a page output).Update #2 I’ve noticed you said that your code-‘snippit’ does work, but the combined code does not. You use
header('Location: ./clearsessions.php');if the session is not set, if you replace that byecho "error";, do you get that in the browser window. Note This is very simplistic debugging as I have little knowledge of what goes on on your system, but it is proven to be effective in times. You can also choose to use the echo-flush()combo, which will force output to the browser everytime you pass a certain line (you can then check how far your code is executing).