2

如何在 php 中销毁会话?

问题是当用户单击注销按钮时,会话将结束,他将被重定向到 index.php 这是我的代码

客户.php

<?php 

session_start(); 
#include("Connection.php");
if (isset($_POST['submit'])) { 
$name = $_POST['customerName']; 
$_SESSION['user'] = $name; 
} 
if (isset($_SESSION['user'])) { echo "Hello {$_SESSION['user']}, welcome back"; }
else{echo "walang tao";}

$sql="INSERT INTO ORDERS(Name) VALUES('$name')";
mysql_query($sql);

session_destroy();
?>
<button><a href="Customer.php"></a></button>

这是来自用户想要再次登录的 index.php

<?PHP 
/* this must go before any html */ 
session_start(); 

if (isset($_SESSION['user'])) { 
header("location: Customer.php"); 
} 
?> 
     <div class="sign">
                    <h2>Welcome</h2>
                    <form action = "Customer.php" method = "POST">
                    Customer Name:<input type = "text" name="customerName">
                    <input type = "submit" name = "submit">
                    </form> 
4

5 回答 5

4
session_start();
session_destroy();
于 2011-08-30T14:15:03.847 回答
1

您也可以使用unset()释放会话环境的功能。

if (isset($_SESSION['user']))
{
  unset($_SESSION['user']);
  header('location:index.php');
}
于 2012-04-15T08:55:26.560 回答
0

将此文件包含在您的标题中并在文件中设置所需的设置。它应该运作良好。

<?php
    session_cache_expire(20);
    if (!isset($_SESSION)) {    
        session_start();
    }
    // set timeout period in seconds
    $inactive = 1200; // timeout for the session
    // check to see if $_SESSION['timeout'] is set
    if(isset($_SESSION['timeout']) ) {
        $session_life = time() - $_SESSION['timeout'];
        if($session_life > $inactive) {
            $_SESSION = array();
            if(isset($_COOKIE[session_name()])) {
                setcookie(session_name(), '', time()-42000, '/');
            } 
            session_destroy(); 
            header("Location: index.php"); // or whatever you prefer to do. 
        }
    }
    $_SESSION['timeout'] = time();
?>
于 2012-06-06T14:21:20.667 回答
0
//If you want complete destroy session then you can write.

session_destroy();

session_unset() //函数释放当前注册的所有会话变量。

于 2013-01-03T10:27:08.767 回答
0

如果您不使用 auth 组件,那么它真的很容易

public function logout(){
    $this->Session->destroy();
    // no cake we really want you to delete it because you suck
    $this->Session->destroy();
}
于 2012-09-17T16:33:00.500 回答