2011-08-30 285 views
2

如何在php中销毁会话?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> 

,这是从当用户想再次登录

<?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> 
+0

用Customer.php替换index.php –

+3

http://php.net/session_destroy – deceze

+9

当你看到他时,向我问好'Bobby Tables'。 –

回答

4
session_start(); 
session_destroy(); 
+1

我不认为这是解决他的问题.. – Rijk

1

您也可以使用的index.php功能释放会话环境。

if (isset($_SESSION['user'])) 
{ 
    unset($_SESSION['user']); 
    header('location:index.php'); 
} 
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(); 
?> 
0

如果不使用身份验证组件然后它真的很容易

public function logout(){ 
    $this->Session->destroy(); 
    // no cake we really want you to delete it because you suck 
    $this->Session->destroy(); 
} 
0
//If you want complete destroy session then you can write. 

session_destroy();

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