PHP会话销毁

时间:2011-08-30 14:11:15

标签: php mysql database session destroy

如何在php中销毁会话?

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

Customer.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> 

5 个答案:

答案 0 :(得分:4)

session_start();
session_destroy();

答案 1 :(得分:1)

您也可以使用unset()功能来自由启动会话环境。

if (isset($_SESSION['user']))
{
  unset($_SESSION['user']);
  header('location:index.php');
}

答案 2 :(得分: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();
?>

答案 3 :(得分:0)

如果您没有使用auth组件,那么它非常简单

public function logout(){
    $this->Session->destroy();
    // no cake we really want you to delete it because you suck
    $this->Session->destroy();
}

答案 4 :(得分:0)

//If you want complete destroy session then you can write.

session_destroy();

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