title: Set cookie and update cookie problem - Php
body: <p>In an attempt to get more familiar with cookies I've decided to set up a simple cookie management system to have more control of the information that I can store and retrieve from a user.</p>
  
  <p>The idea is to set a cookie if it does not exist, and update a cookie if it already exists on the user. </p>
  
  <p>Once the cookie is set, it will also be stored in a database that will keep track on when the session started and when it was last accessed. </p>
  
  <p>Creating a cookie worked well at first. But suddenly it stopped working and wouldn't set anything at all. This is the current code of the createSession() function:</p>
  
  <pre><code>function createSession() {
  
  // check to see if cookie exists
  if(isset($_COOKIE["test"])) {
              // update time
      $expire = time()+81400;
      setcookie("test","$cookiekey",$expire,"/",false,0);
  } else {
      // assign unique cookie id
      list($msec,$sec)=explode(" ",microtime());
      $cookiekey = preg_replace("/./","",($msec+$sec));
              // set time
      $expire = time()+81400;
  
      // set cookie
      setcookie("test","$cookiekey",$expire,"/",false,0);
  
      // assign the unqiue id to $_COOKIE[]
      $_COOKIE["test"]=$cookiekey;
      unset($cookiekey);unset($msec);unset($sec);unset($expire);
     }
  </code></pre>
  
  <p>}</p>
  
  <p>Is my approach heading in the right direction or have I done something way wrong?</p>