-3

Good morning guys, I have set this code and i would like to unset $_SESSION["username"], IF $_POST["logout"] isset

 <?php 


  if (isset($_POST["name"]) && isset($_POST["password"]))
    $_SESSION["username"]=$_POST["name"];        


    if(isset($_POST["logout"]))      
      unset($_SESSION["username"]);

?>
MOS
  • 31
  • 1
  • 9
  • `unset($_SESSION["username"]);` – JYoThI Aug 17 '17 at 07:11
  • 1
    Possible duplicate of [What is the difference between session\_unset() and session\_destroy() in PHP?](https://stackoverflow.com/questions/4303311/what-is-the-difference-between-session-unset-and-session-destroy-in-php) – Jigar Shah Aug 17 '17 at 07:12
  • I have tried that but it doesn't work – MOS Aug 17 '17 at 07:13
  • Did you start the session? And your indenting has no relation to how the code is actually being executed, `$_SESSION["username"]` will always be unset. – jeroen Aug 17 '17 at 07:19

1 Answers1

0

It seem some bad logic in code, try

if (isset($_POST["name"]) && isset($_POST["password"])) {
    $_SESSION["username"]=$_POST["name"];        
}

if(isset($_POST["logout"])) {
    unset($_SESSION["username"]);
} 
Ivan Bolnikh
  • 742
  • 9
  • 19