Ways to assign a value to a session variable.

Ways to assign a value to a session variable.

am 14.01.2008 02:24:16 von kurdayon

I am wandering which way to assign a value to a session variable
exist. Which of the following examples will work.

Example #1:
session_start();
session_register("ex");
$ex = 2.0;

Example #2:
In first.php:
session_start();
session_register("ex");
In second.php:
$ex = 2.0;

Example #3:
In first.php:
session_start();
session_register("ex");
Load second.php?ex=2.0

Example #4:
In first.php:
session_start();
session_register("ex");
Go to the second.php via submission of the form on the first.php,
which contains form-variable $ex.

Example #4 seams not to work? Is it expectable?

Re: Ways to assign a value to a session variable.

am 14.01.2008 02:25:46 von kurdayon

Sorry, I made a mistake. In all 4 cases the second.php should contain
session_start();

Re: Ways to assign a value to a session variable.

am 14.01.2008 02:43:06 von Peter Pei

unless u r using an older version, otherwise avoid session_register

Re: Ways to assign a value to a session variable.

am 14.01.2008 02:43:43 von Peter Pei

"Kurda Yon" wrote in message
news:420f8581-3870-4908-bb70-61593fbcac8b@f47g2000hsd.google groups.com...
> Sorry, I made a mistake. In all 4 cases the second.php should contain
> session_start();
>

Re: Ways to assign a value to a session variable.

am 14.01.2008 02:44:01 von Peter Pei

that's a given

Re: Ways to assign a value to a session variable.

am 14.01.2008 02:46:28 von Peter Pei

4 works if you did things right. cannot go more detailed than that unless
see some code.

Re: Ways to assign a value to a session variable.

am 14.01.2008 02:52:44 von kurdayon

On Jan 13, 8:43 pm, "Peter Pei" wrote:
> unless u r using an older version, otherwise avoid session_register

The example #4 seems not to work. In the second file the session
variable do not want to take the value of the form variable. I tried
to force session to do it by the explicit command: $_SESSION["ex"] =
$ex; But it also does not help. I think that session value of the
variable has a higher priority with respect to the from variable. In
the first file I set the value to the session variable $ex, and than I
submit a form which change the value of the $ex variable, but the
second file does not want to see the values of the form variables. It
sees the values of the session variables.

Re: Ways to assign a value to a session variable.

am 14.01.2008 02:53:34 von Jerry Stuckle

Kurda Yon wrote:
> I am wandering which way to assign a value to a session variable
> exist. Which of the following examples will work.
>
> Example #1:
> session_start();
> session_register("ex");
> $ex = 2.0;
>
> Example #2:
> In first.php:
> session_start();
> session_register("ex");
> In second.php:
> $ex = 2.0;
>
> Example #3:
> In first.php:
> session_start();
> session_register("ex");
> Load second.php?ex=2.0
>
> Example #4:
> In first.php:
> session_start();
> session_register("ex");
> Go to the second.php via submission of the form on the first.php,
> which contains form-variable $ex.
>
> Example #4 seams not to work? Is it expectable?
>

session_register() has been deprecated. The correct way to do it is to
use the $_SESSION superglobal array, i.e.

session_start();
$_SESSION['ex'] = 2.0;

session_start();
$ex = $_SESSION['ex'];

BTW - there is no direct relationship between 'ex' as an index value,
and $ex as a variable.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Ways to assign a value to a session variable.

am 14.01.2008 02:53:53 von luiheidsgoeroe

On Mon, 14 Jan 2008 02:24:16 +0100, Kurda Yon wrote:

> I am wandering which way to assign a value to a session variable
> exist. Which of the following examples will work.

Don't use session_register() anymore. Use a reference if you must.
--
Rik Wasmus

Re: Ways to assign a value to a session variable.

am 14.01.2008 02:56:18 von Peter Pei

there is a relationship if register_global is on, this is at least true for
5.2.5 on vista

Re: Ways to assign a value to a session variable.

am 14.01.2008 02:59:39 von kurdayon

On Jan 13, 8:46 pm, "Peter Pei" wrote:
> 4 works if you did things right. cannot go more detailed than that unless
> see some code.

OK. In the first.php I have:
session_start();
session_register("ex");
$ex = 2.0;
..........


......
print "\n";
......


When I submit the form I put in the text field corresponding to $ex
3.0.

In the second.php I have:
session_start();
$_SESSION["ex"] = $ex;
print "===> $txt_l_1";
print "---> {$_SESSION["txt_l_1"]}";
die();

As the output I have:
===> 2.0
---> 2.0

Not 3.0, as I expect.

Re: Ways to assign a value to a session variable.

am 14.01.2008 03:03:29 von luiheidsgoeroe

On Mon, 14 Jan 2008 02:59:39 +0100, Kurda Yon wrote=
:
> On Jan 13, 8:46 pm, "Peter Pei" wrote:
>> 4 works if you did things right. cannot go more detailed than that =

>> unless
>> see some code.
>
> OK. In the first.php I have:
> session_start();
> session_register("ex");

$_SESSION['ex'] =3D 2.0;
$ex =3D &$_SESSION['ex'];

> .........
>

post">
> .....
> print "\n";
> .....
>

>
> When I submit the form I put in the text field corresponding to $ex
> 3.0.
>
> In the second.php I have:
> session_start();
> $_SESSION["ex"] =3D $ex;

Why overwrite the value in the $_SESSION array here? If you're not even =
=

using it, why store it to begin with?

> print "===3D> $txt_l_1";
> print "---> {$_SESSION["txt_l_1"]}";


Euhm, what magic do you expect from a in this sample code uninitiated & =
=

unassigned variable $txt_1_1?
-- =

Rik Wasmus

Re: Ways to assign a value to a session variable.

am 14.01.2008 03:05:04 von Peter Pei

this is because of the order globals are assigned. u mapped the global
twice, once from _SESSION, once from _POST. don't duplicate the indexes - it
only confuses u and cause nasty bugs.

It's more php's fault than yrs - global, and bad global.

Re: Ways to assign a value to a session variable.

am 14.01.2008 03:09:33 von kurdayon

> > OK. In the first.php I have:
> > session_start();
> > session_register("ex");
>
> $_SESSION['ex'] = 2.0;
> $ex = &$_SESSION['ex'];
What for do you introduce $ex variable? What for do you have "&" in
the second line?

>
> > .........
> >


> > .....
> > print "\n";
> > .....
> >

>
> > When I submit the form I put in the text field corresponding to $ex
> > 3.0.
>
> > In the second.php I have:
> > session_start();
> > $_SESSION["ex"] = $ex;
>
> Why overwrite the value in the $_SESSION array here? If you're not even
> using it, why store it to begin with?
>
$ex supposed to be a form variable and before to use "header" I would
like to put the values of the form variables into the session
variables.

> > print "===> $txt_l_1";
> > print "---> {$_SESSION["txt_l_1"]}";
>
> Euhm, what magic do you expect from a in this sample code uninitiated &
> unassigned variable $txt_1_1?
Instead of $txt_l_1 should be $ex.