<p>I'm trying to do a simple test php script for sessions. Basically it increments a counter (stored in <code>$_SESSION</code>) every time you refresh that page. That works, but I'm trying to have a link to destroy the session which reloads the page with the <code>?destroy=1</code> parameter. I've tried a couple of if statements to see if that parameter is set and if so to destroy the session but it doesn't seem to work.</p>

<p>I've even put an if statement in the main body to pop-up a message if the parameter is set - but it doesn't seem to be picked up.</p>

<p>I know I'm doing something silly (I'm a PHP newbie) but I can't seem to find what it is...</p>

<p>See code here:</p>

<pre><code>&lt;?php
if ($_POST['destroy']) {
    session_destroy();
} else {
    session_start();
}    
?&gt;

&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Session test&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;?php
    if (isset($_POST['destroy'])) {
        echo "Destroy set";
    }

    $_SESSION['counter']++;

    echo "You have visited this page " . $_SESSION['counter'] . " times" . "&lt;BR&gt;";
    echo "I am tracking you using the session id " . session_id() . "&lt;BR&gt;";
    echo "Click &lt;a href=\"" . $_SERVER['PHP_SELF'] . "?destroy=1\"&gt;here&lt;/a&gt; to destroy the session.";
?&gt;
</code></pre>

[{'answer_id': 16818, 'author': 'Andrew G. Johnson', 'author_id': 428190, 'author_profile': 'https://Stackoverflow.com/users/428190', 'pm_score': 4, 'selected': True, 'text': "<p>I think you put</p>\n\n<pre><code>$_POST['destroy']\n</code></pre>\n\n<p>Instead of</p>\n\n<pre><code>$_GET['destroy']\n</code></pre>\n\n<p>You need to use a form if you'd like to use a $_POST variable.  $_GET variables are stored in the URL.</p>\n"}, {'answer_id': 16826, 'author': 'ftdysa', 'author_id': 2016, 'author_profile': 'https://Stackoverflow.com/users/2016', 'pm_score': 1, 'selected': False, 'text': "<p>Yeah, you're going to want to do </p>\n\n<pre><code>if( $_GET['destroy'] == 1 )\n</code></pre>\n\n<p>or</p>\n\n<pre><code>if( isset($_GET['destroy']) )\n</code></pre>\n"}, {'answer_id': 16944, 'author': 'Pat', 'author_id': 238, 'author_profile': 'https://Stackoverflow.com/users/238', 'pm_score': 2, 'selected': False, 'text': "<p>By the way you can use </p>\n\n<blockquote>\n  <p>$_REQUEST['destroy'] </p>\n</blockquote>\n\n<p>which would work regardless if the data is passed in a POST or a GET request.</p>\n"}, {'answer_id': 17104, 'author': 'Pierre Spring', 'author_id': 1532, 'author_profile': 'https://Stackoverflow.com/users/1532', 'pm_score': 0, 'selected': False, 'text': "<blockquote>\n  <p>I know I'm doing something silly (I'm a php newbie) but I can't seem to find what it is...</p>\n</blockquote>\n\n<p>that is how you are going to learn a lot ;) enjoy it ...</p>\n"}, {'answer_id': 17141, 'author': 'grom', 'author_id': 486, 'author_profile': 'https://Stackoverflow.com/users/486', 'pm_score': 2, 'selected': False, 'text': '<p>In the <a href="http://au2.php.net/manual/en/function.session-destroy.php" rel="nofollow noreferrer">PHP Manual</a> it has code snippet for destroying a session.</p>\n\n<pre><code>session_start();\n$_SESSION = array();\nif (isset($_COOKIE[session_name()])) {\n    setcookie(session_name(), \'\', time()-42000, \'/\');\n}\nsession_destroy();\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/16815', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1912/']