title: MongoDB Login system & sessions in PHP
body: <p>I am using MongoDB on my PHP website to keep hold of my registered users, and their login. So far, so good.</p>
  
  <p>When I log in, I am using the following (simplified):</p>
  
  <pre><code>$login = $collection-&gt;findOne(array("mail" =&gt; $mailIn, "pass" =&gt; $passIn));
  if($login) {
      $_SESSION["gatekeeper"] = $login['_id'];
  }
  </code></pre>
  
  <p>and when I register, the following:</p>
  
  <pre><code>$collection-&gt;insert($obj);
  mkdir("$appDirectory/users/" . $obj["_id"]);
  $_SESSION["gatekeeper"] = $obj["_id"];
  </code></pre>
  
  <p>The whole system is working good, but there is something that is bugging me: take a look at the JSON derived from this in the session manager:</p>
  
  <pre><code>"{\"gatekeeper\":{\"$id\":\"505f1cd25c73959504000000\"}}" (redis)
  array(1) { ["$id"]=&gt; string(24) "505f1cd25c73959504000000" } (php var dump - gatekeeper)
  </code></pre>
  
  <p>as you can see, it's storing it as an associative array $id->id.</p>
  
  <p>of course it would be much better to have simply gatekeepeer->id.</p>
  
  <p>How can I change this easily? I honestly find it confusing because of this "$id" that makes my PHP confused a bit, and confuses me as well.</p>
  
  <hr>
  
  <p>Basically the annoyance is when I have to retrieve the gatekeeper:</p>
  
  <pre><code>$gatekeeper = $_SESSION["gatekeeper"];
  
  $userID = $gatekeeper['$id']; //WILL work
  $userID = $gatekeeper; //WON'T WORK
  </code></pre>
  
  <p>it would be much simpler (and better) to just have:</p>
  
  <pre><code>$userID = $_SESSION["gateID"];
  $userName = $_SESSION["gateName"];
  ....
  </code></pre>
  
  <p>instead of:</p>
  
  <pre><code>$gatekeeper = $_SESSION["gatekeeper"];
  
  $userID = $gatekeeper['$id'];
  $userName = $gatekeeper['name'];
  </code></pre>
  
  <p>since the $_SESSION always refer to the same session there is no need to have an array inside the array.</p>
  
  <p>Thanks in advance.</p>