simple session question - Page 4

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Re: simple session question

On Oct 21, 11:19=A0am, Thomas Mlynarczyk <tho...@mlynarczyk-> wrote:
Quoted text here. Click to load it

Weird, my phpinfo() says: "Session Support    enabled"
but even with code like this
echo ++$_SESSION['test'];
just prints "session_start(); 1" - and i'm wondering why it's printing
session_start(); in the browser, seems like it doesn't know this
command...? Weird, any clues...?

Thanks alot!

Re: simple session question

cerr schrieb:
Quoted text here. Click to load it

Strange. Here it works. (Except for the notice about the undefined index
'test' at the first call, but that is easily fixed and has nothing to do
with the problem.) If your PHP installation did not know the
session_start() function it would give you a fatal error. Is your above
code the exact same code (copy & paste) you were running?


Ce n'est pas parce qu'ils sont nombreux avoir tort qu'ils ont raison!

Re: simple session question

On 10/21/2011 5:43 PM, h wrote:
Quoted text here. Click to load it

That's because the code you posted is not what you have in your script.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: simple session question

On Fri, 21 Oct 2011 20:19:53 +0200, Thomas Mlynarczyk wrote:

Quoted text here. Click to load it

Look at the start of his code.

Quoted text here. Click to load it

He always starts a session, and part of his session startup sets stp to 0.

So stp is always reset to 0 when a session is [re]started, not just when
a new session is created.

Now look at his "case 0" code

Quoted text here. Click to load it

The value of the expression "$_SESSION['stp']++" is the value of $_SESSION
['stp'] *before the increment operation*

eg, in:

x = y++;

x = original y
y = original y + 1

so he increments $_SESSION['stp'] and then assigns the value before it
was incremented back to the variable. As an aside, doing such things is
normally considered really really bad coding practice, as you can't
always guarantee that you'll get the same result, as you don't always
know which storage operation will occur last and thus determine the final
value of $_SESSION['stp']

e.g. the incremented value $_SESSION['stp']++ may be stored before or
after the evaluated expression $_SESSION['stp']= stores it's value. I
suspect that with c compilers, this would be an "undefined behaviour"

However, what's happening here is that by using a post increment, the
evaluation of the right side is the original value of the array element,
and that's the value that gets stored last.

So, he [re]starts the session, sets the 'stp' value to 0, increments it
and then over-writes the incremented value with the value before
incrementing, and finally:

Quoted text here. Click to load it

outputs the value, which is still 0.

And that, I suspect, is why he always gets a 0.

Basically, using:

x = x++;

is the headache, because in a single mathematical operation, you attempt
to define two conflicting values for x.

If he wants $_SESSION['stp'] to be 0 on the first pass and always
increment in the sequence 012012012 I'd do something like this:

if(!isset($_SESSION['stp'])) {
   $_SESSION['stp'] = 2;
   echo "new session created<br>\n";
$_SESSION['stp'] = ($_SESSION['stp'] + 1) % 3;
echo "stp value is <br>\n";

I set the initial value to 2 so that the add 1, modulo 3 operation will
return a 0 on the first occasion after the variable is created. Setting
the initial value to -1 will also work. If the initial value is set to 0,
the value will be 1 after the increment, which may not be what the OP
expects the first time the page is loaded? Of course, this will be for
the OP to decide in the context of how he uses the stp value.


Denis McMahon

Site Timeline