title: Session Persistance Issue in Phalcon PHP
body: <p>I am using PhalconPHP, and trying to implement an authentication service. My front-end is an AngularJS 5 application that utilizes this service that is on a subdomain. (My front end is found at www.MyWebsite.com, and my service is found at service.MyWebsite.com) So far I have implemented the following method to start my session.</p>
  
  <pre><code>$di-&gt;setShared('session', function () {
      $session = new Session();
      $session-&gt;start();
      return $session;
  });
  </code></pre>
  
  <p>Then, I call my <code>loginUser</code> method when the user posts their username/password to the service</p>
  
  <pre><code>public function loginUser(array $userData){
      $username = $userData['username'];
      $password = $userData['password'];
      $user = Users::findFirst(
          [
              'conditions' =&gt; '(username = :username:) AND password = :password:',
              'bind' =&gt; [
                  'username'    =&gt; $username,
                  'password'    =&gt; $password,
               ],
               'columns' =&gt; ['users_id AS id, first, last, username, email']
          ]
      );
      $this-&gt;_registerSession($user);
      return $user-&gt;toArray();
  }
  </code></pre>
  
  <p>After a user attempts login, I stash the username and user ID into the session using the <code>_registerSession($user)</code> method.</p>
  
  <pre><code>private function _registerSession($user){
      $this-&gt;session-&gt;set(
          "auth",
          [
              "id" =&gt; $user-&gt;id,
              "username" =&gt; $user-&gt;username,
          ]
      );   
  }
  </code></pre>
  
  <p>At this point, the user is able to successfully log in, as I am able to return the users ID, first and last name, username, and email, with the username and ID stored in my sessions <code>auth</code> configuration.</p>
  
  <p><strong>The issue that I have is that when I refresh my page, the User gets logged out.</strong></p>
  
  <p>The first thing I figured was that since my Angular app is an SPA, so refreshing the page will drop the current global variable I have set to store information on the current user. So, in order to fix that, I think I need to re-pull the information from the server based on the session. </p>
  
  <p>So, I've implemented a method for <code>getCurrentUser()</code></p>
  
  <pre><code>public function getCurrentUser(){
      $id = $this-&gt;session-&gt;auth["id"];
      if($id == null){
          return null;
      }
      $user = Users::findFirst(
          [
              'conditions' =&gt; 'users_id = :id:',
              'bind' =&gt; [
                  'id'    =&gt; $id,
              ],
              'columns'    =&gt; "users_id AS id, first, last, username, email",
          ]
      );
      $this-&gt;_registerSession($user);
      return $user-&gt;toArray();
  }
  </code></pre>
  
  <p>And when I test this using Postman, I am able to post to the login endpoint, and then anytime in the next 30 minutes if I make a call to <code>getCurrentUser</code>, I end up getting my values back.</p>
  
  <p>BUT when I try to do this in the AngularJS app, I always get a null value on <code>$id</code> which tells me that it's not able to pull/doesn't see the <code>auth</code> in the session variable. </p>
  
  <p>I've verified that the PHPSESSIONID being set in the cookies isn't changing, so the sessionID should still be the same... So why can't it pull that ID from the existing session in the Angular app, but it can when I make calls directly from PostMan?</p>
  
  <p><strong>EDIT</strong>
  Someone mentioned that it may be due to a CORS. They deleted their answer, but just to show my setup for that, here is the index file where I am attaching certain headers</p>
  
  <pre><code>if (isset($_SERVER['HTTP_ORIGIN'])) {
      // Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one
      // you want to allow, and if so:
      header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
      header('Access-Control-Allow-Credentials: true');
      header('Access-Control-Max-Age: 86400');    // cache for 1 day
  }
  
  // Access-Control headers are received during OPTIONS requests
  if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  
      if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
          // may also be using PUT, PATCH, HEAD etc
          header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         
  
      if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
          header("Access-Control-Allow-Headers:{$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
      exit(0);
  }
  </code></pre>
  
  <p>It may still have something to do with CORS, but from my limited knowledge I don't see that being the case.</p>