Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6475983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:46:11+00:00 2026-05-25T06:46:11+00:00

I’m using session variables in CakePHP to store my relevant user Twitter and Facebook

  • 0

I’m using session variables in CakePHP to store my relevant user Twitter and Facebook data, when the user logs in if he has linked his Twitter and FB accounts this information is saved in the session variable next to my own user data.

I have a screen where the user can link and unlink said social network data, the problem is the following:

Let’s say I have both networks connected, I decide to disconnect from Facebook, the session variable for Facebook is deleted. Now I wish to reconnect to Facebook, I click on the Connect button, the Facebook data is saved but for some reason it deletes the Twitter variable.

The way my process works is the following:

1) User clicks on connect button.
2) User is directed to social network auth.
3) User is directed to a function that takes the required data, saves it in a session variable called NetworkData and is directed back to the page where he clicked the button.
4) The NetworkData is extracted, set as the according social network (Facebook or Twitter) in the session and is deleted from the session.

Code is as follows:

This is the function that the user is directed after login in to Twitter or FB:

function retrieve_network($page) {
      $networkData = null;
      $this->autoRender = false;
      $this->layout = 'ajax';
      if(isset($_GET['oauth_token'])) {
        $token = $this->TwitterHelper->setOAuthToken($_GET['oauth_token']);
        $userinfo = $this->TwitterHelper->getTwitterUserInfo();
        $networkData = array(
          'TwitterData' => array(
            'username' => $userinfo['username'],
            'name' => $userinfo['name'],
            'token' => $token->oauth_token,
            'token_secret' => $token->oauth_token_secret
          )
        );
      } else if (isset($_GET['code'])) {
        $token = $this->FacebookHelper->facebook->getAccessToken();
        $userinfo = $this->FacebookHelper->getUserInfo();
        $networkData = array(
          'FacebookData' => array(
            'username' => $userinfo['username'],
            'name' => $userinfo['name'],
            'email' => $userinfo['email'],
            'token' => $token,
            'link' => $userinfo['link'],
          )
        );
      }

      $this->Session->write('NetworkData', $networkData);

      if($page == 'settings') {
        $this->redirect(array('controller' => 'fonykers', 'action' => 'settings/networks'));
      }
    }

This is the function that retrieves what’s in network data and sets it to the session:

function settings($tab) {
      $this->layout = 'frontend';  
      $this->Fonyker->recursive = -1;
      $this->TwitterData->recursive = -1;
      $this->FacebookData->recursive = -1;

      if(!$this->checkSessionCookie()) {
        $this->redirect(array('controller' => 'pages', 'action' => 'home'));
      }

      $fields = array(
        'Fonyker.id',
        'Fonyker.username',
        'Fonyker.name',
        'Fonyker.email',
        'Fonyker.gender',
        'Fonyker.birthdate',
        'Fonyker.image_url'
      );

      $fonyker = $this->Fonyker->find('first', array(
        'conditions' => array(
          'Fonyker.fonykid' => $this->Session->read('Fonyker.Fonyker.fonykid')
        ),
        'fields' => $fields
      ));


      $this->Fonyker->set($fonyker);

      $this->data = $fonyker;

      if($this->Session->read('NetworkData')) {
        $networkData = $this->Session->read('NetworkData');
        $this->Session->delete('NetworkData');  

        if($networkData['TwitterData']) {
          $networkData['TwitterData']['fonyker_id'] = $fonyker['Fonyker']['id']; 
          if($this->TwitterData->save($networkData)) {
            $this->Session->write('TwitterData', $networkData['TwitterData']);
          }
        } else if($networkData['FacebookData']) {
          $networkData['FacebookData']['fonyker_id'] = $fonyker['Fonyker']['id']; 
          if($this->FacebookData->save($networkData)) {
            $this->Session->write('FacebookData', $networkData['FacebookData']);
          }
        }
      }

      pr($this->Session->read());

      if(!$this->Session->read('TwitterData')) {
        $this->TwitterHelper->setTwitterObj();
        $this->set('twitterUrl', $this->TwitterHelper->twitterObj->getAuthorizeUrl(null, array('oauth_callback' => 'http://127.0.0.1/fonykweb/pages/retrieve_network/settings')));
      } else {
        $this->set('twitterUrl', '#');
      }


      if(!$this->Session->read('FacebookData')) {
        $this->set('facebookUrl', $this->FacebookHelper->facebook->getLoginUrl(array('redirect_uri' => 'http://localhost/fonykweb/pages/retrieve_network/settings','scope' => 'email,user_birthday,publish_stream,offline_access')));
      } else {
        $this->set('facebookUrl', '#');
      } 

      $this->set('tab', $tab);
    }

This is the function that removes the network if the user wishes:

function remove_network($network) {
      $this->autoRender = false;
      $this->Fonyker->recursive = -1;
      $this->TwitterData->recursive = -1;
      $this->FacebookData->recursive = -1;
      $response = null;

      if($network == 'twitter') {
        $twitterData = $this->TwitterData->find('first', array(
          'conditions' => array(
            'TwitterData.fonyker_id' => $this->Session->read('TwitterData.fonyker_id')
          )
        ));

        if($this->TwitterData->delete($twitterData['TwitterData']['id'], false)) {
          $this->TwitterHelper->setTwitterObj();
          $twitterUrl = $this->TwitterHelper->twitterObj->getAuthorizeUrl(null, array('oauth_callback' => 'http://127.0.0.1/fonykweb/pages/retrieve_network/settings'));
          $this->Session->delete('TwitterData');
          $response = json_encode(array('ok' => true, 'url' => $twitterUrl));
        } else {
          $response = json_encode(array('ok' => false)); 
        }
      }

      if($network == 'facebook') {
        $facebookData = $this->FacebookData->find('first', array(
          'conditions' => array(
            'FacebookData.fonyker_id' => $this->Session->read('FacebookData.fonyker_id')
          )
        ));

        if($this->FacebookData->delete($facebookData['FacebookData']['id'], false)) {
          $facebookUrl = $this->FacebookHelper->facebook->getLoginUrl(array('redirect_uri' => 'http://localhost/fonykweb/pages/retrieve_network/settings','scope' => 'email,user_birthday,publish_stream,offline_access'));
          $this->Session->delete('FacebookData');
          $response = json_encode(array('ok' => true, 'url' => $facebookUrl));
        } else {
          $response = json_encode(array('ok' => false)); 
        }

      }

  echo $response; 
}

View code:

<script type="text/javascript">
  $(document).ready(function() {
    var splitUrl = window.location.href.split('/');
    $('#' + splitUrl[splitUrl.length - 1] + '-tab').addClass('active-tab');
    $('#' + splitUrl[splitUrl.length - 1] + '-tab').children().addClass('active-tab');
  });
</script>
<div class="prepend-1 prepend-top span-23">
<div class="tabs span-22">
  <ul>
    <li id="account-tab">
      <a href="<?php echo $html->url(array('controller' => 'fonykers', 'action' => 'settings'), true); ?>/account">
        Account
      </a>
    </li>
    <li id="password-tab">
      <a href="<?php echo $html->url(array('controller' => 'fonykers', 'action' => 'settings'), true); ?>/password">
        Password 
      </a>
    </li>
    <li id="notifications-tab">
      <a href="<?php echo $html->url(array('controller' => 'fonykers', 'action' => 'settings'), true); ?>/notifications">
        Notifications 
      </a>
    </li>
    <li id="networks-tab">
      <a href="<?php echo $html->url(array('controller' => 'fonykers', 'action' => 'settings'), true); ?>/networks"> 
       Social Networks 
      </a>
    </li>
  </ul>
</div>
<div class="tab-content prepend-top prepend-1">
  <?php 
    if($tab == 'account') {
      echo $this->element('settings/account');
    } else if ($tab == 'password') {
      echo $this->element('settings/password');
    } else if ($tab == 'notifications') {
      echo $this->element('settings/notifications');
    } else {
      echo $this->element('settings/networks');
    }
  ?>
</div>
</div>

Element code:

<script type="text/javascript">
  $(document).ready(function(){

    var deleteNetwork = function(network, button) {
      $.ajax({
        url: '<?php echo $html->url('/fonykers/remove_network/', true); ?>' + network,
        dataType: 'json',
        type: 'POST',
        success: function(response) {
          if(response.ok) {
            button.replaceWith('<a id="'+network+'-connect" class="connect-button connect" href="'+response.url+'" class="span-3">Connect</a>');
          }
        }
      });  
    }

    if($('#twitter-connect').attr('href') == '#'){
      $('#twitter-connect').addClass('connected');
      $('#twitter-connect').html('Connected');
    } else {
      $('#twitter-connect').addClass('connect');
      $('#twitter-connect').html('Connect'); 
    }

    if($('#facebook-connect').attr('href') == '#'){
      $('#facebook-connect').addClass('connected');
      $('#facebook-connect').html('Connected');
    } else {
      $('#facebook-connect').addClass('connect');
      $('#facebook-connect').html('Connect'); 
    }

    $('.connected').hover(
      function() { 
        $(this).removeClass('connected');
        $(this).addClass('disconnect');
        $(this).html('Disconnect')
      },
      function() {
        $(this).removeClass('disconnect');
        $(this).addClass('connected');
        $(this).html('Connected')
      }
    );

    $('#twitter-connect').click(function(event) {
      if($(this).attr('href') == '#') {
        event.preventDefault();
        deleteNetwork('twitter', $(this));
      }
    });

    $('#facebook-connect').click(function(event) {
      if($(this).attr('href') == '#') {
        event.preventDefault();
        deleteNetwork('facebook', $(this));
      }
    });

  });
</script>
<div class="span-4 prepend-top">
<div class="span-4">
  <div class="span-1">
    <?php echo $html->image('twitter-connect.png', array('alt' => 'Twitter', 'class' => 'span-1',  'style' => 'height:40px;width:40px')); ?>
  </div>
  <div class="span-3 last">
    <a id="twitter-connect" class="connect-button" href="<?php echo $twitterUrl; ?>" class="span-3"></a>
  </div>
</div>
<div class="span-4 prepend-top">
  <div class="span-1">
  <?php echo $html->image('facebook-connect.png', array('alt' => 'Twitter', 'class' => 'span-1', 'style' => 'height:40px;width:40px')); ?>
  </div>
  <div class="span-3 last">
    <a id="facebook-connect" class="connect-button" href="<?php echo $facebookUrl; ?>"></a>
  </div>
</div>
</div>

Sorry for the long post.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:46:11+00:00Added an answer on May 25, 2026 at 6:46 am

    In your retrieve_network action, which I am assuming you call when a user reconnects to a particular service, you are overwriting the NetworkData session variable
    if you find 1 particular Service has been connected to:

    if(isset($_GET['oauth_token'])) {...} 
    

    OR

    if(isset($_GET['code'])) {...}
    

    You set $networkData to the returned services object and then overwrite the whole session via:

    $this->Session->write('NetworkData', $networkData);
    

    Following your code, I would always check to see if an existing service is currently in session and if so do not overwrite the whole session, just add the particular data to the existing NetworkData session array:

    if($this->Session->read('NetworkData.TwitterData')){
        $facebookData = array(
                    'username' => $userinfo['username'],
                    'name' => $userinfo['name'],
                    'email' => $userinfo['email'],
                    'token' => $token,
                    'link' => $userinfo['link'],
        );
        $this->Session->write('NetworkData.FacebookData', $facebookData);
    }
    

    Note: This is just an example showing how you can achieve this. I would refactor that method with better logic for this particular situation, perhaps storing TwitterData and FacebookData in their own separate arrays as opposed to a larger more complex NetworkData array. Additionally, you can access $_GET params via $this->params[‘url’][‘paramname’] to maintain cakePHP convention.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
Seemingly simple, but I cannot find anything relevant on the web. What is the
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.