title: Php-script ran outside apache gets "Permission denied" when opening cookies
body: <p>Allright. This is an interresting one.</p>
  
  <p>I am writing a socket server where i want to authenticate connecting users by passing their session id from the browser, and then loading some data about the user using that session.</p>
  
  <p>Cool, right? Sadly, it was not going to be that simple. When i try to set the current session in php using <code>session_id();</code>, i get these errors:</p>
  
  <pre><code>PHP Fatal error:  Call to a member function getUsername() on null in /home/#website#/public_html/api/socketserver/authenticateduser.php on line 35
  
  PHP Warning:  Unknown: open(/var/lib/php5/sessions/sess_2rkcsmjc6p7nj8ourihsdofeu3, O_RDWR) failed: Permission denied (13) in Unknown on line 0
  
  PHP Warning:  Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php5/sessions) in Unknown on line 0
  </code></pre>
  
  <p><strong>Can i see some code?</strong> Yes!</p>
  
  <pre><code>static function AuthenticateUser($session, $sessionid) {
      echo "Authenticating session id " . $sessionid . "\n";
      echo "session data: " . exec("cat /var/lib/php5/sessions/sess_" . $sessionid) . "\n";
      session_id($sessionid);
      //session_start();
      $user = Session::getCurrentUser();
      echo "Authented user " . $user-&gt;getUsername();
  }
  </code></pre>
  
  <p>Please note that i am still prototyping this code, so nothing is complete, and this may not be the final design.</p>
  
  <p>So, the first error happens because <code>Session::getCurrentUser()</code> returns null. This is understandable, as we aren't logged in by default, and the target session is never loaded(shown by errors below). Why the errors are below are beyond me.</p>
  
  <p>The two other errors are because of some file permissions.</p>
  
  <p><strong>Have you tried X?</strong></p>
  
  <ul>
  <li><strong>Have you tried running as root?</strong> Yes. I even ran <code>exec("cat /var/lib/php5/sessions/sess_" . $sessionid)</code> to prove i could read from the session file. Still no dice. This is why this case is so wierd.</li>
  <li><strong>What are the permissions?</strong> /var/lib/php5/sessions has rwxr-xr-t, and is owned by www-data. The session files inside have -rw------- permissions, and are owned by www-data.</li>
  <li><strong>What OS are you on? What versions?</strong> Ubuntu 15.04 with php 5.6.4-4ubuntu6.2</li>
  <li><strong>Does apache and php share the same session save path? You DO know php.ini are different for cli and apahe?</strong> Yes.</li>
  </ul>
  
  <p>This issue has bothered me for the past days, but because php had issues even when i ran the script as root, i am a bit perplexed.</p>