Welcome to WebmasterWorld Guest from 174.129.127.214

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Session Problem in PHP

Apache : 1.3.26 PHP : 4.0.5

   
9:44 am on Jul 24, 2002 (gmt 0)

10+ Year Member



While handling session variables unexpected results occurs.
First time a page is accesed session variable is not set.
Second time the same page executes fine.

What is the remedy?

10:24 am on Jul 26, 2002 (gmt 0)

10+ Year Member



Please be more specific, I'm not sure I really understand the question.

php sessions dont start automatically, you have to start them once with session_start();

7:04 pm on Jul 29, 2002 (gmt 0)

10+ Year Member



Session variables are not set until the entire PHP document has been parsed.

I ran into a more subtle variation on this, which mystified me for ages. Take this code:


<?
session_start();
session_register("my_variable");
$my_variable="Hello world";
header("Location: next.php");
exit;
?>

The session variable will never be set. Why? Because the browser is redirected before the end of the script has been parsed. If you've ever wondered why bulletin boards usually display a "Your message has been sent" page before redirecting with a <meta> refresh... that's the reason.

5:15 pm on Jul 30, 2002 (gmt 0)

10+ Year Member



Are you sure?
I believe it is because it should be like this:
<?
session_start();
$my_variable="Hello world";
session_register("my_variable");
header("Location: next.php");
exit;
?>
Fist assign the value, then register the variable.
Works for me all the time.
You might try without the session_start(), I never use it and everything works all right.
5:56 pm on Jul 30, 2002 (gmt 0)

10+ Year Member



It doesn't matter if you register the variable before or after you set it. I can't think how your session variables can possibly work without session_start() -- they're not supposed to.
11:52 pm on Jul 30, 2002 (gmt 0)

10+ Year Member



Of course it works! I tell you, I do it all the time! :)
Here, from the PHP.net documentation:
If session_start() was not called before this function (session_register) is called, an implicit call to session_start() with no parameters will be made.

[php.net ]

And yes, the order is imporant, because you can't change the value of a variable after registering it.
Have you tried this?


<?
session_register("variable");
unset($variable);
session_unregister("variable");
$variable = "foobar";
session_register("variable");
header("location: next.php");
exit;
?>

-- next.php --

<?
session_register("variable");
echo $variable;
?>

This must work!
Cheers!
5:56 pm on Jul 31, 2002 (gmt 0)

10+ Year Member



I change the values of registered variables all the time.

page1;

<?php
session_start();
$myVAR = "23";
session_register("$myVAR");
?>

page2;

<?php
session_start();
$myVAR++;
?>

page3;

<?php
echo "$myVAR"; // returns 24
?>

You can use one page for all this using conditional statements or switch.

But as far as I know it always works for me with no problems, unless you try to re-register the variable.

11:33 am on Aug 2, 2002 (gmt 0)

10+ Year Member



Of course it works! I tell you, I do it all the time!
Here, from the PHP.net documentation: If session_start() was not called before this function (session_register) is called, an implicit call to session_start() with no parameters will be made.

Ah, but only if your script includes session_register(). It will often be the case that you want to use session variables on a page that doesn't register any; in which case, you must have a call to session_start()

Also, of course you can change the values of a registered session variables. Session variables are variables, which means the values they contained can be changed at any time -- that's what "variable" means. However, I have discovered that if you redirect the browser, under certain conditions which have little to do with the placement of session_register() (I know, because my first thought was that I'd put it in the wrong place, so I moved it), the value of the variable as recorded in the text file that stores session variables is not updated.

11:41 am on Aug 2, 2002 (gmt 0)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Yes, you definately change session vars after registering them.

That's kind of one of the key points.

Nick

7:58 pm on Aug 2, 2002 (gmt 0)

10+ Year Member



Yes, yes! I'm sorry, bad boy! bad boy! :)
They can be changed.
What I wanted to say was, that session variables cannot be changed by the user, that is, if you have a session_register("variable") and its value is 3, and you load a page [domain.com...] $variable will be 3.
I am right on this, am I not?

And if you change a variable after registering it and then, go to another page, the new page will reflect the new value?
I mean:
-- page1.php --
<?
session_start();
$variable = 3;
session_register("variable");
$variable++;
header ("location: next.php");
exit;
?>

-- rext.php --
<?
session_start();
session_register("variable");
echo $variable;
?>
--
It prints 4!
Sessions are so cool!

7:04 am on Aug 4, 2002 (gmt 0)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Yes, they are!

No need for the session register on the next page BTW.

Also, there are times when you want a var passed in the url to affect the session: Very easy:


if(£varInUrl="yes") {
$sessionVar="yes";
} else $sessionVsr="no";

Just be careful with sessions and SE's, be aware that it's easy to fall into a 'duplicate content trap'.

Nick