2

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

認証コンポーネントを使用していない場合は、非常に簡単です

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 に答える
0
//If you want complete destroy session then you can write.

session_destroy();

The session_unset() //function frees all session variables currently registered.

于 2013-01-03T10:27:08.767 に答える