2011-08-30 188 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()//函數釋放當前註冊的所有會話變量。