-2

i am just trying to destroy my session with session_destroy but its not getting destroy this is my code of logout.php

<?php
session_destroy();
//print_r($_SESSION);exit;  
?>
<script type="text/javascript">
    location.href=root_path+"index/";
</script>

i am getting a waring

Warning: session_destroy(): Session object destruction failed in F:\xampp\htdocs\synthesis\module\login\logout.php on line 3

if i remove third line from comment and click again on logout its showing me array of $_SESSION with all data and if i do comment it again without refreshing browser and then refresh the browser then session is getting destroy i am calling this page from my index page and on index page i have written session_start

Vivek Modi
  • 39
  • 8
  • you have start with session_start and then session_destroy – jvk Aug 18 '18 at 17:19
  • @VivekModi You have to use `session_start()` in every file unless you include this file in some parent file for the same HTTP response. – nice_dev Aug 18 '18 at 18:52

2 Answers2

0

Declare session_start(); first to close the existing sessions.

    <?php
     session_start(); 
     session_destroy();
     //print_r($_SESSION);exit;  
    ?>
    <script type="text/javascript">
       location.href=root_path+"index/";
    </script>
Bebeto Alves
  • 105
  • 5
  • please read my hole question i had mentioned that i had called my this page on my index page and i had written session_start over there on first line – Vivek Modi Aug 18 '18 at 17:26
  • i had written error_reporting on my index page if i remove that i am getting a warning which i had added in my question – Vivek Modi Aug 18 '18 at 17:28
0

You forgot the session_start before destroy it. and you redirect using php, here you are using script to redirect

<?php 
session_start();
session_destroy();  
header("Location: index.php");

?>
jvk
  • 2,133
  • 3
  • 19
  • 28
  • please read my hole question i had mentioned that i had called my this page on my index page and i had written session_start over there on first line – Vivek Modi Aug 18 '18 at 17:26
  • i had written error_reporting on my index page if i remove that i am getting a warning which i had added in my question – Vivek Modi Aug 18 '18 at 17:28
  • and sometimes header function give warnings that's why i am using javascript redirect – Vivek Modi Aug 18 '18 at 17:29
  • @VivekModi, The mistake is from your side and check this url to clean it up. next time https://stackoverflow.com/questions/17366719/php-session-destroy-warning-session-object-destruction-failed – jvk Aug 18 '18 at 17:30
  • i had now added $_SESSION=array(); session_regenerate_id(); on my logout page but still same problem is there – Vivek Modi Aug 18 '18 at 17:33
  • Warning: session_regenerate_id(): Cannot regenerate session id - headers already sent in F:\xampp\htdocs\synthesis\module\login\logout.php on line 4 Warning: session_destroy(): Session object destruction failed in F:\xampp\htdocs\synthesis\module\login\logout.php on line 5 getting these warning – Vivek Modi Aug 18 '18 at 17:34
  • i find the solution i have to written ob_start on my index page and now its working is it ok to use ob_start – Vivek Modi Aug 18 '18 at 17:37
  • Ok, you finally solved your problem, congrats – jvk Aug 18 '18 at 17:39