回显会话变量按预期工作,但是当“回显”时被省略,发出未定义的变量通知

发布于 2024-11-11 14:28:39 字数 492 浏览 1 评论 0原文

你好,我想取消这个例子。

当我

echo $_SESSION['lang'];

输出时,我得到了“en”,正如我所料。

zh

:当我在同一行中写入时

$_SESSION['lang'];

,我注意到索引 lang 未定义。

注意:未定义索引:lang ...

请注意,我在 Zend Framework 上,只有当我设置 Zend_Form_Hash 元素(该元素还设置了 $_SESSION['__ZF']时,才会发生这种情况) $_SESSION['Zend_Form_Element_Hash_unique_token'] 值。

有人能解释一下这个案例吗?

Hello I would like to undestard this example.

When I have

echo $_SESSION['lang'];

in output I have 'en' as I expected.

en

BUT: When I in the same line just write

$_SESSION['lang'];

I have notice, that index lang is undefined.

Notice: Undefined index: lang ...

Note that I'm on Zend Framework and this case occure ONLY when I set up Zend_Form_Hash element which also set up $_SESSION['__ZF'] and $_SESSION[' Zend_Form_Element_Hash_unique_token'] values.

Can anybody explain this case?

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

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

发布评论

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

评论(5

眼藏柔 2024-11-18 14:28:39

如果它位于同一个文件中,并且您只是交换了语句,那么 $_SESSION['lang']echo $_SESSION['lang'] 会访问任何不同的东西。

在发布实际答案之前,我们需要查看完整的代码和您实际在做什么的解释。

如果你真的像我说的那样这样做,那不会有什么区别:

<?php
session_start();
echo $_SESSION['lang'];

然后更改第3行:

<?php
session_start();
$_SESSION['lang'];

虽然第二行不会输出任何内容,但肯定不会有错误。

If it's in the same file, and you've simply interchanged the statements, there's absolutely no way that $_SESSION['lang'] and echo $_SESSION['lang'] would access anything different.

Before posting an actual answer, we need to see full code and explanation of what you're actually doing.

If you're literally doing this like I said, it won't make a difference:

<?php
session_start();
echo $_SESSION['lang'];

then changing line 3:

<?php
session_start();
$_SESSION['lang'];

Although the second line won't output anything, there certainly wouldn't be an error.

南…巷孤猫 2024-11-18 14:28:39

在第一个语句中,您将会话中“lang”索引的值发送到输出。在第二条语句中,您只是访问会话中“lang”索引的值,而不执行任何特殊操作。
两种说法都指的是相同的值。

所以问题不在于是否使用“echo”。问题是:
您确定会话中的“lang”索引在您的两次尝试中都可用吗?
您是否在同一文件中使用这两个语句来响应同一请求?会话值在用户“上下文”中设置并可用。

in your first statement, you are sending the value of the 'lang' index in the session to the output. in your second statement, you are just accessing the value of the 'lang' index in the session, and not doing anything special.
Both statements are referring to the same value.

So the problem is not between using the "echo" or not. The problem is that:
Are you sure that the 'lang' index in the session is available in your both tries?
Are you using both statements in the same file, in response to the same request? session values are set and available in the user "context".

污味仙女 2024-11-18 14:28:39

var_dump($_SESSION) 来找出 $_SESSION 里面到底是什么

var_dump($_SESSION) to find out what is exactly inside $_SESSION

落花随流水 2024-11-18 14:28:39

如果您使用 ZF 那么您应该使用 Zend_Session_Namespaces。
您需要像这样设置您的名称空间:

// setup namespace
$namespace = new Zend_Session_Namespace('locale');
// write values to your namespace with
$namespace->lang = 'en';
// read values from your namespace with
$language = $namespace->lang;

您还需要在早期阶段启动 Zend_Session,例如在引导程序或早期插件中:

Zend_Session::start();

亲切的问候
加里

If your using ZF then you should be using Zend_Session_Namespaces.
You need to setup your namespace like this:

// setup namespace
$namespace = new Zend_Session_Namespace('locale');
// write values to your namespace with
$namespace->lang = 'en';
// read values from your namespace with
$language = $namespace->lang;

You also need to start Zend_Session at an early stage, such as in your bootstrap or an early plugin with:

Zend_Session::start();

Kind regards
Garry

混浊又暗下来 2024-11-18 14:28:39

在 bootstrap.php

  1. 中,我启动自己的会话。这些值从数据库存储/恢复。因此,我初始化 $_SESSION 值。

  2. 然后我处理语言值。只需设置即可。

<代码>
$_SESSION['lang'] = 'en';

当我检查这个值时,它按我的预期工作。问题是当我使用 Zend_Form_Element_Hash 时,它使用“自己的”SESSION 处理。我没有深入研究它,但我解释说,当我在类方法中回显 SESSION 时,我可以将“lang”视为“en”,但如果没有回显,代码就会继续前进,Zend_Form_Element_Hash 重写完整的会话,这就是为什么“lang”是丢失的。

我只是实现了我自己的哈希令牌并继续...

感谢大家的帮助并对不清楚的请求表示歉意。

In bootstrap.php

  1. I start my own session. The values are stored/restored from DB. So I initilaize $_SESSION values.

  2. Then I handle language value. Simply set.


$_SESSION['lang'] = 'en';

When I check this value it works as I expect. The problem is when I used Zend_Form_Element_Hash which uses "own" SESSION handling. I didnt study it deeply, but I explain it that when I echo SESSION inside class method I can see as 'lang' as 'en' but without echo the code go forward and Zend_Form_Element_Hash rewrite complete session and that is why the 'lang' is missing.

I just implement my own Hash Token and go on ...

Thanks guys for helping and aplologize for unclear request.

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