警告:session_destroy():试图破坏未初始化的会话[使用的session_start()]

时间:2019-04-19 14:35:57

标签: php html mysql

我知道我的代码非常入门,因为我刚开始使用php。 很抱歉开始

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

警告:session_destroy():尝试破坏未初始化的会话

所有刚才提到的答案,我需要在使用

之前使用 session_start()

session_destroy();

但是他们没有解释为什么即使我已经使用过 session_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>";
}
 }
 ?>

dashboard.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();

 ?>

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与可以帮助的人联系起来。

1 个答案:

答案 0 :(得分:1)

您两次运行'create recordset of all detail rows for a single dispensing event strSQLDetail = "Select pbm.NDC, pbm.Total_Quantity, pbm.Extended_List_Price, pbm.Extended_Net_Price, pbm.UNIT_OF_MEASURE_CODE as UOM, pay.PRICE " & _ "from PBM_HCN_DATA as pbm left outer join [PBM Payers] as pay on pbm.payer_org_id=pay.org_id and pbm.plan_code=pay.plan_code " & _ "left outer join [guidant_cardiac_milrinone] as guid on pbm.payer_org_id=guid.payer_org_id and pbm.plan_code=guid.plan_code and pbm.ndc=guid.ndc and pbm.ro_rxnbr=guid.ro_rxnbr and pbm.ro_rxfill=guid.ro_rxfill " & _ "where pbm.Patient_ID = '" & DISTINCTrs.Fields(0).Value & "' and pbm.SHIP_DATE = #" & DISTINCTrs.Fields(1).Value & "# and pbm.Rx_Number = '" & DISTINCTrs.Fields(2).Value & "' and pbm.Refill_Number = '" & DISTINCTrs.Fields(3).Value & "';" Debug.Print strSQLDetail 'set cursor type to allow use of the record count property DETAILrs.CursorType = adOpenStatic DETAILrs.Open strSQLDetail, CurrentProject.Connection 'loop through the details for a single dispensing event For x = 1 To DETAILrs.RecordCount If UOM = "EA" Then dfd = 5 Else dfd = 11 End If If UOM = "EA" Then dfu = 1 Else dfu = 3 End If If DETAILrs.Fields(5).Value = "Net" Then costbasis = "03" priceamt = DETAILrs.Fields(3).Value Else costbasis = "07" priceamt = DETAILrs.Fields(2).Value End If If DETAILrs.Fields(6).Value = "Y" Then costbasis = "15" Else costbasis = "07" priceamt = DETAILrs.Fields(2).Value End If !这会导致您的错误。 if语句中的代码在初始session_destroy()之后运行。

您也正在为session_destroy()做同样的修复。

这是引起问题的代码(第二个session_unset()返回false):

session_destroy()

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

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