无法将数组保存到会话,显示为空

发布于 2024-12-27 17:31:29 字数 1110 浏览 1 评论 0原文

如果您能让这个简单的会话变量示例正常工作,我会拥抱您!
我无法让会话变量在我的网站上工作。我正在 Windows 7 上使用 Chrome 浏览器运行 php5。

这是发送页面:

    <?php
     session_start();
     $red='red';
     $blue='blue';
     $green='green';

     $SESSVARS=array($red,$blue,$green);

     $_SESSION['USERVARS']= $SESSVARS; 

     ?>

 <p>Set Sessionvars</p>
 <form action="SessVarCheck.php" method="post">
 <input name="Submit" type="submit">
 </form>

这是结果页面:

   <?php
    session_start();
    echo "val 1:".$_SESSION['USERVARS'][0];
    echo "val 2:". $_SESSION['USERVARS'][1];
    echo "val 3:". $_SESSION['USERVARS'][2];
   ?>
   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SessVarCheck.php</title>
</head>

<body>
The Result page
</body>
</html>

我在所有三个回声上都为空。 任何想法都会受到欢迎!

If you can get this simple SESSION VARIABLES example to work, I'll hug you!
I can't get Session Variables to work at all on my site. I'm running php5, on Windows 7, using Chrome browser.

Here's the send page:

    <?php
     session_start();
     $red='red';
     $blue='blue';
     $green='green';

     $SESSVARS=array($red,$blue,$green);

     $_SESSION['USERVARS']= $SESSVARS; 

     ?>

 <p>Set Sessionvars</p>
 <form action="SessVarCheck.php" method="post">
 <input name="Submit" type="submit">
 </form>

Here's the result page:

   <?php
    session_start();
    echo "val 1:".$_SESSION['USERVARS'][0];
    echo "val 2:". $_SESSION['USERVARS'][1];
    echo "val 3:". $_SESSION['USERVARS'][2];
   ?>
   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SessVarCheck.php</title>
</head>

<body>
The Result page
</body>
</html>

I get empty on all three echos.
Any ideas would be welcome!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖心男生 2025-01-03 17:31:29

确保在两个页面上输出任何页面内容之前调用 session_start()。 (请参阅此处的“注意”部分)

(我假设您可能已经知道这一点,但鉴于我在发送页面上没有看到“”,我认为该脚本可能还有更多内容。另外,我不知道这个要求有多敏感 - 只是空格。在 php 标签之前可能足以成为一个问题。)

Make sure that the call to session_start() happens before any page contents are output, on both pages. (See "Note" section here)

(I assume you may already know about this, but given that I don't see a "<html>" on the send page I thought there might be more to that script. Also, I don't know how sensitive this requirement is - just the spaces before the php tag might be enough to be a problem.)

就是爱搞怪 2025-01-03 17:31:29

请尝试以下操作:

1) 在结果页面上执行 var_dump($_SESSION),以查看会话的原始内容是什么

2) 验证会话是否正常工作。添加 echo "SessionID: " 。 session_id(); 到两个脚本,在 session_start() 调用之后的某个地方。如果您没有在每个页面上获得相同的会话 ID,则每次都会获得一个新会话,这很可能是 cookie 问题。

Try a few things:

1) do a var_dump($_SESSION) on your results page, to see what the raw contents of the session are

2) Verify that the sessions are working correctly. Add echo "SessionID: " . session_id(); to both scripts, somewhere AFTER the session_start() calls. If you don't get the same session ID on each page, you're getting a new session each time and it's most likely a cookie problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文