It is necessary to call such a function through POST:

if (isset($_POST['exit'])) { unset($_COOKIE['token']); setcookie('token', null, -1); header('Location: index.php'); } 

Tell me how best to do this?

    1 answer 1

    There are a couple of options:

    1. Using the form:

       <form action="action.php" method="POST"> <input type="hidden" name="exit" value="1" /> <a class="submit" href="#">Exit</a> </form> <script> $('form .submit').click(function () { $(this).parents('form').submit(); }); </script> 
    2. Using the $.post() method of the jQuery library:

       <a class="redirectable" href="action.php?exit=1">Exit</a> <script> $('.redirectable').click(function () { $.post($(this).attr('href')) .success(function (data) { if (data && data.result === 'ok') window.location.href = data.location; }); }); </script> // или <a id="exit" href="action.php">Exit</a> <script> $('#exit').click(function () { $.post($(this).attr('href'), { exit: 1 }) .success(function (data) { if (data && data.result === 'ok') window.location.href = data.location; }); }); </script> 

      At the same time, the PHP file will have to be corrected, since ajax cannot be intercepted by redirects:

       if (isset($_POST['exit'])) { unset($_COOKIE['token']); setcookie('token', null, -1); die(json_encode(array('result' => 'ok', 'location' => '/index.php'))); }