Why do cookies remain even after the page is redirected, the array of cookies is not displayed? And what does it mean that before sending the setcookie header, the header should not be output?

index.php

<?php error_reporting(-1); header('Content-Type: text/html; charset=utf-8'); ini_set('display_errors', 'On'); if(isset($_POST['login']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) && ($_POST['password'])){ setcookie('name', $_POST['login'], time()+3600*24*30, '/'); setcookie('email', $_POST['email'], time()+3600*24*30, '/'); setcookie('password', $_POST['password'], time()+3600*24*30, '/'); header('Location: index2.php'); } ?> <html> <head> <title>Авторизация</title> </head> <body> <div class="login" style="border: 1px solid; width: 220px; clear: both; overflow: hidden"> <p style="text-align: center;">Вход</p> <form action="" method="post"> <div style="clear: both;"> <span style="float: left">Логин:</span> <input type="text" name="login" style="float: right; width: 150px;"> </div> <div style="clear: both;"> <span style="float: left">Email:</span> <input type="text" name="email" style="float: right; width: 150px;"> </div> <div style="clear: both;"> <span style="float: left">Пароль:</span> <input type="text" name="password" style="float: right; width: 150px;"> </div> <div style="margin-top: 30px; overflow: hidden"> <input type="submit" value="Войти" style="float: right;"> </div> </form> </div> </body> </html> 

index2.php

 <?php error_reporting(-1); header('Content-Type: text/html; char set=utf-8'); ini_set('display_errors', 'On'); if(isset($_GET['exit']) && $_GET['exit'] == 1){ setcookie('name', "", time() - 3600, '/'); header('Location: index.php'); echo '<pre>'; print_r($_COOKIE); echo '</pre>'; exit(); } ?> <html> <head> <title>Личный кабинет</title> </head> <body> <?php echo 'Ваш логин: ' . $_COOKIE['name'] . '<br>'; echo 'Ваш email: ' . $_COOKIE['email'] . '<br>'; echo 'Ваш пароль: ' . $_COOKIE['password'] . '<br>'; ?> <form action="" method="GET"> <input type="hidden" name="exit" value="1"> <a href="index.php?exit=1">Выход</a> </form> </body> </html> 

    1 answer 1

    Well, look. You have a cookie removal script in the second file, etc. In the same file below, the exit form with a link like

     <a href="index.php?exit=1">Выход</a> 

    You send yourself to go to the first file, where there is no delete script.

    In general, everything is very clumsy and confusing. In the end, they themselves were confused. Do you understand?

    Do not understand. Correct the index2.php code above to the code below.

     <a href="index2.php?exit=1">Выход</a> 
    • Why put a link in the form? By the way, I do not recommend displaying the user's password in the account to the user. and even more so to keep it in the куках . куки can be stolen and viewed, they are stored on the client side. - webDev_
    • @webDev_ And where am I? You write it to him. I just pointed out the error. Fix on index2.php - and cookies are deleted. The issue is resolved. Others do not ask. - n.osennij
    • rendered the link by the form and still the cookies are not deleted - DivMan
    • @DivMan what have you done ?! what form did you take out the link for? what are you talking about? You have earned. Go rest. - n.osennij
    • I rested for 2 days. I do not understand why forwarding works, but deleting does not? - DivMan