I'm using the code below to log users in. When the new session is created they are redirected to a new page - content.php
. I wonder what's the best way/the proper way to destroy the session and log out the users, redirectiong them back to the index.php
.
<?php
if (isset($_REQUEST['signin'])){
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM `user` WHERE username='$username' and password='$password'";
$result = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($result);
if ($count == 1){
$_SESSION['username'] = $username;
header('Location: content.php');
}
else{
echo "Invalid Login Credentials.";
}
}
if (isset($_SESSION['username'])){
$username = $_SESSION['username'];
header('Location: content.php');
}
?>
<form method="post" name="login">
<?php
if (isset($msg) & !empty($msg)) {
echo $msg;
}
?>
<label for="username">Username:</label><br>
<input type="text" name="username"><br>
<label for="password">Password:</label><br>
<input type="password" name="password"><br>
<button type="submit" name="signin">Sign in</button>
</form>
I know that there are flaws in this script (e.g. not encrypted password), but for now I'm looking for a simple script to log out.