Php exit button

I have this code and need a code to add a logout button, can someone write code for a logout button that logs out, I read something about destroying a session, but I don’t know how to write the code you , thanks!

<?php include 'connection.php'; //start of checking if user is logged in code if (!valid_credentials) { header('Location: login.php'); exit(); } $_SESSION['user'] = 'username'; if (!isset($_SESSION['user'])) { header('Location: login.php'); exit(); } //end of logged in code and starting a session $query = "SELECT * FROM people"; $result = mysql_query($query); While($person = mysql_fetch_array($result)) { echo "<h3>" . $person['Name'] . "</h3>"; echo "<p>" . $person['Description'] . "</p>"; echo "<a href=\"modify.php?id=" . $person['ID']. "\">Modify User</a>"; echo "<span> </span>"; echo "<a href=\"delete.php?id=" . $person['ID']. "\">Delete User</a>"; } ?> <h1>Create a User</h1> <form action="create.php" method="post"> Name<input type ="text" name="inputName" value="" /><br /> Description<input type ="text" name="inputDesc" value="" /> <br /> <input type="submit" name="submit" /> </form> 
+7
source share
2 answers

Place a link instead of a button and go to another page

 <a href="logout.php">Logout</a> 

Then on the logout.php page use

 session_start(); session_destroy(); header('Location: login.php'); exit; 
+18
source

If you want to completely destroy the session, you need to do more, just

 session_destroy(); 

First, you must disable any session variables. Then you must destroy the session, and then close the session record. This can be done as follows:

 <?php session_start(); unset($_SESSION); session_destroy(); session_write_close(); header('Location: /'); die; ?> 

The reason you want to have a separate script to exit is because you don't accidentally execute it on the page. So make a link to your script output, then the header will be redirected to the root of your site.

Edit:

You need to remove () from the exit code at the top of your script. it should just be

 exit; 
+8
source

All Articles