【问题标题】:PHP Session DestroyPHP 会话销毁
【发布时间】:2011-11-06 20:36:11
【问题描述】:

如何在 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> 

【问题讨论】:

  • 将 index.php 替换为 Customer.php
  • 见到Bobby Tables时替我问好。
  • 您的 Customer.php 文件中已经有一个session_destroy 调用? “注销按钮”链接到哪里?
  • 因为我希望用户重定向到 index.php 页面

标签: php mysql database session destroy


【解决方案1】:
session_start();
session_destroy();

【讨论】:

    【解决方案2】:

    您也可以使用unset()函数来释放会话环境。

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

    【讨论】:

      【解决方案3】:

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

      <?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();
      ?>
      

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:
          //If you want complete destroy session then you can write.
          

          session_destroy();

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

          【讨论】:

            猜你喜欢
            • 2012-05-25
            • 1970-01-01
            • 1970-01-01
            • 2011-02-01
            • 1970-01-01
            • 2015-03-20
            • 1970-01-01
            • 2013-10-18
            • 2011-12-12
            相关资源
            最近更新 更多