【问题标题】:error with sessions variables at google app engine (php)谷歌应用引擎(php)的会话变量错误
【发布时间】:2021-02-19 18:23:09
【问题描述】:

每次我将应用程序部署到谷歌应用程序引擎时,我都会遇到同样的问题:从同一网站中的一个页面转到另一个页面时,我的会话变量消失了。在我的本地主机上运行我的应用程序时我没有这个问题,所以我相信这是因为错误的 GAE 配置(我是 GAE 的新手)。

我将在这里展示我的一个项目:

app.yaml:

runtime: php73

# Defaults to "serve index.php" and "serve public/index.php". Can be used to
# serve a custom PHP front controller (e.g. "serve backend/index.php") or to
# run a long-running PHP script as a worker process (e.g. "php worker.php").
#
# Serve your app through a front controller at index.php or public/index.php.
runtime_config:
  document_root: .

handlers:
# Serve images as static resources.
- url: /(.+\.(gif|png|jpg|css|js|map|PNG|svg))$
  static_files: \1
  upload: .+\.(gif|png|jpg|css|js|map|PNG|svg)$

- url: /.*
  script: auto

index.php:

<?php
ini_set('allow_url_fopen',1);
switch (@parse_url($_SERVER['REQUEST_URI'])['path']) {
    case '/':
        require 'first.php';
        break;
    case '/first.php':
        require 'first.php';
        break;
    case '/first':
        require 'first.php';
        break;
    case '/second.php':
        require 'second.php';
        break;
        case '/second':
            require 'second.php';
            break;
    default:
        require 'first.php';
        break;
}
 ?>

first.php:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>fisrt</title>
  </head>
  <body>
    <h1>first</h1>
    <?php
      session_start();
      $_SESSION['id'] = 123;
      $_SESSION['nombre'] = 'franc';
     ?>
     <script>
        window.location.href="second.php";
     </script>
  </body>
</html>

second.php:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Second</title>
  </head>
  <body>
    <h1>Second</h1>
    <?php
      session_start();
      echo $_SESSION['id'];
      echo $_SESSION['nombre'];
     ?>
  </body>
</html>

在我的本地主机中,当我运行此项目时,它按预期工作:索引处的前端控制器重定向到 first.php。 first.php 创建会话变量并重定向到 second.php。最后 second.php 打印出会话变量中的值。

但是,当我将相同的项目部署到 GAE 时,它会显示没有会话变量的 secon.php 页面: image of second.php at GAE

PD:我只是在没有数据库实例的谷歌应用引擎上部署项目。

【问题讨论】:

    标签: php google-app-engine session-variables


    【解决方案1】:

    我在本地和 GAE 中测试了您的文件,并看到了与您相同的结果,在本地工作,而不是 GAE 部署。您的 yaml 文件很好,但您的 php 文件应在顶部包含 session_start(),在任何 html 甚至 &lt;!DOCTYPE html&gt; 之前,如 @NoCommandLine 所述。

    我找不到任何确切的官方文档来说明为什么它应该放在首位,但社区众所周知,最佳实践要求它是这样的,this 同胞分享了我无法做到的更详细的见解去其他地方找。

    这是在应用程序后端发送的错误:...session_start(): Cannot start session when headers already sent in...。 要查看您的应用程序中的日志和事件,请查看日志浏览器的documentation。此工具可以帮助您识别诸如此类的错误和警告,以正确调试您的代码。

    这是您发送的正确使用session_start()的sn-ps:

    index.php

    我稍作调整并继续在此处添加session_start(),在 if 条件中确定是否已发送标头或会话 ID 是否为空

    <?php
    ini_set('allow_url_fopen',1);
    
    if( !headers_sent() && '' == session_id() ) {
    session_start();
    }
    
    switch (@parse_url($_SERVER['REQUEST_URI'])['path']) {
        case '/':
            require 'first.php';
            break;
        case '/first.php':
            require 'first.php';
            break;
        case '/first':
            require 'first.php';
            break;
        case '/second.php':
            require 'second.php';
            break;
            case '/second':
                require 'second.php';
                break;
        default:
            require 'first.php';
            break;
    }
     ?>
    

    first.php

    <?php
    session_start();
    ?>
    
    <!DOCTYPE html>
    <html lang="en" dir="ltr">
      <head>
        <meta charset="utf-8">
        <title>fisrt</title>
      </head>
      <body>
        <h1>first 1</h1>
        <?php
    
          $_SESSION['id'] = 123;
          $_SESSION['nombre'] = 'franc';
         ?>
         <script>
            window.location.href="second.php";
         </script>
      </body>
    </html>
    

    second.php

    <?php
    session_start();
    ?>
    
    <!DOCTYPE html>
    <html lang="en" dir="ltr">
      <head>
        <meta charset="utf-8">
        <title>Second</title>
      </head>
      <body>
        <h1>Second 1</h1>
        <?php
    
          echo $_SESSION['id'];
          echo $_SESSION['nombre'];
         ?>
      </body>
    </html>
    

    有了这些文件,您应该能够正确地执行测试。同样,我建议您最终研究一个 PHP 框架,该框架可以帮助您处理每个文件的这些简单问题。 Here 是关于 PHP 框架的更多信息。

    编码愉快!

    【讨论】:

      【解决方案2】:

      我相信session_start() 应该出现在文件顶部(在输出任何数据之前),即尝试将其放在&lt;!DOCTYPE html&gt; 之前

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-17
        • 1970-01-01
        • 1970-01-01
        • 2012-03-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多