title: onclick event with PHP SESSION variable happening on every page load
body: <p>I'm trying to implement a logout mechanism, which would consist of a link to click that should set a SESSION variable to True, send the user to the login page where PHP will check the value of the same variable and destroy the cookie before regenerating the session if it's set to true. The problem is that what should happen as an onclick event happens every time I load the page (And I can confirm this by echoing the variable at the top of the page, which returns always 1) except for the first time, where instead I get an error message because the variable is still not set. Here's my code:</p>
  
  <p>JavaScript:</p>
  
  <pre><code>&lt;script&gt;
  function destroy_session(){
      &lt;?php $_SESSION["Logout"]=True; ?&gt;
  }
  document.getElementById("logout").onclick=destroy_session;
  &lt;/script&gt;
  </code></pre>
  
  <p>HTML:</p>
  
  <pre><code>&lt;li&gt;&lt;a href="../index.php" id="logout"&gt;&lt;i class="fa fa-sign-out fa-fw"&gt;&lt;/i&gt; Logout&lt;/a&gt;
  </code></pre>
  
  <p>PHP:</p>
  
  <pre><code>if ($_SESSION['Logout']){
      session_unset();     
      session_destroy();
      session_start();
  }
  </code></pre>
  
  <p>Am I doing something wrong? Is there a way to fix this?</p>