title: CakePHP - Saving one session variable deletes another
body: <p>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.</p>
  
  <p>I have a screen where the user can link and unlink said social network data, the problem is the following:</p>
  
  <p>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.</p>
  
  <p>The way my process works is the following: </p>
  
  <p>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.</p>
  
  <p>Code is as follows:</p>
  
  <p>This is the function that the user is directed after login in to Twitter or FB:</p>
  
  <pre><code>function retrieve_network($page) {
        $networkData = null;
        $this-&gt;autoRender = false;
        $this-&gt;layout = 'ajax';
        if(isset($_GET['oauth_token'])) {
          $token = $this-&gt;TwitterHelper-&gt;setOAuthToken($_GET['oauth_token']);
          $userinfo = $this-&gt;TwitterHelper-&gt;getTwitterUserInfo();
          $networkData = array(
            'TwitterData' =&gt; array(
              'username' =&gt; $userinfo['username'],
              'name' =&gt; $userinfo['name'],
              'token' =&gt; $token-&gt;oauth_token,
              'token_secret' =&gt; $token-&gt;oauth_token_secret
            )
          );
        } else if (isset($_GET['code'])) {
          $token = $this-&gt;FacebookHelper-&gt;facebook-&gt;getAccessToken();
          $userinfo = $this-&gt;FacebookHelper-&gt;getUserInfo();
          $networkData = array(
            'FacebookData' =&gt; array(
              'username' =&gt; $userinfo['username'],
              'name' =&gt; $userinfo['name'],
              'email' =&gt; $userinfo['email'],
              'token' =&gt; $token,
              'link' =&gt; $userinfo['link'],
            )
          );
        }
  
        $this-&gt;Session-&gt;write('NetworkData', $networkData);
  
        if($page == 'settings') {
          $this-&gt;redirect(array('controller' =&gt; 'fonykers', 'action' =&gt; 'settings/networks'));
        }
      }
  </code></pre>
  
  <p>This is the function that retrieves what's in network data and sets it to the session:</p>
  
  <pre><code>function settings($tab) {
        $this-&gt;layout = 'frontend';  
        $this-&gt;Fonyker-&gt;recursive = -1;
        $this-&gt;TwitterData-&gt;recursive = -1;
        $this-&gt;FacebookData-&gt;recursive = -1;
  
        if(!$this-&gt;checkSessionCookie()) {
          $this-&gt;redirect(array('controller' =&gt; 'pages', 'action' =&gt; 'home'));
        }
  
        $fields = array(
          'Fonyker.id',
          'Fonyker.username',
          'Fonyker.name',
          'Fonyker.email',
          'Fonyker.gender',
          'Fonyker.birthdate',
          'Fonyker.image_url'
        );
  
        $fonyker = $this-&gt;Fonyker-&gt;find('first', array(
          'conditions' =&gt; array(
            'Fonyker.fonykid' =&gt; $this-&gt;Session-&gt;read('Fonyker.Fonyker.fonykid')
          ),
          'fields' =&gt; $fields
        ));
  
  
        $this-&gt;Fonyker-&gt;set($fonyker);
  
        $this-&gt;data = $fonyker;
  
        if($this-&gt;Session-&gt;read('NetworkData')) {
          $networkData = $this-&gt;Session-&gt;read('NetworkData');
          $this-&gt;Session-&gt;delete('NetworkData');  
  
          if($networkData['TwitterData']) {
            $networkData['TwitterData']['fonyker_id'] = $fonyker['Fonyker']['id']; 
            if($this-&gt;TwitterData-&gt;save($networkData)) {
              $this-&gt;Session-&gt;write('TwitterData', $networkData['TwitterData']);
            }
          } else if($networkData['FacebookData']) {
            $networkData['FacebookData']['fonyker_id'] = $fonyker['Fonyker']['id']; 
            if($this-&gt;FacebookData-&gt;save($networkData)) {
              $this-&gt;Session-&gt;write('FacebookData', $networkData['FacebookData']);
            }
          }
        }
  
        pr($this-&gt;Session-&gt;read());
  
        if(!$this-&gt;Session-&gt;read('TwitterData')) {
          $this-&gt;TwitterHelper-&gt;setTwitterObj();
          $this-&gt;set('twitterUrl', $this-&gt;TwitterHelper-&gt;twitterObj-&gt;getAuthorizeUrl(null, array('oauth_callback' =&gt; 'http://127.0.0.1/fonykweb/pages/retrieve_network/settings')));
        } else {
          $this-&gt;set('twitterUrl', '#');
        }
  
  
        if(!$this-&gt;Session-&gt;read('FacebookData')) {
          $this-&gt;set('facebookUrl', $this-&gt;FacebookHelper-&gt;facebook-&gt;getLoginUrl(array('redirect_uri' =&gt; 'http://localhost/fonykweb/pages/retrieve_network/settings','scope' =&gt; 'email,user_birthday,publish_stream,offline_access')));
        } else {
          $this-&gt;set('facebookUrl', '#');
        } 
  
        $this-&gt;set('tab', $tab);
      }
  </code></pre>
  
  <p>This is the function that removes the network if the user wishes:</p>
  
  <pre><code>function remove_network($network) {
        $this-&gt;autoRender = false;
        $this-&gt;Fonyker-&gt;recursive = -1;
        $this-&gt;TwitterData-&gt;recursive = -1;
        $this-&gt;FacebookData-&gt;recursive = -1;
        $response = null;
  
        if($network == 'twitter') {
          $twitterData = $this-&gt;TwitterData-&gt;find('first', array(
            'conditions' =&gt; array(
              'TwitterData.fonyker_id' =&gt; $this-&gt;Session-&gt;read('TwitterData.fonyker_id')
            )
          ));
  
          if($this-&gt;TwitterData-&gt;delete($twitterData['TwitterData']['id'], false)) {
            $this-&gt;TwitterHelper-&gt;setTwitterObj();
            $twitterUrl = $this-&gt;TwitterHelper-&gt;twitterObj-&gt;getAuthorizeUrl(null, array('oauth_callback' =&gt; 'http://127.0.0.1/fonykweb/pages/retrieve_network/settings'));
            $this-&gt;Session-&gt;delete('TwitterData');
            $response = json_encode(array('ok' =&gt; true, 'url' =&gt; $twitterUrl));
          } else {
            $response = json_encode(array('ok' =&gt; false)); 
          }
        }
  
        if($network == 'facebook') {
          $facebookData = $this-&gt;FacebookData-&gt;find('first', array(
            'conditions' =&gt; array(
              'FacebookData.fonyker_id' =&gt; $this-&gt;Session-&gt;read('FacebookData.fonyker_id')
            )
          ));
  
          if($this-&gt;FacebookData-&gt;delete($facebookData['FacebookData']['id'], false)) {
            $facebookUrl = $this-&gt;FacebookHelper-&gt;facebook-&gt;getLoginUrl(array('redirect_uri' =&gt; 'http://localhost/fonykweb/pages/retrieve_network/settings','scope' =&gt; 'email,user_birthday,publish_stream,offline_access'));
            $this-&gt;Session-&gt;delete('FacebookData');
            $response = json_encode(array('ok' =&gt; true, 'url' =&gt; $facebookUrl));
          } else {
            $response = json_encode(array('ok' =&gt; false)); 
          }
  
        }
  
    echo $response; 
  }
  </code></pre>
  
  <p>View code:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
    $(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');
    });
  &lt;/script&gt;
  &lt;div class="prepend-1 prepend-top span-23"&gt;
  &lt;div class="tabs span-22"&gt;
    &lt;ul&gt;
      &lt;li id="account-tab"&gt;
        &lt;a href="&lt;?php echo $html-&gt;url(array('controller' =&gt; 'fonykers', 'action' =&gt; 'settings'), true); ?&gt;/account"&gt;
          Account
        &lt;/a&gt;
      &lt;/li&gt;
      &lt;li id="password-tab"&gt;
        &lt;a href="&lt;?php echo $html-&gt;url(array('controller' =&gt; 'fonykers', 'action' =&gt; 'settings'), true); ?&gt;/password"&gt;
          Password 
        &lt;/a&gt;
      &lt;/li&gt;
      &lt;li id="notifications-tab"&gt;
        &lt;a href="&lt;?php echo $html-&gt;url(array('controller' =&gt; 'fonykers', 'action' =&gt; 'settings'), true); ?&gt;/notifications"&gt;
          Notifications 
        &lt;/a&gt;
      &lt;/li&gt;
      &lt;li id="networks-tab"&gt;
        &lt;a href="&lt;?php echo $html-&gt;url(array('controller' =&gt; 'fonykers', 'action' =&gt; 'settings'), true); ?&gt;/networks"&gt; 
         Social Networks 
        &lt;/a&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
  &lt;div class="tab-content prepend-top prepend-1"&gt;
    &lt;?php 
      if($tab == 'account') {
        echo $this-&gt;element('settings/account');
      } else if ($tab == 'password') {
        echo $this-&gt;element('settings/password');
      } else if ($tab == 'notifications') {
        echo $this-&gt;element('settings/notifications');
      } else {
        echo $this-&gt;element('settings/networks');
      }
    ?&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Element code:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
    $(document).ready(function(){
  
      var deleteNetwork = function(network, button) {
        $.ajax({
          url: '&lt;?php echo $html-&gt;url('/fonykers/remove_network/', true); ?&gt;' + network,
          dataType: 'json',
          type: 'POST',
          success: function(response) {
            if(response.ok) {
              button.replaceWith('&lt;a id="'+network+'-connect" class="connect-button connect" href="'+response.url+'" class="span-3"&gt;Connect&lt;/a&gt;');
            }
          }
        });  
      }
  
      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));
        }
      });
  
    });
  &lt;/script&gt;
  &lt;div class="span-4 prepend-top"&gt;
  &lt;div class="span-4"&gt;
    &lt;div class="span-1"&gt;
      &lt;?php echo $html-&gt;image('twitter-connect.png', array('alt' =&gt; 'Twitter', 'class' =&gt; 'span-1',  'style' =&gt; 'height:40px;width:40px')); ?&gt;
    &lt;/div&gt;
    &lt;div class="span-3 last"&gt;
      &lt;a id="twitter-connect" class="connect-button" href="&lt;?php echo $twitterUrl; ?&gt;" class="span-3"&gt;&lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="span-4 prepend-top"&gt;
    &lt;div class="span-1"&gt;
    &lt;?php echo $html-&gt;image('facebook-connect.png', array('alt' =&gt; 'Twitter', 'class' =&gt; 'span-1', 'style' =&gt; 'height:40px;width:40px')); ?&gt;
    &lt;/div&gt;
    &lt;div class="span-3 last"&gt;
      &lt;a id="facebook-connect" class="connect-button" href="&lt;?php echo $facebookUrl; ?&gt;"&gt;&lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Sorry for the long post.</p>