【问题标题】:PHP Session won't work after refresh刷新后 PHP 会话将不起作用
【发布时间】:2013-11-29 15:24:19
【问题描述】:

我已经尝试添加一个 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_start()两次。
  • error_reporting(o);真的吗?
  • 您的第二个 session_start() 将失败,因为您已经执行了输出。还有...error_report(o)?真的吗?信o?和未定义的常量?
  • 好吧,只要你用 $_POST['code'] 覆盖 $_SESSION['pass'] ...如果 $_POST['code'] 为空,那么 $_SESSION[ '通过']
  • 为什么要在脚本标签中添加样式? ;)

标签: php mysql session


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    需要

    session_start();
    if ('POST' === $_SERVER['REQUEST_METHOD']) {
        $_SESSION["pass"] = $_POST["code"];
    }
    

    【讨论】:

      【解决方案3】:
      1. 您应该检查您的服务器是否配置正确。检查会话存储和存储的数据
      2. 您应该在任何输出之前开始会话(检查您在哪里开始会话并且您在之后没有空格?>)。覆盖超级全局值是不好的做法

      【讨论】:

        【解决方案4】:

        我很抱歉这么说,但你真的需要阅读更多关于 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>
        

        不要复制粘贴,查看已完成的操作并评估将发生的情况,还有一个缺失的变量,所以你必须照顾好它

        【讨论】:

          猜你喜欢
          • 2012-08-30
          • 2015-06-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-08-18
          • 2014-07-09
          相关资源
          最近更新 更多