2

Why I have to nullify the session variables?Is session_destroy not enough?

<?php
     session_start();
     $_SESSION = array();// <--Is necessary?
     if (isset($_COOKIE[session_name()]])){
        setcookie(session_name(),'',time()-42000,'/');
     }
     session_destroy();
     header('Location:Login.php');
?> 
giannis christofakis
  • 8,201
  • 4
  • 54
  • 65

2 Answers2

2

It is a precautionary measure to nullify the values, just in case the object is not destroyed by the session_destroy();.

Just precautionary and good practice, it is not required.

Why are you starting a session and destroying it right away tho?

Jakub
  • 20,418
  • 8
  • 65
  • 92
1

You should use session_unset() and session_destroy() both. Note that session_destroy() only empties out the variables when the page is reloaded or redirected to some other page. As long as it's the same page, the variables are still usable after invoking session_destroy(), so it is best practice to use session_unset() just before session_destroy().

Shakti Singh
  • 84,385
  • 21
  • 134
  • 153