title: how to unset cookie in PHP?
body: <p>I need to figure out how to unset this cookie. Everything I tried so far has failed.</p>
  
  <p>This is how I am currently unsetting it and it doesn't seem to work.</p>
  
  <pre><code>setcookie("user_id", $user_id, time() - 7200);
  </code></pre>
  
  <p>This is how I set it:</p>
  
  <pre><code>setcookie("user_id", $user_id, time() + 7200);
  </code></pre>
  
  <p>I have this function called <code>set_session_from_cookie()</code> that checks if a cookie is set, and if it is set, it starts a new session using the cookie.</p>
  
  <p>The problem is that when I use this on my page I am unable to logout. I assume this is because I am unable to unset the session.</p>
  
  <p>The reason I have this function is if a user wants to be remembered after they end the session, they can restart the session by calling the cookie.</p>
  
  <pre><code>function set_session_from_cookie()
  {
      if (isset($_SESSION['user_id'])) {
          echo '';
      } else {
          $_SESSION['user_id']=$_COOKIE['user_id'];
      }
  }
  </code></pre>
  
  <p>Logout:</p>
  
  <pre><code>&lt;?php
  require'core.php';
  session_destroy();
  
  setcookie("user_id", "", time() - 7200);
  header('Location:/social_learning/site_pages/starter-template.php');
  </code></pre>
  
  <p>I set my cookie with the following code:</p>
  
  <pre><code>if ($rememberme == "on") {
      $user_id = mysql_result($query_run, 0, 'id');
      setcookie("user_id", $user_id, time() + 7200);
      $_SESSION['user_id'] = $user_id;
      redirect('home_page.php');
  } else {
      if ($rememberme == "") {
          echo 'ok';
          $user_id = mysql_result($query_run, 0, 'id');
          echo $user_id;
          $_SESSION['user_id'] = $user_id;
          redirect('home_page.php');
      }
  }
  </code></pre>
  
  <p>How can I restart the session using the saved cookie without using the function I created? Since the function seems to be causing the user to no longer be able to logout.</p>