警告:session_destroy():试图销毁未初始化的会话[session_start() used]

绍伊卜·萨比尔

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

我已经阅读堆栈其他问题和答案

警告:session_destroy():试图销毁未初始化的会话

所有答案都提到我需要在使用之前使用session_start()

session_destroy();

但他们没有解释为什么会出现这个警告,即使我已经使用了session_start()我只是想登录然后仪表板页面出现,用户可以点击注销按钮注销

整个应用程序工作正常,唯一的问题是当我单击dashboard.php 中的注销按钮时,它会转到注销页面并显示上面提到的警告

登录.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>";
}
 }
 ?>

仪表盘.php

<?php
session_start();
?>

<!DOCTYPE html>
<html>
<head>
  <title>
    <?php
require 'connection.php';
echo $_SESSION['user_name'];
    ?>
  </title>
  <link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>
<div class="header">  <h1 align="center"><?php echo "Welcome:  ".ucfirst($_SESSION['user_name']); ?></h1>
</div>
<table>
  <tr>
<td align="right">Change User Name:</td><td><input type="button" onclick="location.href='user.php'" value="click here"></td>
</tr>
<tr></tr>
<tr>
<td align="right">Change Password:</td><td><input type="button" onclick="location.href='pass.php'" value="click here"></td>
</tr colspan="2">
<tr><td align="right"><input type="button" value="Logout" onclick="location.href='logout.php'"></td></tr>
</table>

</body>
</html>
<?php
if (isset($_SESSION['timeout']) && (time() - $_SESSION['timeout'] > 60)) {

    session_unset();     // unset $_SESSION variable for the run-time
    session_destroy();   // destroy session data in storage
    header("location:logout.php");
}
$_SESSION['timeout'] = time();

 ?>

登出.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 与任何可以提供帮助的人联系起来。

转数192

你跑了session_destroy()两次!这会导致您的错误。if语句运行的代码的最初session_destroy()

你也在这样做session_unset(),同样的修复。

这是导致问题的代码(第二个session_destroy()返回 false):

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";
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

错误— session_destroy()—试图破坏未初始化的会话

尽管称为 session_destroy(),但会话并未销毁

session_destroy()+ session_start()不在头重定向之后

$ _SESSION 未初始化 - PHP

session_start():在yii2中启用memcache时,无法初始化存储模块:memcache(路径:/ tmp)

session_start():无法解码会话对象

关于session_destroy的晦涩

Unset() 和 session_destroy();

session_start()每次刷新都会创建新的会话,没有警告

警告:session_start():找不到保存处理程序 's' - 会话启动失败

警告:session_start():无法发送会话缓存限制器 - 即使在使用 session_start() 和 ob_start() 后,标头也已发送

session_start 的使用

PHP session_start 不接触会话文件

session_destroy()没有注销我

session_destroy()在PHP中做什么?

警告:session_start():找不到保存处理程序“redis”

警告:session_start():open(/ var / www / temp

为什么session_destroy无法取消设置当前会话值php

警告:session_regenerate_id():会话对象销毁失败

修改session.save_path(会话数据保存在服务器上)后,session_destroy无法正常工作

requests.session 初始化会话并输入搜索词

Session_Start多次触发

PHP条件session_start

练习使用session_start()

PHP session_start()的用法

致命错误:session_start()

使用未初始化的变量警告

未初始化的警告-C编程

警告:变量未初始化