如何在我的PHP中实现注销按钮?

时间:2017-06-18 07:16:41

标签: php html if-statement

我正在试图找出如何在我的代码中实现“注销”按钮, 但似乎无法弄清楚如何做到这一点。这是我的登录代码 页面和索引页面。

索引页面HTML

<?php
include("connectDatabase.php");
include("products.php");
?>
<?php
require "logincheck.php";
?>

<html>
<head>
<br><br>
<a href="resetPassword.php">Reset Password Page</a><br><br>
<a href="login.php">Login Page</a><br><br>
<a href="forgotPassword.php">Forgot Password Page</a><br><br>
<a href="logincheck.php">Login Check Page</a><br><br>
<a href="register.php">Register Page</a><br><br>

</head>
<body>
    <h1> Small IT Business <h1>
    Welcome <?php echo $_SESSION["email"] ?>.
</body>
</html>

登录页面PHP

<?php
session_start();

if (isset ($_SESSION["email"]) && isset($_SESSION["loggedIn"])) {
    header("Location: index.php");
    exit();
}
if(isset($_POST["logIn"])) {
    $connection =  new mysqli("localhost", "root", "", "membershipsystem");

$email = $connection->real_escape_string($_POST["email"]);
$password = sha1($connection->real_escape_string($_POST["password"]));

$data = $connection->query("SELECT firstName FROM users WHERE 
email='$email' AND '$password'");

if($data->num_rows > 0) {
       $_SESSION["email"] = $email;
       $_SESSION["loggedIn"] = 1;


       header("Location: index.php");
       exit();
}else{
        echo "Please check your imputs!";
}
}
?>
<html>
<body>
    <form actions="login.php" method="post"> 
        <input type="text" name="email" placeholder="Email"/><br />
        <input type="password" name="password" placeholder="Password"/><br 
/>
        <input type="submit" value="Log In" name="logIn" />
</body>
</html>

3 个答案:

答案 0 :(得分:1)

要注销,您需要销毁会话。

<?php
session_start();
session_destroy()
?>

<html>
<head></head>
  <body>
     <h1>You have been logged out.<h1>
  </body>
</html>

按键取消设置(类似于数组未设置)

//This will remove your custom session by key
unset($_SESSION['email']);
unset($_SESSION['loggedIn']);

删除所有会话变量(类似于设置空白数组$ a = array();)

session_unset(); 

销毁会话(这将取消设置会话并删除服务器上的会话文件。)

session_destroy();

答案 1 :(得分:0)

在那里创建logout.phpdestroy会话

<?php
session_start();
session_destroy();
echo 'You have been logged out';

将退出链接放在所有必需的页面中。

<a href='logout.php'>Log out</a>

答案 2 :(得分:0)

<强> Home.php

<?php
include 'connect_to_database.php';

 // connect the connection page

if (empty($_SESSION)) // if the session not yet started
session_start();

if (!isset($_SESSION['email']))
    { //if not yet logged in
    header("Location: login.php"); // send to login page
    exit;
    }

?>

<html>
<body>
    Welcome <?php echo $_SESSION['email']; ?>, <a href="logout.php">logout</a> 
</body>
</html> 

<强> Logout.php

<?php
session_start();
unset($_SESSION['email']);
unset($_SESSION['loggedIn']);
session_destroy();
header("Location: login.php");
exit;
?>