开发者

Echoing a session variable works as expected, but when 'echo' is omitted, undefined variable notice is emitted

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?


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.


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".


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜