PHP session variable not changing value after navigation

I’ve got a few pages on my site where I pull through some organisation’s twitter feed. I’m using WordPress and I get their twitter handle using a custom field which loads dynamically based on which page / post you’re on.

However, on my homepage I’m using our organisations twitter handle so you get general tweets from us and then specific organisation tweets on their profile pages.

Read More

This works fine until I navigate back to the homepage and then the tweets either taking far too long to load or don’t load at all. I think it’s something to do with my $_SESSION['variable'] variable logic.

Here’s my code sessions.php which I include in the header.php file which is on all of my website’s pages.

session_start();

if(is_front_page()) {
    $_SESSION['twitter'] = "OurOrganisation";
}

else {
    // SET UP SINGLE ORG META DATA
    $_SESSION['twitter'] = get_post_meta($post->ID, 'wpcf-twitter-usename', true);
}

And then this is the tweets.php page where I access my twitter OUTH tokens and encode the tweets into JSON for my jquery twitter feed.

session_start();
require_once("twitteroauth-master/twitteroauth/twitteroauth.php");      //Path to twitteroauth library

$twitteruser = $_SESSION['twitter'];
$notweets = 30;
$consumerkey = "****";
$consumersecret = "****";
$accesstoken = "****";
$accesstokensecret = "****";


function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
  $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
  return $connection;
}

$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);

$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);

echo json_encode($tweets);
unset($_SESSION['twitter']); // tidy up

Could anyone tell me what is causing this erratic behaviour described above? $_SESSION['twitter'] is a textual value of a twitter handle / screen name.

Related posts