PHP会话获胜';刷新后无法工作


PHP Session won't work after refresh

我尝试添加一个PHP会话,这样他们就不必一直填写相同的密码。如果我转到页面并填写代码,它将显示具有正确会话的页面,但当我刷新页面时,会话已消失。

会话代码:

<head>
    <script>
        { background-color:#87CEFA; }
    </script>
</head>
<?php
    error_reporting(0);
    session_start();
    $_SESSION["pass"] = $_POST["code"];
    $pass = $_POST["code"];
    $con=mysqli_connect("localhost","bb","$pass","bb");
    if (mysqli_connect_errno($con))
    {
        echo "Kan geen verbinding maken, de ingevulde code is verkeerd of de server is      offline!";
        echo 'Hello '.$_SESSION["pass"].'!';
    }
    $result = mysqli_query($con,"SELECT * FROM ftp");
    while($row = mysqli_fetch_array($result))
    {
        $myuser = $row['user'];
        $mypass = $row['pass'];
        $myhost = $row['host'];
        $conn_id = ftp_connect($myhost) or die("Couldn't connect to $myhost"); 
        if (@ftp_login($conn_id, $myuser, $mypass))
        {
             //Path to your *.txt file:
             $file = $ftp_server['DOCUMENT_ROOT'] . "bbbb";
             $contents = file($file); 
             $string = implode($contents); 
             echo $string;
         }
     }
     mysqli_close($con);
?>

谢谢。

在每次页面加载时,您都在运行代码$_SESSION["pass"] = $_POST["code"];。然后你试着像echo 'Hello '.$_SESSION["pass"].'!';一样回应它。实际上,您所做的是呼应$_POST["code"]。只有当您提交表单时,才会设置$_POST变量。

每次加载页面时都会覆盖会话变量。在设置会话变量之前,您需要检查表单是否已提交

session_start();
$_SESSION["pass"] = $_POST["code"];

需要

session_start();
if ('POST' === $_SERVER['REQUEST_METHOD']) {
    $_SESSION["pass"] = $_POST["code"];
}
  1. 您应该检查您的服务器配置是否正确。检查会话存储和存储的数据
  2. 您应该在任何输出之前启动会话(请检查启动会话的位置,并且在?>之后没有空间)。重写超全局值是一种糟糕的做法

很抱歉这么说,但您确实需要阅读更多关于php会话的内容。

我随意改写和评论,但如果你有什么不明白的地方,请问

组合脚本

<?php
// protect against any output, leave no spaces or shenanigans before the session_start()
// even warnings and errors
// this is the first thing that has to happen BEFORE ANY OUTPUT
session_start();
// is this a good idea when you're in development
error_reporting(0);
// we don't know if $_POST['whateva'] actually exists
if (isset($_POST['code'])) {
  // this may not be such a good idea...
  $_SESSION['pass'] = $_POST['code'];
}
$pass = isset($_POST['code']) ? $_POST['code'] : '';
$error = '';
try {
  $con = mysqli_connect('localhost','bb',$pass,'bb');// is this barebones MVC? just curious
  if (mysqli_connect_errno($con)) throw new Exception('Kan geen verbinding maken, de ingevulde code is verkeerd of de server is offline! Hello '.$_SESSION['pass'].'!');
  $result = mysqli_query($con,'SELECT * FROM ftp');
  $ftpContent = array();
  while ($row = mysqli_fetch_array($result)) {
    $myuser = $row['user'];
    $mypass = $row['pass'];
    $myhost = $row['host'];
    if (!$conn_id = ftp_connect($myhost)) throw new Exception('Couldn''t connect to '.$myhost);
    if (@ftp_login($conn_id,$myuser,$mypass)) {
      //Path to your *.txt file:
      $file = $ftp_server['DOCUMENT_ROOT'].'bbbb';// where does this $ftp_server come from????
      $contents = file($file);
      $string = implode($contents);
      $ftpContent[$myuser] = $string;
    }
  }
  mysqli_close($con);
} catch (Exception $e) {
  $error = $e->getMessage();
}
// now output all your stuff here
?>
<!DOCTYPE html>
<html>
  <head>
    <style>body{background-color:#87cefa;}.error{color:#f00;}</style>
  </head>
  <body>
    <?php if ($error) echo '<p class="error">There has been an error: '.$error.'</p>'; ?>
  </body>
</html>

不要复制粘贴这个,查找已经完成的内容并评估将要发生的事情,还有一个丢失的变量,所以你必须处理