Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 警告:会话_destroy():尝试销毁未初始化的会话[使用了会话_start()]_Php_Html_Mysql - Fatal编程技术网

Php 警告:会话_destroy():尝试销毁未初始化的会话[使用了会话_start()]

Php 警告:会话_destroy():尝试销毁未初始化的会话[使用了会话_start()],php,html,mysql,Php,Html,Mysql,我知道我的代码是非常初学者,因为我刚刚开始php。 一开始我很抱歉 我已经阅读了有关的其他问题和答案 警告:会话_destroy():尝试销毁未初始化的会话 所有答案都提到,在使用之前,我需要使用session\u start() 会话_destroy() 但即使我已经使用了session\u start() 我只是尝试登录,然后仪表板页面出现,用户可以点击注销按钮注销 整个应用程序运行良好,唯一的问题是当我点击dashboard.php中的注销按钮时,它会进入注销页面并显示上面提到的警告 lo

我知道我的代码是非常初学者,因为我刚刚开始php。 一开始我很抱歉

我已经阅读了有关的其他问题和答案

警告:会话_destroy():尝试销毁未初始化的会话

所有答案都提到,在使用之前,我需要使用session\u start()

会话_destroy()

但即使我已经使用了session\u start() 我只是尝试登录,然后仪表板页面出现,用户可以点击注销按钮注销

整个应用程序运行良好,唯一的问题是当我点击dashboard.php中的注销按钮时,它会进入注销页面并显示上面提到的警告

login.php

<?php
session_start();
}
?>
<!DOCTYPE html>
<html>
<head>
  <title>Login Page</title>
  <link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>
<div class="header">
  <h1 align="center">Login</h1>
</div>
  orm action="" method="post">
  <table align="center">
    <tr>
<td align="right"><input type="email" name="email" placeholder="Email" required></td>
<td class="s1">*</td>
    </tr>
    <tr>
<td align="right"><input  type="password" name="pass" placeholder="Password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,12}" title="Must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters" required></td>
<td class="s1">*</td>
    </tr>
    <tr>
<td align="right"><input type="submit" value="Login"><td>
    </tr>
  </table>
  <p align="center"><strong>Or</strong></p>
  <p align="center">Create New acount <strong><input type="button" value="Sign Up" onclick="location.href='sign_up.php'"></strong></p>
  <br>
  <br>
  <br>
  <br>
  <br>
<div class="footer">
  <p><span class="s1">*</span> indicates mandatory feild<p>
</div>
</body>
</html>

<?php
require 'connection.php'; // connection

if($_SERVER['REQUEST_METHOD'] === 'POST')
{
$email = $_POST['email'];
$pass = $_POST['pass'];
// getting username from database and storing into session variable
///  $sql =mysqli_query($conn,"SELECT name FROM users WHERE email='$email'");
  $sql1 = mysqli_fetch_assoc($sql);
  $user_name = $sql1['name'];
//  setting session variables
  $_SESSION['user_name']= $cookie_name;
  $_SESSION['timeout']=time();

// check email exists or not
$query=mysqli_query($conn,"SELECT email FROM users WHERE email='$email'");
if(mysqli_num_rows($query) > 0)
{
  //check email and password correct or not
  $query1=mysqli_query($conn,"SELECT email, password FROM users WHERE email='$email' AND password='$pass'");
  if(mysqli_num_rows($query1) > 0)
  {
    // login success
    header("location:dashboard.php");
    // ---->  alternative way to redirect to dashboard.php
  /*echo  "<script type='text/javascript'>
window.location.href = 'dashboard.php'
</script>";*/
  }
  else
  {
    // incorrect password
    echo "<p align='center' style='color:#ff6262'>your password is incorrect!<br>Please try again</p>";
  }
}
else
{
  // email not exist
    echo "<p align='center' style='color:#ff6262'><b>Email you are entering does not exist in our database<br></b></p>";
}
 }
 ?>

登录页面
登录
orm action=”“method=“post”>
*
*

创建新帐户






*表示强制字段
logout.php

<?php
session_start();
?>
<?php
session_unset();
if(!session_unset())
{
  echo "session not unset";
}
else {
  echo "session unset";
}

session_destroy();
if(!session_destroy())
{
  echo "session not destroyed";
}
else {
  echo "session destroyed";
}
//header(location:login.php);
 ?>


其他一些人建议我使用cookie,但我不知道如何将cookie与会话联系起来,谁能提供帮助。

您正在运行
session\u destroy()
两次!这将导致您的错误。if语句中的代码在初始
会话_destroy()
之后运行

您也在为
会话\u unset()
执行此操作,相同的修复

这是导致问题的代码(第二个
会话\u destroy()
返回false):


您可以更改为此,以解决错误

if(!session_destroy())
{
  echo "session not destroyed";
}
else {
  echo "session destroyed";
}

您是否在日志文件中也收到了已发送的
头文件
错误?您正在调用会话\u destroy两次,以及会话\u unset。从手册中删除第一个。注意:仅对不使用$\u会话的旧的不推荐代码使用session_unset()。此问答将解释下一个错误,您将不会得到任何信息。如果我在1分钟之前单击“注销”按钮,销毁会话的仪表板中的代码将不会运行,并且我还通过注释进行检查。您对此有何评论?您在logout.php中运行了两次。超时后,您将运行它3次。一次在仪表板中,两次在logout.php文件中。是的,你说得对,我打了两次电话。谢谢
session_destroy(); // returns true
if(!session_destroy()) // returns false, because the session is already destroyed.
{
  echo "session not destroyed"; // this gets ran
}
else {
  echo "session destroyed";
}
if(!session_destroy())
{
  echo "session not destroyed";
}
else {
  echo "session destroyed";
}